File size: 1,713 Bytes
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
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()