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()