Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,14 +21,17 @@ model = genai.GenerativeModel(
|
|
| 21 |
system_instruction="You are an expert in detecting objects from xray image. Your job is to detect the objects from x-ray images.",
|
| 22 |
)
|
| 23 |
|
| 24 |
-
def analyze_image(
|
| 25 |
"""Analyze the uploaded image using Gemini model"""
|
| 26 |
try:
|
|
|
|
|
|
|
|
|
|
| 27 |
# Start a new chat session
|
| 28 |
chat = model.start_chat()
|
| 29 |
|
| 30 |
# Upload and analyze the image
|
| 31 |
-
image_file = genai.upload_file(
|
| 32 |
|
| 33 |
# Send the image with a prompt
|
| 34 |
response = chat.send_message([
|
|
@@ -38,17 +41,27 @@ def analyze_image(image_path):
|
|
| 38 |
|
| 39 |
return response.text
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"Error analyzing image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Create Gradio interface
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=analyze_image,
|
| 46 |
-
inputs=gr.
|
| 47 |
outputs=gr.Textbox(label="Analysis Result", lines=10),
|
| 48 |
title="X-ray Image Object Detection",
|
| 49 |
description="Upload an X-ray image and get a detailed description of the objects detected in it.",
|
| 50 |
-
examples=
|
| 51 |
-
cache_examples=
|
| 52 |
)
|
| 53 |
|
| 54 |
# Launch the app
|
|
|
|
| 21 |
system_instruction="You are an expert in detecting objects from xray image. Your job is to detect the objects from x-ray images.",
|
| 22 |
)
|
| 23 |
|
| 24 |
+
def analyze_image(image):
|
| 25 |
"""Analyze the uploaded image using Gemini model"""
|
| 26 |
try:
|
| 27 |
+
if image is None:
|
| 28 |
+
return "Please upload an image to analyze."
|
| 29 |
+
|
| 30 |
# Start a new chat session
|
| 31 |
chat = model.start_chat()
|
| 32 |
|
| 33 |
# Upload and analyze the image
|
| 34 |
+
image_file = genai.upload_file(image)
|
| 35 |
|
| 36 |
# Send the image with a prompt
|
| 37 |
response = chat.send_message([
|
|
|
|
| 41 |
|
| 42 |
return response.text
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"Error analyzing image: {str(e)}\nPlease make sure you've uploaded a valid image file."
|
| 45 |
+
|
| 46 |
+
# Get example images from the examples directory
|
| 47 |
+
examples_dir = os.path.join(os.path.dirname(__file__), "examples")
|
| 48 |
+
example_images = []
|
| 49 |
+
if os.path.exists(examples_dir):
|
| 50 |
+
example_images = [
|
| 51 |
+
os.path.join(examples_dir, f)
|
| 52 |
+
for f in os.listdir(examples_dir)
|
| 53 |
+
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
|
| 54 |
+
]
|
| 55 |
|
| 56 |
# Create Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=analyze_image,
|
| 59 |
+
inputs=gr.Image(type="filepath", label="Upload X-ray Image"),
|
| 60 |
outputs=gr.Textbox(label="Analysis Result", lines=10),
|
| 61 |
title="X-ray Image Object Detection",
|
| 62 |
description="Upload an X-ray image and get a detailed description of the objects detected in it.",
|
| 63 |
+
examples=example_images,
|
| 64 |
+
cache_examples=True
|
| 65 |
)
|
| 66 |
|
| 67 |
# Launch the app
|