Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ import google.generativeai as genai
|
|
6 |
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
|
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": 0.2,
|
@@ -15,31 +17,53 @@ generation_config = {
|
|
15 |
"response_mime_type": "text/plain",
|
16 |
}
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
model = genai.GenerativeModel(
|
19 |
-
model_name="gemini-
|
20 |
generation_config=generation_config,
|
21 |
-
system_instruction=
|
22 |
)
|
23 |
|
24 |
-
def
|
25 |
-
"""Analyze the
|
26 |
try:
|
27 |
if image is None:
|
28 |
return "Please upload an image to analyze."
|
29 |
|
30 |
-
# Use default instruction if none provided
|
31 |
-
if not instruction:
|
32 |
-
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): ",
|
33 |
# Start a new chat session
|
34 |
chat = model.start_chat()
|
35 |
|
36 |
# Upload and analyze the image
|
37 |
image_file = genai.upload_file(image)
|
38 |
|
39 |
-
# Send the image with the
|
40 |
response = chat.send_message([
|
41 |
image_file,
|
42 |
-
|
43 |
])
|
44 |
|
45 |
return response.text
|
@@ -51,25 +75,22 @@ examples_dir = os.path.join(os.path.dirname(__file__), "examples")
|
|
51 |
example_images = []
|
52 |
if os.path.exists(examples_dir):
|
53 |
example_images = [
|
54 |
-
|
55 |
for f in os.listdir(examples_dir)
|
56 |
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
|
57 |
]
|
58 |
|
59 |
# Create Gradio interface
|
60 |
iface = gr.Interface(
|
61 |
-
fn=
|
62 |
-
inputs=
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
outputs=gr.Textbox(label="Analysis Result", lines=10),
|
71 |
-
title="Fish Quality Detection",
|
72 |
-
description="Upload an image of a fish and provide instructions for analysis. The model will detect and describe objects based on your instructions.",
|
73 |
examples=example_images,
|
74 |
cache_examples=True
|
75 |
)
|
|
|
6 |
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
|
7 |
genai.configure(api_key=GEMINI_API_KEY)
|
8 |
|
9 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
10 |
+
|
11 |
# Create the model with the same configuration as the sample
|
12 |
generation_config = {
|
13 |
"temperature": 0.2,
|
|
|
17 |
"response_mime_type": "text/plain",
|
18 |
}
|
19 |
|
20 |
+
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.
|
21 |
+
|
22 |
+
Primary Key Indicators for Fish Quality Assessment
|
23 |
+
1. Eyes
|
24 |
+
Freshness Indicator: The eyes should be clear, bright, and bulging with a metallic sheen.
|
25 |
+
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.
|
26 |
+
2. Gills
|
27 |
+
Freshness Indicator: Gills should be bright red or pink, indicating fresh blood and optimal freshness.
|
28 |
+
Deterioration Signs: Gills turning brown, grey, or green suggest that the fish is past its prime and potentially spoiled.
|
29 |
+
3. Skin Appearance
|
30 |
+
Freshness Indicator: The skin should have a shiny, mother-of-pearl sheen and be free of blemishes or discoloration.
|
31 |
+
Deterioration Signs: Dullness or yellowing of the skin indicates degradation and loss of freshness.
|
32 |
+
4. Odor
|
33 |
+
Freshness Indicator: Fresh fish typically has a neutral smell with slight hints of seaweed.
|
34 |
+
Deterioration Signs: A sour, off, or overly strong odor suggests spoilage.
|
35 |
+
5. Texture
|
36 |
+
Freshness Indicator: The flesh should be firm to the touch and spring back when pressed.
|
37 |
+
Deterioration Signs: Softness or mushiness indicates that the fish is no longer fresh.
|
38 |
+
6. Color of Flesh
|
39 |
+
Freshness Indicator: The flesh should have a vibrant color appropriate for the species (e.g., pink for salmon).
|
40 |
+
Deterioration Signs: Any discoloration or dullness in the flesh can indicate spoilage.
|
41 |
+
7. Overall Appearance
|
42 |
+
Freshness Indicator: The overall appearance should be appealing without any signs of drying out or excessive slime.
|
43 |
+
Deterioration Signs: Excessive slime or drying out can indicate that the fish is not fresh."""
|
44 |
+
|
45 |
model = genai.GenerativeModel(
|
46 |
+
model_name="gemini-2.0-flash-exp",
|
47 |
generation_config=generation_config,
|
48 |
+
system_instruction=SYSTEM_INSTRUCTION,
|
49 |
)
|
50 |
|
51 |
+
def analyze_fish_quality(image):
|
52 |
+
"""Analyze the fish quality using Gemini model"""
|
53 |
try:
|
54 |
if image is None:
|
55 |
return "Please upload an image to analyze."
|
56 |
|
|
|
|
|
|
|
57 |
# Start a new chat session
|
58 |
chat = model.start_chat()
|
59 |
|
60 |
# Upload and analyze the image
|
61 |
image_file = genai.upload_file(image)
|
62 |
|
63 |
+
# Send the image with the instruction
|
64 |
response = chat.send_message([
|
65 |
image_file,
|
66 |
+
"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): "
|
67 |
])
|
68 |
|
69 |
return response.text
|
|
|
75 |
example_images = []
|
76 |
if os.path.exists(examples_dir):
|
77 |
example_images = [
|
78 |
+
os.path.join(examples_dir, f)
|
79 |
for f in os.listdir(examples_dir)
|
80 |
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
|
81 |
]
|
82 |
|
83 |
# Create Gradio interface
|
84 |
iface = gr.Interface(
|
85 |
+
fn=analyze_fish_quality,
|
86 |
+
inputs=gr.Image(type="filepath", label="Upload Fish Image"),
|
87 |
+
outputs=gr.Textbox(label="Quality Analysis Result", lines=4),
|
88 |
+
title="Fish Quality Analyzer",
|
89 |
+
description="""Upload an image of a fish to analyze its quality. The system will evaluate:
|
90 |
+
- Overall quality (Good/Average/Bad)
|
91 |
+
- Quality score (0-10)
|
92 |
+
|
93 |
+
The analysis is based on various factors including eyes, gills, skin appearance, color, and overall condition.""",
|
|
|
|
|
|
|
94 |
examples=example_images,
|
95 |
cache_examples=True
|
96 |
)
|