Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import google.generativeai as genai | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# 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", | |
} | |
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. | |
Primary Key Indicators for Fish Quality Assessment | |
1. Eyes | |
Freshness Indicator: The eyes should be clear, bright, and bulging with a metallic sheen. | |
Deterioration Signs: If the eyes appear cloudy, sunken, or have a greyish color, it indicates that the fish is not fresh and may be decomposing. | |
2. Gills | |
Freshness Indicator: Gills should be bright red or pink, indicating fresh blood and optimal freshness. | |
Deterioration Signs: Gills turning brown, grey, or green suggest that the fish is past its prime and potentially spoiled. | |
3. Skin Appearance | |
Freshness Indicator: The skin should have a shiny, mother-of-pearl sheen and be free of blemishes or discoloration. | |
Deterioration Signs: Dullness or yellowing of the skin indicates degradation and loss of freshness. | |
4. Odor | |
Freshness Indicator: Fresh fish typically has a neutral smell with slight hints of seaweed. | |
Deterioration Signs: A sour, off, or overly strong odor suggests spoilage. | |
5. Texture | |
Freshness Indicator: The flesh should be firm to the touch and spring back when pressed. | |
Deterioration Signs: Softness or mushiness indicates that the fish is no longer fresh. | |
6. Color of Flesh | |
Freshness Indicator: The flesh should have a vibrant color appropriate for the species (e.g., pink for salmon). | |
Deterioration Signs: Any discoloration or dullness in the flesh can indicate spoilage. | |
7. Overall Appearance | |
Freshness Indicator: The overall appearance should be appealing without any signs of drying out or excessive slime. | |
Deterioration Signs: Excessive slime or drying out can indicate that the fish is not fresh.""" | |
model = genai.GenerativeModel( | |
model_name="gemini-2.0-flash-exp", | |
generation_config=generation_config, | |
system_instruction=SYSTEM_INSTRUCTION, | |
) | |
def analyze_fish_quality(image): | |
"""Analyze the fish quality 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 the instruction | |
response = chat.send_message([ | |
image_file, | |
"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): " | |
]) | |
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_fish_quality, | |
inputs=gr.Image(type="filepath", label="Upload Fish Image"), | |
outputs=gr.Textbox(label="Quality Analysis Result", lines=4), | |
title="Fish Quality Analyzer", | |
description="""Upload an image of a fish to analyze its quality. The system will evaluate: | |
- Overall quality (Good/Average/Bad) | |
- Quality score (0-10) | |
The analysis is based on various factors including eyes, gills, skin appearance, color, and overall condition.""", | |
examples=example_images, | |
cache_examples=True | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
iface.launch() |