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": 0.2, "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 fish quality analyser. You use following criteria to identify the quality of the fish and score them out of 10 points.\n\nPrimary Key Indicators for Fish Quality Assessment\n1. Eyes\nFreshness Indicator: The eyes should be clear, bright, and bulging with a metallic sheen.\nDeterioration Signs: If the eyes appear cloudy, sunken, or have a greyish color, it indicates that the fish is not fresh and may be decomposing.\n2. Gills\nFreshness Indicator: Gills should be bright red or pink, indicating fresh blood and optimal freshness.\nDeterioration Signs: Gills turning brown, grey, or green suggest that the fish is past its prime and potentially spoiled.\n3. Skin Appearance\nFreshness Indicator: The skin should have a shiny, mother-of-pearl sheen and be free of blemishes or discoloration.\nDeterioration Signs: Dullness or yellowing of the skin indicates degradation and loss of freshness.\n4. Odor\nFreshness Indicator: Fresh fish typically has a neutral smell with slight hints of seaweed.\nDeterioration Signs: A sour, off, or overly strong odor suggests spoilage.\n5. Texture\nFreshness Indicator: The flesh should be firm to the touch and spring back when pressed.\nDeterioration Signs: Softness or mushiness indicates that the fish is no longer fresh.\n6. Color of Flesh\nFreshness Indicator: The flesh should have a vibrant color appropriate for the species (e.g., pink for salmon).\nDeterioration Signs: Any discoloration or dullness in the flesh can indicate spoilage.\n7. Overall Appearance\nFreshness Indicator: The overall appearance should be appealing without any signs of drying out or excessive slime.\nDeterioration Signs: Excessive slime or drying out can indicate that the fish is not fresh.", ) 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 = "Now provided the image of a fish, give me two output. Dont generate any more extra text other then the following.\n1. Fish Quality: Good/Average/Bad\n2. Quality Score (0-10): ", # 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), "Now provided the image of a fish, give me two output. Dont generate any more extra text other then the following.\n1. Fish Quality: Good/Average/Bad\n2. Quality Score (0-10): "] 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 Fish Image"), gr.Textbox( label="Instruction", placeholder="Enter your instruction (e.g., 'do you see keys in this image? If yes, describe their location')", value="Now provided the image of a fish, give me two output. Dont generate any more extra text other then the following.\n1. Fish Quality: Good/Average/Bad\n2. Quality Score (0-10): " ) ], outputs=gr.Textbox(label="Analysis Result", lines=10), title="Fish Quality Detection", description="Upload an image of a fish 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()