Spaces:
Sleeping
Sleeping
Zane Vijay Falcao
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,121 @@
|
|
1 |
-
import os
|
2 |
-
from PIL import Image as PILImage
|
3 |
-
from agno.agent import Agent
|
4 |
-
from agno.models.google import Gemini
|
5 |
-
from agno.tools.duckduckgo import DuckDuckGoTools
|
6 |
-
from agno.media import Image as AgnoImage
|
7 |
-
import gradio as gr
|
8 |
-
|
9 |
-
# Set your API Key (Replace with your actual key)
|
10 |
-
GOOGLE_API_KEY = "AIzaSyCUPYFW5ESk1YIwSjwpGd3jZJJ7oPAX4_s"
|
11 |
-
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
12 |
-
|
13 |
-
# Ensure API Key is provided
|
14 |
-
if not GOOGLE_API_KEY:
|
15 |
-
raise ValueError("⚠️ Please set your Google API Key in GOOGLE_API_KEY")
|
16 |
-
|
17 |
-
# Initialize the Medical Agent
|
18 |
-
medical_agent = Agent(
|
19 |
-
model=Gemini(id="gemini-2.0-flash-exp"),
|
20 |
-
tools=[DuckDuckGoTools()],
|
21 |
-
markdown=True
|
22 |
-
)
|
23 |
-
|
24 |
-
# Medical Analysis Query
|
25 |
-
query = """
|
26 |
-
You are a highly skilled medical imaging expert with extensive knowledge in radiology and diagnostic imaging. Analyze the medical image and structure your response as follows:
|
27 |
-
|
28 |
-
### 1. Image Type & Region
|
29 |
-
- Identify imaging modality (X-ray/MRI/CT/Ultrasound/etc.).
|
30 |
-
- Specify anatomical region and positioning.
|
31 |
-
- Evaluate image quality and technical adequacy.
|
32 |
-
|
33 |
-
### 2. Key Findings
|
34 |
-
- Highlight primary observations systematically.
|
35 |
-
- Identify potential abnormalities with detailed descriptions.
|
36 |
-
- Include measurements and densities where relevant.
|
37 |
-
|
38 |
-
### 3. Diagnostic Assessment
|
39 |
-
- Provide primary diagnosis with confidence level.
|
40 |
-
- List differential diagnoses ranked by likelihood.
|
41 |
-
- Support each diagnosis with observed evidence.
|
42 |
-
- Highlight critical/urgent findings.
|
43 |
-
|
44 |
-
### 4. Patient-Friendly Explanation
|
45 |
-
- Simplify findings in clear, non-technical language.
|
46 |
-
- Avoid medical jargon or provide easy definitions.
|
47 |
-
- Include relatable visual analogies.
|
48 |
-
|
49 |
-
### 5. Research Context
|
50 |
-
- Use DuckDuckGo search to find recent medical literature.
|
51 |
-
- Search for standard treatment protocols.
|
52 |
-
- Provide 2-3 key references supporting the analysis.
|
53 |
-
|
54 |
-
Ensure a structured and medically accurate response using clear markdown formatting.
|
55 |
-
"""
|
56 |
-
|
57 |
-
# Function to analyze medical image
|
58 |
-
def analyze_medical_image(image):
|
59 |
-
"""Processes and analyzes a medical image using AI."""
|
60 |
-
if image is None:
|
61 |
-
return "⚠️ Please upload an image to analyze."
|
62 |
-
|
63 |
-
# Save the input image to a temporary file
|
64 |
-
temp_path = "temp_image.png"
|
65 |
-
image.save(temp_path)
|
66 |
-
|
67 |
-
# Create AgnoImage object
|
68 |
-
agno_image = AgnoImage(filepath=temp_path)
|
69 |
-
|
70 |
-
# Run AI analysis
|
71 |
-
try:
|
72 |
-
response = medical_agent.run(query, images=[agno_image])
|
73 |
-
return response.content
|
74 |
-
except Exception as e:
|
75 |
-
return f"⚠️ Analysis error: {e}"
|
76 |
-
finally:
|
77 |
-
# Clean up temporary file
|
78 |
-
if os.path.exists(temp_path):
|
79 |
-
os.remove(temp_path)
|
80 |
-
|
81 |
-
# Create Gradio interface
|
82 |
-
with gr.Blocks(title="Medical Image Analysis") as demo:
|
83 |
-
gr.Markdown(
|
84 |
-
"""
|
85 |
-
# 🩺 Medical Image Analysis Tool 🔬
|
86 |
-
|
87 |
-
Welcome to the **Medical Image Analysis** tool! 📸
|
88 |
-
|
89 |
-
Upload a medical image (X-ray, MRI, CT, Ultrasound, etc.), and our AI-powered system will analyze it,
|
90 |
-
providing detailed findings, diagnosis, and research insights.
|
91 |
-
|
92 |
-
Let's get started!
|
93 |
-
"""
|
94 |
-
)
|
95 |
-
|
96 |
-
with gr.Row():
|
97 |
-
with gr.Column(scale=1):
|
98 |
-
input_image = gr.Image(type="pil", label="Upload Medical Image")
|
99 |
-
analyze_button = gr.Button("Analyze Image", variant="primary")
|
100 |
-
|
101 |
-
with gr.Column(scale=2):
|
102 |
-
output_report = gr.Markdown(label="Analysis Report")
|
103 |
-
|
104 |
-
analyze_button.click(
|
105 |
-
fn=analyze_medical_image,
|
106 |
-
inputs=input_image,
|
107 |
-
outputs=output_report
|
108 |
-
)
|
109 |
-
|
110 |
-
gr.Examples(
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
)
|
118 |
-
|
119 |
-
# Launch the application
|
120 |
-
if __name__ == "__main__":
|
121 |
demo.launch(debug=True, share=True)
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image as PILImage
|
3 |
+
from agno.agent import Agent
|
4 |
+
from agno.models.google import Gemini
|
5 |
+
from agno.tools.duckduckgo import DuckDuckGoTools
|
6 |
+
from agno.media import Image as AgnoImage
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Set your API Key (Replace with your actual key)
|
10 |
+
GOOGLE_API_KEY = "AIzaSyCUPYFW5ESk1YIwSjwpGd3jZJJ7oPAX4_s"
|
11 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
12 |
+
|
13 |
+
# Ensure API Key is provided
|
14 |
+
if not GOOGLE_API_KEY:
|
15 |
+
raise ValueError("⚠️ Please set your Google API Key in GOOGLE_API_KEY")
|
16 |
+
|
17 |
+
# Initialize the Medical Agent
|
18 |
+
medical_agent = Agent(
|
19 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
20 |
+
tools=[DuckDuckGoTools()],
|
21 |
+
markdown=True
|
22 |
+
)
|
23 |
+
|
24 |
+
# Medical Analysis Query
|
25 |
+
query = """
|
26 |
+
You are a highly skilled medical imaging expert with extensive knowledge in radiology and diagnostic imaging. Analyze the medical image and structure your response as follows:
|
27 |
+
|
28 |
+
### 1. Image Type & Region
|
29 |
+
- Identify imaging modality (X-ray/MRI/CT/Ultrasound/etc.).
|
30 |
+
- Specify anatomical region and positioning.
|
31 |
+
- Evaluate image quality and technical adequacy.
|
32 |
+
|
33 |
+
### 2. Key Findings
|
34 |
+
- Highlight primary observations systematically.
|
35 |
+
- Identify potential abnormalities with detailed descriptions.
|
36 |
+
- Include measurements and densities where relevant.
|
37 |
+
|
38 |
+
### 3. Diagnostic Assessment
|
39 |
+
- Provide primary diagnosis with confidence level.
|
40 |
+
- List differential diagnoses ranked by likelihood.
|
41 |
+
- Support each diagnosis with observed evidence.
|
42 |
+
- Highlight critical/urgent findings.
|
43 |
+
|
44 |
+
### 4. Patient-Friendly Explanation
|
45 |
+
- Simplify findings in clear, non-technical language.
|
46 |
+
- Avoid medical jargon or provide easy definitions.
|
47 |
+
- Include relatable visual analogies.
|
48 |
+
|
49 |
+
### 5. Research Context
|
50 |
+
- Use DuckDuckGo search to find recent medical literature.
|
51 |
+
- Search for standard treatment protocols.
|
52 |
+
- Provide 2-3 key references supporting the analysis.
|
53 |
+
|
54 |
+
Ensure a structured and medically accurate response using clear markdown formatting.
|
55 |
+
"""
|
56 |
+
|
57 |
+
# Function to analyze medical image
|
58 |
+
def analyze_medical_image(image):
|
59 |
+
"""Processes and analyzes a medical image using AI."""
|
60 |
+
if image is None:
|
61 |
+
return "⚠️ Please upload an image to analyze."
|
62 |
+
|
63 |
+
# Save the input image to a temporary file
|
64 |
+
temp_path = "temp_image.png"
|
65 |
+
image.save(temp_path)
|
66 |
+
|
67 |
+
# Create AgnoImage object
|
68 |
+
agno_image = AgnoImage(filepath=temp_path)
|
69 |
+
|
70 |
+
# Run AI analysis
|
71 |
+
try:
|
72 |
+
response = medical_agent.run(query, images=[agno_image])
|
73 |
+
return response.content
|
74 |
+
except Exception as e:
|
75 |
+
return f"⚠️ Analysis error: {e}"
|
76 |
+
finally:
|
77 |
+
# Clean up temporary file
|
78 |
+
if os.path.exists(temp_path):
|
79 |
+
os.remove(temp_path)
|
80 |
+
|
81 |
+
# Create Gradio interface
|
82 |
+
with gr.Blocks(title="Medical Image Analysis") as demo:
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
# 🩺 Medical Image Analysis Tool 🔬
|
86 |
+
|
87 |
+
Welcome to the **Medical Image Analysis** tool! 📸
|
88 |
+
|
89 |
+
Upload a medical image (X-ray, MRI, CT, Ultrasound, etc.), and our AI-powered system will analyze it,
|
90 |
+
providing detailed findings, diagnosis, and research insights.
|
91 |
+
|
92 |
+
Let's get started!
|
93 |
+
"""
|
94 |
+
)
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column(scale=1):
|
98 |
+
input_image = gr.Image(type="pil", label="Upload Medical Image")
|
99 |
+
analyze_button = gr.Button("Analyze Image", variant="primary")
|
100 |
+
|
101 |
+
with gr.Column(scale=2):
|
102 |
+
output_report = gr.Markdown(label="Analysis Report")
|
103 |
+
|
104 |
+
analyze_button.click(
|
105 |
+
fn=analyze_medical_image,
|
106 |
+
inputs=input_image,
|
107 |
+
outputs=output_report
|
108 |
+
)
|
109 |
+
|
110 |
+
# gr.Examples(
|
111 |
+
# examples=[
|
112 |
+
# "examples/xray_chest.jpg",
|
113 |
+
# "examples/mri_brain.jpg",
|
114 |
+
# "examples/ct_abdomen.jpg"
|
115 |
+
# ],
|
116 |
+
# inputs=input_image
|
117 |
+
# )
|
118 |
+
|
119 |
+
# Launch the application
|
120 |
+
if __name__ == "__main__":
|
121 |
demo.launch(debug=True, share=True)
|