Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
# Configure Gemini API
|
6 |
+
GEMINI_API_KEY = "AIzaSyDooG1UD_7ae5lgl7HwfsVyGlwn2XycXko"
|
7 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
8 |
+
|
9 |
+
# Create the model with the same configuration as the sample
|
10 |
+
generation_config = {
|
11 |
+
"temperature": 1,
|
12 |
+
"top_p": 0.95,
|
13 |
+
"top_k": 40,
|
14 |
+
"max_output_tokens": 8192,
|
15 |
+
"response_mime_type": "text/plain",
|
16 |
+
}
|
17 |
+
|
18 |
+
model = genai.GenerativeModel(
|
19 |
+
model_name="gemini-1.5-pro",
|
20 |
+
generation_config=generation_config,
|
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_path):
|
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(image_path.name)
|
32 |
+
|
33 |
+
# Send the image with a prompt
|
34 |
+
response = chat.send_message([
|
35 |
+
image_file,
|
36 |
+
"Here is an xray image, describe all objects you can see in this image and their relative positions to each other."
|
37 |
+
])
|
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.File(label="Upload X-ray Image"),
|
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=False
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the app
|
55 |
+
if __name__ == "__main__":
|
56 |
+
iface.launch()
|