Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,22 +21,26 @@ 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(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
|
| 37 |
response = chat.send_message([
|
| 38 |
image_file,
|
| 39 |
-
|
| 40 |
])
|
| 41 |
|
| 42 |
return response.text
|
|
@@ -48,7 +52,7 @@ 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 |
]
|
|
@@ -56,10 +60,17 @@ if os.path.exists(examples_dir):
|
|
| 56 |
# Create Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=analyze_image,
|
| 59 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
outputs=gr.Textbox(label="Analysis Result", lines=10),
|
| 61 |
title="X-ray Image Object Detection",
|
| 62 |
-
description="Upload an X-ray image and
|
| 63 |
examples=example_images,
|
| 64 |
cache_examples=True
|
| 65 |
)
|
|
|
|
| 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, instruction):
|
| 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 |
+
# Use default instruction if none provided
|
| 31 |
+
if not instruction:
|
| 32 |
+
instruction = "Here is an xray image, describe all objects you can see in this image and their relative positions to each other."
|
| 33 |
+
|
| 34 |
# Start a new chat session
|
| 35 |
chat = model.start_chat()
|
| 36 |
|
| 37 |
# Upload and analyze the image
|
| 38 |
image_file = genai.upload_file(image)
|
| 39 |
|
| 40 |
+
# Send the image with the provided instruction
|
| 41 |
response = chat.send_message([
|
| 42 |
image_file,
|
| 43 |
+
instruction
|
| 44 |
])
|
| 45 |
|
| 46 |
return response.text
|
|
|
|
| 52 |
example_images = []
|
| 53 |
if os.path.exists(examples_dir):
|
| 54 |
example_images = [
|
| 55 |
+
[os.path.join(examples_dir, f), "Here is an xray image, do you see keys in this image, if yes then describe its nature in few words and location relative to the other objects."]
|
| 56 |
for f in os.listdir(examples_dir)
|
| 57 |
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
|
| 58 |
]
|
|
|
|
| 60 |
# Create Gradio interface
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=analyze_image,
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.Image(type="filepath", label="Upload X-ray Image"),
|
| 65 |
+
gr.Textbox(
|
| 66 |
+
label="Instruction",
|
| 67 |
+
placeholder="Enter your instruction (e.g., 'do you see keys in this image? If yes, describe their location')",
|
| 68 |
+
value="Here is an xray image, do you see keys in this image, if yes then describe its nature in few words and location relative to the other objects."
|
| 69 |
+
)
|
| 70 |
+
],
|
| 71 |
outputs=gr.Textbox(label="Analysis Result", lines=10),
|
| 72 |
title="X-ray Image Object Detection",
|
| 73 |
+
description="Upload an X-ray image and provide instructions for analysis. The model will detect and describe objects based on your instructions.",
|
| 74 |
examples=example_images,
|
| 75 |
cache_examples=True
|
| 76 |
)
|