rknl commited on
Commit
8071fb2
·
verified ·
1 Parent(s): d8e6be1

Update app.py

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