Xalphinions commited on
Commit
83c2e18
Β·
verified Β·
1 Parent(s): a7e0ab1

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -35
app.py CHANGED
@@ -81,8 +81,8 @@ from preprocess import process_image_data
81
 
82
  # Using the decorator directly on the function definition
83
  @spaces.GPU
84
- def predict_ripeness(audio, image, model_path):
85
- """Function with GPU acceleration to predict watermelon ripeness"""
86
  try:
87
  # Now check CUDA availability inside the GPU-decorated function
88
  if torch.cuda.is_available():
@@ -192,38 +192,38 @@ def predict_ripeness(audio, image, model_path):
192
  print(f"\033[92mDEBUG\033[0m: Running inference on device: {device}")
193
  if mfcc is not None and processed_image is not None:
194
  with torch.no_grad():
195
- ripeness = model(mfcc, processed_image)
196
- print(f"\033[92mDEBUG\033[0m: Prediction successful: {ripeness.item()}")
197
  else:
198
  return "Error: Failed to process inputs. Please check the debug logs."
199
 
200
  # Format the result with a range display
201
- if ripeness is not None:
202
- ripeness_value = ripeness.item()
203
 
204
  # Create a header with the numerical result
205
- result = f"πŸ‰ Predicted Ripeness Score: {ripeness_value:.2f}/13 πŸ‰\n\n"
206
 
207
- # Add ripeness scale visualization
208
- result += "Ripeness Scale based on Sugar Content:\n"
209
  result += "──────────────────────────────────\n"
210
 
211
- # Create the scale display
212
  scale_ranges = [
213
- (0, 8, "Underripe"),
214
- (8, 9, "Slightly Ripe"),
215
- (9, 10, "Moderately Ripe"),
216
- (10, 11, "Ripe"),
217
- (11, 13, "Very Ripe")
218
  ]
219
 
220
  # Find which category the prediction falls into
221
  user_category = None
222
  for min_val, max_val, category_name in scale_ranges:
223
- if min_val <= ripeness_value < max_val:
224
  user_category = category_name
225
  break
226
- if ripeness_value >= scale_ranges[-1][0]: # Handle edge case
227
  user_category = scale_ranges[-1][2]
228
 
229
  # Display the scale with the user's result highlighted
@@ -235,21 +235,21 @@ def predict_ripeness(audio, image, model_path):
235
 
236
  result += "──────────────────────────────────\n\n"
237
 
238
- # Add assessment of the watermelon's ripeness
239
- if ripeness_value < 8:
240
- result += "Assessment: This watermelon is underripe. It may not have developed full flavor yet."
241
- elif ripeness_value < 9:
242
- result += "Assessment: This watermelon is slightly ripe. You might want to wait a few more days."
243
- elif ripeness_value < 10:
244
- result += "Assessment: This watermelon has moderate ripeness. It should have decent flavor."
245
- elif ripeness_value < 11:
246
- result += "Assessment: This watermelon is properly ripe! It should be sweet and juicy."
247
  else:
248
- result += "Assessment: This watermelon is perfectly ripe! Excellent choice for maximum sweetness and flavor."
249
 
250
  return result
251
  else:
252
- return "Error: Could not predict ripeness. Please try again with different inputs."
253
 
254
  except Exception as e:
255
  import traceback
@@ -265,25 +265,25 @@ def create_app(model_path):
265
  """Create and launch the Gradio interface"""
266
  # Define the prediction function with model path
267
  def predict_fn(audio, image):
268
- return predict_ripeness(audio, image, model_path)
269
 
270
  # Create Gradio interface
271
- with gr.Blocks(title="Watermelon Ripeness Predictor", theme=gr.themes.Soft()) as interface:
272
- gr.Markdown("# πŸ‰ Watermelon Ripeness Predictor")
273
  gr.Markdown("""
274
- This app predicts the ripeness of a watermelon based on its sound and appearance.
275
 
276
  ## Instructions:
277
  1. Upload or record an audio of tapping the watermelon
278
  2. Upload or capture an image of the watermelon
279
- 3. Click 'Predict' to get the ripeness estimation
280
  """)
281
 
282
  with gr.Row():
283
  with gr.Column():
284
  audio_input = gr.Audio(label="Upload or Record Audio", type="numpy")
285
  image_input = gr.Image(label="Upload or Capture Image")
286
- submit_btn = gr.Button("Predict Ripeness", variant="primary")
287
 
288
  with gr.Column():
289
  output = gr.Textbox(label="Prediction Results", lines=12)
@@ -298,6 +298,10 @@ def create_app(model_path):
298
  ## Tips for best results
299
  - For audio: Tap the watermelon with your knuckle and record the sound
300
  - For image: Take a clear photo of the whole watermelon in good lighting
 
 
 
 
301
  """)
302
 
303
  return interface
@@ -305,7 +309,7 @@ def create_app(model_path):
305
  if __name__ == "__main__":
306
  import argparse
307
 
308
- parser = argparse.ArgumentParser(description="Watermelon Ripeness Prediction App")
309
  parser.add_argument(
310
  "--model_path",
311
  type=str,
 
81
 
82
  # Using the decorator directly on the function definition
83
  @spaces.GPU
84
+ def predict_sugar_content(audio, image, model_path):
85
+ """Function with GPU acceleration to predict watermelon sugar content in Brix"""
86
  try:
87
  # Now check CUDA availability inside the GPU-decorated function
88
  if torch.cuda.is_available():
 
192
  print(f"\033[92mDEBUG\033[0m: Running inference on device: {device}")
193
  if mfcc is not None and processed_image is not None:
194
  with torch.no_grad():
195
+ brix_value = model(mfcc, processed_image)
196
+ print(f"\033[92mDEBUG\033[0m: Prediction successful: {brix_value.item()}")
197
  else:
198
  return "Error: Failed to process inputs. Please check the debug logs."
199
 
200
  # Format the result with a range display
201
+ if brix_value is not None:
202
+ brix_score = brix_value.item()
203
 
204
  # Create a header with the numerical result
205
+ result = f"πŸ‰ Predicted Sugar Content: {brix_score:.1f}Β° Brix πŸ‰\n\n"
206
 
207
+ # Add Brix scale visualization
208
+ result += "Sugar Content Scale (in Β°Brix):\n"
209
  result += "──────────────────────────────────\n"
210
 
211
+ # Create the scale display with Brix ranges
212
  scale_ranges = [
213
+ (0, 8, "Low Sugar (< 8Β° Brix)"),
214
+ (8, 9, "Mild Sweetness (8-9Β° Brix)"),
215
+ (9, 10, "Medium Sweetness (9-10Β° Brix)"),
216
+ (10, 11, "Sweet (10-11Β° Brix)"),
217
+ (11, 13, "Very Sweet (11-13Β° Brix)")
218
  ]
219
 
220
  # Find which category the prediction falls into
221
  user_category = None
222
  for min_val, max_val, category_name in scale_ranges:
223
+ if min_val <= brix_score < max_val:
224
  user_category = category_name
225
  break
226
+ if brix_score >= scale_ranges[-1][0]: # Handle edge case
227
  user_category = scale_ranges[-1][2]
228
 
229
  # Display the scale with the user's result highlighted
 
235
 
236
  result += "──────────────────────────────────\n\n"
237
 
238
+ # Add assessment of the watermelon's sugar content
239
+ if brix_score < 8:
240
+ result += "Assessment: This watermelon has low sugar content. It may taste bland or slightly bitter."
241
+ elif brix_score < 9:
242
+ result += "Assessment: This watermelon has mild sweetness. Acceptable flavor but not very sweet."
243
+ elif brix_score < 10:
244
+ result += "Assessment: This watermelon has moderate sugar content. It should have pleasant sweetness."
245
+ elif brix_score < 11:
246
+ result += "Assessment: This watermelon has good sugar content! It should be sweet and juicy."
247
  else:
248
+ result += "Assessment: This watermelon has excellent sugar content! Perfect choice for maximum sweetness and flavor."
249
 
250
  return result
251
  else:
252
+ return "Error: Could not predict sugar content. Please try again with different inputs."
253
 
254
  except Exception as e:
255
  import traceback
 
265
  """Create and launch the Gradio interface"""
266
  # Define the prediction function with model path
267
  def predict_fn(audio, image):
268
+ return predict_sugar_content(audio, image, model_path)
269
 
270
  # Create Gradio interface
271
+ with gr.Blocks(title="Watermelon Sugar Content Predictor", theme=gr.themes.Soft()) as interface:
272
+ gr.Markdown("# πŸ‰ Watermelon Sugar Content Predictor")
273
  gr.Markdown("""
274
+ This app predicts the sugar content (in Β°Brix) of a watermelon based on its sound and appearance.
275
 
276
  ## Instructions:
277
  1. Upload or record an audio of tapping the watermelon
278
  2. Upload or capture an image of the watermelon
279
+ 3. Click 'Predict' to get the sugar content estimation
280
  """)
281
 
282
  with gr.Row():
283
  with gr.Column():
284
  audio_input = gr.Audio(label="Upload or Record Audio", type="numpy")
285
  image_input = gr.Image(label="Upload or Capture Image")
286
+ submit_btn = gr.Button("Predict Sugar Content", variant="primary")
287
 
288
  with gr.Column():
289
  output = gr.Textbox(label="Prediction Results", lines=12)
 
298
  ## Tips for best results
299
  - For audio: Tap the watermelon with your knuckle and record the sound
300
  - For image: Take a clear photo of the whole watermelon in good lighting
301
+
302
+ ## About Brix Measurement
303
+ Brix (Β°Bx) is a measurement of sugar content in a solution. For watermelons, higher Brix values indicate sweeter fruit.
304
+ The average ripe watermelon has a Brix value between 9-11Β°.
305
  """)
306
 
307
  return interface
 
309
  if __name__ == "__main__":
310
  import argparse
311
 
312
+ parser = argparse.ArgumentParser(description="Watermelon Sugar Content Prediction App")
313
  parser.add_argument(
314
  "--model_path",
315
  type=str,