Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
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
|
85 |
-
"""Function with GPU acceleration to predict watermelon
|
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 |
-
|
196 |
-
print(f"\033[92mDEBUG\033[0m: Prediction successful: {
|
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
|
202 |
-
|
203 |
|
204 |
# Create a header with the numerical result
|
205 |
-
result = f"π Predicted
|
206 |
|
207 |
-
# Add
|
208 |
-
result += "
|
209 |
result += "ββββββββββββββββββββββββββββββββββ\n"
|
210 |
|
211 |
-
# Create the scale display
|
212 |
scale_ranges = [
|
213 |
-
(0, 8, "
|
214 |
-
(8, 9, "
|
215 |
-
(9, 10, "
|
216 |
-
(10, 11, "
|
217 |
-
(11, 13, "Very
|
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 <=
|
224 |
user_category = category_name
|
225 |
break
|
226 |
-
if
|
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
|
239 |
-
if
|
240 |
-
result += "Assessment: This watermelon
|
241 |
-
elif
|
242 |
-
result += "Assessment: This watermelon
|
243 |
-
elif
|
244 |
-
result += "Assessment: This watermelon has moderate
|
245 |
-
elif
|
246 |
-
result += "Assessment: This watermelon
|
247 |
else:
|
248 |
-
result += "Assessment: This watermelon
|
249 |
|
250 |
return result
|
251 |
else:
|
252 |
-
return "Error: Could not predict
|
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
|
269 |
|
270 |
# Create Gradio interface
|
271 |
-
with gr.Blocks(title="Watermelon
|
272 |
-
gr.Markdown("# π Watermelon
|
273 |
gr.Markdown("""
|
274 |
-
This app predicts the
|
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
|
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
|
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
|
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,
|