Rahatara commited on
Commit
bc712dc
·
verified ·
1 Parent(s): c61c0cd

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -130
app.py DELETED
@@ -1,130 +0,0 @@
1
- import gradio as gr
2
- import os
3
- from io import BytesIO
4
- from PIL import Image, ImageDraw, ImageFont
5
- from PIL import ImageColor
6
- import json
7
- from google import genai
8
- from google.genai import types
9
-
10
- # Initialize Google Gemini client
11
- client = genai.Client(api_key=os.environ['GOOGLE_API_KEY'])
12
- model_name = "gemini-2.0-flash-exp"
13
-
14
-
15
- #Skin issue detection system instructions
16
- bounding_box_system_instructions = """
17
- Return bounding boxes as a JSON array with labels specific to medical ultrasound imaging. Never return masks or code fencing. Limit to 25 objects per frame."""
18
-
19
- # Additional colors f
20
-
21
- # Additional colors for bounding box visualization
22
- additional_colors = [colorname for (colorname, colorcode) in ImageColor.colormap.items()]
23
-
24
- def parse_json(json_output):
25
- """
26
- Parse JSON output from the Gemini model.
27
- """
28
- try:
29
- json_start = json_output.find("[")
30
- json_end = json_output.rfind("]")
31
- if json_start == -1 or json_end == -1:
32
- raise ValueError("Bounding box JSON not found in response.")
33
- return json_output[json_start:json_end + 1]
34
- except Exception as e:
35
- print(f"Error parsing JSON: {e}")
36
- return None
37
-
38
- def plot_bounding_boxes(im, bounding_boxes):
39
- """
40
- Plots bounding boxes on an image with labels.
41
- """
42
- im = im.copy()
43
- draw = ImageDraw.Draw(im)
44
- colors = ['red', 'green', 'blue', 'yellow', 'pink'] + additional_colors
45
-
46
- try:
47
- bounding_boxes_json = json.loads(bounding_boxes)
48
- for i, bounding_box in enumerate(bounding_boxes_json):
49
- color = colors[i % len(colors)]
50
- x_min, y_min = bounding_box["x_min"], bounding_box["y_min"]
51
- x_max, y_max = bounding_box["x_max"], bounding_box["y_max"]
52
- draw.rectangle(((x_min, y_min), (x_max, y_max)), outline=color, width=4)
53
- if "label" in bounding_box:
54
- label = f"{bounding_box['label']} ({bounding_box['metadata']['severity']})"
55
- draw.text((x_min + 5, y_min - 10), label, fill=color)
56
- except Exception as e:
57
- print(f"Error drawing bounding boxes: {e}")
58
-
59
- return im
60
-
61
- def predict_bounding_boxes(image, prompt):
62
- """
63
- Process the image and prompt through Gemini and draw bounding boxes.
64
- """
65
- try:
66
- image = image.resize((1024, int(1024 * image.height / image.width)))
67
- buffered = BytesIO()
68
- image.save(buffered, format="JPEG")
69
- image_bytes = buffered.getvalue()
70
-
71
- response = client.models.generate_content(
72
- model=model_name,
73
- contents=[prompt, image],
74
- config=types.GenerateContentConfig(
75
- system_instruction=bounding_box_system_instructions,
76
- temperature=0.5
77
- )
78
- )
79
-
80
- bounding_boxes = parse_json(response.text)
81
- if not bounding_boxes:
82
- raise ValueError("No bounding boxes returned.")
83
- return plot_bounding_boxes(image, bounding_boxes)
84
- except Exception as e:
85
- print(f"Error: {e}")
86
- return image
87
-
88
- def gradio_interface():
89
- """
90
- Gradio app interface for skin issue detection.
91
- """
92
- examples = [
93
- ["skin_example1.jpg", "Detect and label skin abnormalities."],
94
- ["skin_example2.jpg", "Find acne lesions and classify severity."],
95
- ["skin_example3.jpg", "Identify suspicious moles and discolorations."],
96
- ]
97
-
98
- with gr.Blocks() as demo:
99
- gr.Markdown("# Skin Issue Detection with Gemini")
100
-
101
- with gr.Row():
102
- with gr.Column():
103
- input_image = gr.Image(type="pil", label="Input Image")
104
- input_prompt = gr.Textbox(
105
- lines=2,
106
- label="Input Prompt",
107
- placeholder="Describe what to detect (e.g., 'Identify acne lesions').",
108
- value="Identify and label skin abnormalities."
109
- )
110
- submit_btn = gr.Button("Generate")
111
-
112
- with gr.Column():
113
- output_image = gr.Image(type="pil", label="Output Image")
114
-
115
- gr.Examples(
116
- examples=examples,
117
- inputs=[input_image, input_prompt]
118
- )
119
-
120
- submit_btn.click(
121
- predict_bounding_boxes,
122
- inputs=[input_image, input_prompt],
123
- outputs=[output_image]
124
- )
125
-
126
- return demo
127
-
128
- if __name__ == "__main__":
129
- app = gradio_interface()
130
- app.launch()