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): """Analyze the uploaded image using Gemini model""" try: if image is None: return "Please upload an image to analyze." # Start a new chat session chat = model.start_chat() # Upload and analyze the image image_file = genai.upload_file(image) # 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)}\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) 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"), 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=example_images, cache_examples=True ) # Launch the app if __name__ == "__main__": iface.launch()