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"""
|
86 |
try:
|
87 |
# Now check CUDA availability inside the GPU-decorated function
|
88 |
if torch.cuda.is_available():
|
@@ -192,28 +192,28 @@ def predict_sweetness(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
|
201 |
-
if
|
202 |
-
result = f"Predicted
|
203 |
|
204 |
# Add a qualitative description
|
205 |
-
if
|
206 |
-
result += "\n\nThis watermelon is not very
|
207 |
-
elif
|
208 |
-
result += "\n\nThis watermelon has moderate
|
209 |
-
elif
|
210 |
-
result += "\n\nThis watermelon is
|
211 |
else:
|
212 |
-
result += "\n\nThis watermelon is
|
213 |
|
214 |
return result
|
215 |
else:
|
216 |
-
return "Error: Could not predict
|
217 |
|
218 |
except Exception as e:
|
219 |
import traceback
|
@@ -229,25 +229,25 @@ def create_app(model_path):
|
|
229 |
"""Create and launch the Gradio interface"""
|
230 |
# Define the prediction function with model path
|
231 |
def predict_fn(audio, image):
|
232 |
-
return
|
233 |
|
234 |
# Create Gradio interface
|
235 |
-
with gr.Blocks(title="Watermelon
|
236 |
-
gr.Markdown("# 🍉 Watermelon
|
237 |
gr.Markdown("""
|
238 |
-
This app predicts the
|
239 |
|
240 |
## Instructions:
|
241 |
1. Upload or record an audio of tapping the watermelon
|
242 |
2. Upload or capture an image of the watermelon
|
243 |
-
3. Click 'Predict' to get the
|
244 |
""")
|
245 |
|
246 |
with gr.Row():
|
247 |
with gr.Column():
|
248 |
audio_input = gr.Audio(label="Upload or Record Audio", type="numpy")
|
249 |
image_input = gr.Image(label="Upload or Capture Image")
|
250 |
-
submit_btn = gr.Button("Predict
|
251 |
|
252 |
with gr.Column():
|
253 |
output = gr.Textbox(label="Prediction Results", lines=6)
|
@@ -265,7 +265,7 @@ def create_app(model_path):
|
|
265 |
- Audio analysis using MFCC features and LSTM neural network
|
266 |
- Image analysis using ResNet-50 convolutional neural network
|
267 |
|
268 |
-
The model was trained on a dataset of watermelons with known
|
269 |
|
270 |
## Tips for best results
|
271 |
- For audio: Tap the watermelon with your knuckle and record the sound
|
@@ -277,7 +277,7 @@ def create_app(model_path):
|
|
277 |
if __name__ == "__main__":
|
278 |
import argparse
|
279 |
|
280 |
-
parser = argparse.ArgumentParser(description="Watermelon
|
281 |
parser.add_argument(
|
282 |
"--model_path",
|
283 |
type=str,
|
|
|
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 |
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
|
201 |
+
if ripeness is not None:
|
202 |
+
result = f"Predicted Ripeness: {ripeness.item():.2f}/13"
|
203 |
|
204 |
# Add a qualitative description
|
205 |
+
if ripeness.item() < 9:
|
206 |
+
result += "\n\nThis watermelon is not very ripe. You might want to choose another one."
|
207 |
+
elif ripeness.item() < 10:
|
208 |
+
result += "\n\nThis watermelon has moderate ripeness."
|
209 |
+
elif ripeness.item() < 11:
|
210 |
+
result += "\n\nThis watermelon is ripe! A good choice."
|
211 |
else:
|
212 |
+
result += "\n\nThis watermelon is perfectly ripe! Excellent choice!"
|
213 |
|
214 |
return result
|
215 |
else:
|
216 |
+
return "Error: Could not predict ripeness. Please try again with different inputs."
|
217 |
|
218 |
except Exception as e:
|
219 |
import traceback
|
|
|
229 |
"""Create and launch the Gradio interface"""
|
230 |
# Define the prediction function with model path
|
231 |
def predict_fn(audio, image):
|
232 |
+
return predict_ripeness(audio, image, model_path)
|
233 |
|
234 |
# Create Gradio interface
|
235 |
+
with gr.Blocks(title="Watermelon Ripeness Predictor", theme=gr.themes.Soft()) as interface:
|
236 |
+
gr.Markdown("# 🍉 Watermelon Ripeness Predictor")
|
237 |
gr.Markdown("""
|
238 |
+
This app predicts the ripeness of a watermelon based on its sound and appearance.
|
239 |
|
240 |
## Instructions:
|
241 |
1. Upload or record an audio of tapping the watermelon
|
242 |
2. Upload or capture an image of the watermelon
|
243 |
+
3. Click 'Predict' to get the ripeness estimation
|
244 |
""")
|
245 |
|
246 |
with gr.Row():
|
247 |
with gr.Column():
|
248 |
audio_input = gr.Audio(label="Upload or Record Audio", type="numpy")
|
249 |
image_input = gr.Image(label="Upload or Capture Image")
|
250 |
+
submit_btn = gr.Button("Predict Ripeness", variant="primary")
|
251 |
|
252 |
with gr.Column():
|
253 |
output = gr.Textbox(label="Prediction Results", lines=6)
|
|
|
265 |
- Audio analysis using MFCC features and LSTM neural network
|
266 |
- Image analysis using ResNet-50 convolutional neural network
|
267 |
|
268 |
+
The model was trained on a dataset of watermelons with known ripeness values.
|
269 |
|
270 |
## Tips for best results
|
271 |
- For audio: Tap the watermelon with your knuckle and record the sound
|
|
|
277 |
if __name__ == "__main__":
|
278 |
import argparse
|
279 |
|
280 |
+
parser = argparse.ArgumentParser(description="Watermelon Ripeness Prediction App")
|
281 |
parser.add_argument(
|
282 |
"--model_path",
|
283 |
type=str,
|