Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import numpy as np
|
|
| 7 |
# Configure Gemini API
|
| 8 |
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
|
| 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,
|
|
@@ -48,53 +47,11 @@ model = genai.GenerativeModel(
|
|
| 48 |
system_instruction=SYSTEM_INSTRUCTION,
|
| 49 |
)
|
| 50 |
|
| 51 |
-
def create_quality_plot(quality, score):
|
| 52 |
-
"""Create a visualization of the fish quality score"""
|
| 53 |
-
plt.figure(figsize=(10, 4))
|
| 54 |
-
|
| 55 |
-
# Create bar chart
|
| 56 |
-
score = float(score) # Convert score to float
|
| 57 |
-
bar_color = 'red' if score <= 4 else ('yellow' if score <= 7 else 'green')
|
| 58 |
-
|
| 59 |
-
plt.bar(['Quality Score'], [score], color=bar_color)
|
| 60 |
-
plt.axhline(y=10, color='gray', linestyle='--', alpha=0.5)
|
| 61 |
-
|
| 62 |
-
# Customize plot
|
| 63 |
-
plt.ylim(0, 11)
|
| 64 |
-
plt.title(f'Fish Quality Assessment: {quality}', pad=20)
|
| 65 |
-
plt.ylabel('Score (0-10)')
|
| 66 |
-
|
| 67 |
-
# Add quality zones
|
| 68 |
-
plt.axhspan(0, 4, alpha=0.1, color='red', label='Bad (0-4)')
|
| 69 |
-
plt.axhspan(4, 7, alpha=0.1, color='yellow', label='Average (4-7)')
|
| 70 |
-
plt.axhspan(7, 10, alpha=0.1, color='green', label='Good (7-10)')
|
| 71 |
-
|
| 72 |
-
# Add legend
|
| 73 |
-
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
|
| 74 |
-
|
| 75 |
-
# Save plot to a temporary file
|
| 76 |
-
plt.tight_layout()
|
| 77 |
-
temp_plot = "temp_plot.png"
|
| 78 |
-
plt.savefig(temp_plot, bbox_inches='tight', dpi=300)
|
| 79 |
-
plt.close()
|
| 80 |
-
|
| 81 |
-
return temp_plot
|
| 82 |
-
|
| 83 |
-
def parse_response(response_text):
|
| 84 |
-
"""Parse the model's response to extract quality and score"""
|
| 85 |
-
try:
|
| 86 |
-
lines = response_text.strip().split('\n')
|
| 87 |
-
quality = lines[0].split(': ')[1].strip()
|
| 88 |
-
score = lines[1].split(': ')[1].strip().strip('()')
|
| 89 |
-
return quality, score
|
| 90 |
-
except:
|
| 91 |
-
return "Bad", "0"
|
| 92 |
-
|
| 93 |
def analyze_fish_quality(image):
|
| 94 |
"""Analyze the fish quality using Gemini model"""
|
| 95 |
try:
|
| 96 |
if image is None:
|
| 97 |
-
return "Please upload an image to analyze."
|
| 98 |
|
| 99 |
# Start a new chat session
|
| 100 |
chat = model.start_chat()
|
|
@@ -108,13 +65,9 @@ def analyze_fish_quality(image):
|
|
| 108 |
"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): "
|
| 109 |
])
|
| 110 |
|
| 111 |
-
|
| 112 |
-
quality, score = parse_response(response.text)
|
| 113 |
-
plot_path = create_quality_plot(quality, score)
|
| 114 |
-
|
| 115 |
-
return response.text, plot_path
|
| 116 |
except Exception as e:
|
| 117 |
-
return f"Error analyzing image: {str(e)}\nPlease make sure you've uploaded a valid image file."
|
| 118 |
|
| 119 |
# Get example images from the examples directory
|
| 120 |
examples_dir = os.path.join(os.path.dirname(__file__), "examples")
|
|
@@ -130,20 +83,13 @@ if os.path.exists(examples_dir):
|
|
| 130 |
iface = gr.Interface(
|
| 131 |
fn=analyze_fish_quality,
|
| 132 |
inputs=gr.Image(type="filepath", label="Upload Fish Image"),
|
| 133 |
-
outputs=
|
| 134 |
-
gr.Textbox(label="Quality Analysis Result", lines=4),
|
| 135 |
-
gr.Image(label="Quality Score Visualization")
|
| 136 |
-
],
|
| 137 |
title="Fish Quality Analyzer",
|
| 138 |
description="""Upload an image of a fish to analyze its quality. The system will evaluate:
|
| 139 |
- Overall quality (Good/Average/Bad)
|
| 140 |
- Quality score (0-10)
|
| 141 |
|
| 142 |
-
The analysis is based on various factors including eyes, gills, skin appearance, color, and overall condition.
|
| 143 |
-
The visualization shows the quality score with color-coded zones:
|
| 144 |
-
- Green: Good (7-10)
|
| 145 |
-
- Yellow: Average (4-7)
|
| 146 |
-
- Red: Bad (0-4)""",
|
| 147 |
examples=example_images,
|
| 148 |
cache_examples=True
|
| 149 |
)
|
|
|
|
| 7 |
# Configure Gemini API
|
| 8 |
GEMINI_API_KEY = os.environ["GEMINI_API_KEY"]
|
| 9 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
| 10 |
# Create the model with the same configuration as the sample
|
| 11 |
generation_config = {
|
| 12 |
"temperature": 0.2,
|
|
|
|
| 47 |
system_instruction=SYSTEM_INSTRUCTION,
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def analyze_fish_quality(image):
|
| 51 |
"""Analyze the fish quality using Gemini model"""
|
| 52 |
try:
|
| 53 |
if image is None:
|
| 54 |
+
return "Please upload an image to analyze."
|
| 55 |
|
| 56 |
# Start a new chat session
|
| 57 |
chat = model.start_chat()
|
|
|
|
| 65 |
"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): "
|
| 66 |
])
|
| 67 |
|
| 68 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
+
return f"Error analyzing image: {str(e)}\nPlease make sure you've uploaded a valid image file."
|
| 71 |
|
| 72 |
# Get example images from the examples directory
|
| 73 |
examples_dir = os.path.join(os.path.dirname(__file__), "examples")
|
|
|
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=analyze_fish_quality,
|
| 85 |
inputs=gr.Image(type="filepath", label="Upload Fish Image"),
|
| 86 |
+
outputs=gr.Textbox(label="Quality Analysis Result", lines=4),
|
|
|
|
|
|
|
|
|
|
| 87 |
title="Fish Quality Analyzer",
|
| 88 |
description="""Upload an image of a fish to analyze its quality. The system will evaluate:
|
| 89 |
- Overall quality (Good/Average/Bad)
|
| 90 |
- Quality score (0-10)
|
| 91 |
|
| 92 |
+
The analysis is based on various factors including eyes, gills, skin appearance, color, and overall condition.""",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
examples=example_images,
|
| 94 |
cache_examples=True
|
| 95 |
)
|