Spaces:
Sleeping
Sleeping
File size: 2,922 Bytes
7f0b420 6d4a991 7f0b420 d7f368c 7f0b420 526c870 d7f368c 7f0b420 526c870 7f0b420 d7f368c 7f0b420 d7f368c 7f0b420 526c870 d7f368c 526c870 7f0b420 d7f368c 7f0b420 d7f368c 526c870 7f0b420 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import os
import gradio as gr
import google.generativeai as genai
# Configure Gemini API
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
genai.configure(api_key=GEMINI_API_KEY)
# Create the model with the same configuration as the sample
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
system_instruction="You are an expert in detecting objects from xray image. Your job is to detect the objects from x-ray images.",
)
def analyze_image(image, instruction):
"""Analyze the uploaded image using Gemini model"""
try:
if image is None:
return "Please upload an image to analyze."
# Use default instruction if none provided
if not instruction:
instruction = "Here is an xray image, describe all objects you can see in this image and their relative positions to each other."
# Start a new chat session
chat = model.start_chat()
# Upload and analyze the image
image_file = genai.upload_file(image)
# Send the image with the provided instruction
response = chat.send_message([
image_file,
instruction
])
return response.text
except Exception as e:
return f"Error analyzing image: {str(e)}\nPlease make sure you've uploaded a valid image file."
# Get example images from the examples directory
examples_dir = os.path.join(os.path.dirname(__file__), "examples")
example_images = []
if os.path.exists(examples_dir):
example_images = [
[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."]
for f in os.listdir(examples_dir)
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
]
# Create Gradio interface
iface = gr.Interface(
fn=analyze_image,
inputs=[
gr.Image(type="filepath", label="Upload X-ray Image"),
gr.Textbox(
label="Instruction",
placeholder="Enter your instruction (e.g., 'do you see keys in this image? If yes, describe their location')",
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."
)
],
outputs=gr.Textbox(label="Analysis Result", lines=10),
title="X-ray Image Object Detection",
description="Upload an X-ray image and provide instructions for analysis. The model will detect and describe objects based on your instructions.",
examples=example_images,
cache_examples=True
)
# Launch the app
if __name__ == "__main__":
iface.launch() |