Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import google.generativeai as genai | |
# Configure Gemini API | |
GEMINI_API_KEY = "AIzaSyDooG1UD_7ae5lgl7HwfsVyGlwn2XycXko" | |
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_path): | |
"""Analyze the uploaded image using Gemini model""" | |
try: | |
# Start a new chat session | |
chat = model.start_chat() | |
# Upload and analyze the image | |
image_file = genai.upload_file(image_path.name) | |
# Send the image with a prompt | |
response = chat.send_message([ | |
image_file, | |
"Here is an xray image, describe all objects you can see in this image and their relative positions to each other." | |
]) | |
return response.text | |
except Exception as e: | |
return f"Error analyzing image: {str(e)}" | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=analyze_image, | |
inputs=gr.File(label="Upload X-ray Image"), | |
outputs=gr.Textbox(label="Analysis Result", lines=10), | |
title="X-ray Image Object Detection", | |
description="Upload an X-ray image and get a detailed description of the objects detected in it.", | |
examples=[], | |
cache_examples=False | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
iface.launch() |