Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
@@ -10,8 +15,7 @@ from typing import Optional, Dict, Any
|
|
10 |
import json
|
11 |
import io
|
12 |
import os
|
13 |
-
import speech_recognition as sr
|
14 |
-
from datasets import load_dataset # Moved to top
|
15 |
|
16 |
# Configuration (unchanged)
|
17 |
INSULIN_TYPES = {
|
@@ -63,21 +67,19 @@ def load_food_data():
|
|
63 |
print(f"Error loading food data: {e}")
|
64 |
return pd.DataFrame()
|
65 |
|
66 |
-
# Load model and processor (error handling)
|
67 |
-
model_loaded = False
|
68 |
-
processor = None
|
69 |
-
model = None
|
70 |
try:
|
71 |
processor = AutoImageProcessor.from_pretrained("rajistics/finetuned-indian-food")
|
72 |
model = AutoModelForImageClassification.from_pretrained("rajistics/finetuned-indian-food")
|
73 |
model_loaded = True
|
74 |
-
print("Model loaded successfully!") # Add confirmation
|
75 |
except Exception as e:
|
76 |
print(f"Model Load Error: {e}")
|
|
|
|
|
|
|
77 |
|
78 |
def classify_food(image):
|
79 |
-
if not model_loaded or image is None
|
80 |
-
return "
|
81 |
try:
|
82 |
inputs = processor(images=image, return_tensors="pt")
|
83 |
with torch.no_grad():
|
@@ -172,7 +174,7 @@ def insulin_activity(t: float, insulin_type: str, bolus_dose: float, bolus_durat
|
|
172 |
return 0
|
173 |
|
174 |
def calculate_active_insulin(insulin_history: list, current_time: float) -> float:
|
175 |
-
return sum(insulin_activity(current_time - dose_time,
|
176 |
for dose_time, dose_amount, insulin_type, bolus_duration in insulin_history)
|
177 |
|
178 |
def calculate_insulin_needs(carbs: float, glucose_current: float, glucose_target: float,
|
@@ -243,20 +245,17 @@ def create_detailed_report(nutrition_info: Dict[str, Any], insulin_info: Dict[st
|
|
243 |
|
244 |
return glucose_meal_details, insulin_details, basal_details
|
245 |
|
246 |
-
def speech_to_text(audio_file):
|
247 |
recognizer = sr.Recognizer()
|
248 |
try:
|
249 |
with sr.AudioFile(audio_file) as source:
|
250 |
audio = recognizer.record(source)
|
251 |
try:
|
252 |
-
|
253 |
-
text = recognizer.recognize_sphinx(audio) # Using Sphinx
|
254 |
-
|
255 |
return HINDI_TO_ENGLISH.get(text.lower(), text)
|
256 |
except sr.UnknownValueError:
|
257 |
-
|
258 |
-
|
259 |
-
return f"Could not request results from Sphinx service; {e}"
|
260 |
except Exception as e:
|
261 |
return f"Error processing audio: {e}"
|
262 |
|
@@ -370,4 +369,4 @@ with gr.Blocks(title="Type 1 Diabetes Management Dashboard") as app:
|
|
370 |
outputs=[glucose_meal_output, insulin_output, basal_output, bolus_output, plot_output]
|
371 |
)
|
372 |
|
373 |
-
app.launch()
|
|
|
1 |
+
!apt-get update
|
2 |
+
!apt-get install -y portaudio19-dev
|
3 |
+
!pip install pyaudio gradio datasets transformers torch pandas numpy matplotlib Pillow
|
4 |
+
|
5 |
+
from datasets import load_dataset
|
6 |
import gradio as gr
|
7 |
import numpy as np
|
8 |
import pandas as pd
|
|
|
15 |
import json
|
16 |
import io
|
17 |
import os
|
18 |
+
import speech_recognition as sr
|
|
|
19 |
|
20 |
# Configuration (unchanged)
|
21 |
INSULIN_TYPES = {
|
|
|
67 |
print(f"Error loading food data: {e}")
|
68 |
return pd.DataFrame()
|
69 |
|
|
|
|
|
|
|
|
|
70 |
try:
|
71 |
processor = AutoImageProcessor.from_pretrained("rajistics/finetuned-indian-food")
|
72 |
model = AutoModelForImageClassification.from_pretrained("rajistics/finetuned-indian-food")
|
73 |
model_loaded = True
|
|
|
74 |
except Exception as e:
|
75 |
print(f"Model Load Error: {e}")
|
76 |
+
model_loaded = False
|
77 |
+
processor = None
|
78 |
+
model = None
|
79 |
|
80 |
def classify_food(image):
|
81 |
+
if not model_loaded or image is None:
|
82 |
+
return "unknown"
|
83 |
try:
|
84 |
inputs = processor(images=image, return_tensors="pt")
|
85 |
with torch.no_grad():
|
|
|
174 |
return 0
|
175 |
|
176 |
def calculate_active_insulin(insulin_history: list, current_time: float) -> float:
|
177 |
+
return sum(insulin_activity(current_time - dose_time, insulin_type, dose_amount, bolus_duration)
|
178 |
for dose_time, dose_amount, insulin_type, bolus_duration in insulin_history)
|
179 |
|
180 |
def calculate_insulin_needs(carbs: float, glucose_current: float, glucose_target: float,
|
|
|
245 |
|
246 |
return glucose_meal_details, insulin_details, basal_details
|
247 |
|
248 |
+
def speech_to_text(audio_file):
|
249 |
recognizer = sr.Recognizer()
|
250 |
try:
|
251 |
with sr.AudioFile(audio_file) as source:
|
252 |
audio = recognizer.record(source)
|
253 |
try:
|
254 |
+
text = recognizer.recognize_google(audio, language="hi-IN")
|
|
|
|
|
255 |
return HINDI_TO_ENGLISH.get(text.lower(), text)
|
256 |
except sr.UnknownValueError:
|
257 |
+
text = recognizer.recognize_google(audio, language="en-US")
|
258 |
+
return text
|
|
|
259 |
except Exception as e:
|
260 |
return f"Error processing audio: {e}"
|
261 |
|
|
|
369 |
outputs=[glucose_meal_output, insulin_output, basal_output, bolus_output, plot_output]
|
370 |
)
|
371 |
|
372 |
+
app.launch(share=True) # Share=True generates a public URL
|