Update app.py
Browse files
app.py
CHANGED
@@ -160,16 +160,20 @@ def analyze_symptoms(text):
|
|
160 |
return "Error: No valid transcription", 0.0
|
161 |
with torch.no_grad():
|
162 |
result = symptom_classifier(text)
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
173 |
except Exception as e:
|
174 |
logger.error(f"Symptom analysis failed: {str(e)}")
|
175 |
return f"Error: {str(e)}", 0.0
|
@@ -182,13 +186,11 @@ def analyze_voice(audio_file):
|
|
182 |
logger.error(f"Audio file not found: {audio_file}")
|
183 |
return "Error: Audio file not found"
|
184 |
|
185 |
-
# Ensure temp directory is writable
|
186 |
temp_dir = os.path.join(tempfile.gettempdir(), "gradio")
|
187 |
if not ensure_writable_dir(temp_dir):
|
188 |
logger.error(f"Temp directory {temp_dir} not writable")
|
189 |
return f"Error: Temp directory {temp_dir} not writable"
|
190 |
|
191 |
-
# Rename file to unique path
|
192 |
unique_path = os.path.join(
|
193 |
temp_dir,
|
194 |
f"gradio_{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
@@ -198,8 +200,8 @@ def analyze_voice(audio_file):
|
|
198 |
audio_file = unique_path
|
199 |
logger.debug(f"Moved to: {audio_file}")
|
200 |
except Exception as e:
|
201 |
-
logger.error(f"Failed to
|
202 |
-
return f"Error: Failed to
|
203 |
|
204 |
file_hash = compute_file_hash(audio_file)
|
205 |
logger.info(f"Processing audio, Hash: {file_hash}")
|
@@ -241,12 +243,12 @@ def analyze_voice(audio_file):
|
|
241 |
|
242 |
def test_with_sample_audio():
|
243 |
"""Test with sample or synthetic audio."""
|
244 |
-
sample_audio_path = os.path.join("audio_samples", "sample.wav")
|
245 |
temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
|
246 |
if not ensure_writable_dir(temp_dir):
|
247 |
logger.error(f"Temp directory {temp_dir} not writable")
|
248 |
return f"Error: Temp directory {temp_dir} not writable"
|
249 |
|
|
|
250 |
if not os.path.exists(sample_audio_path):
|
251 |
logger.warning("Sample audio not found; generating synthetic audio")
|
252 |
sr = 16000
|
@@ -263,7 +265,6 @@ def test_with_sample_audio():
|
|
263 |
logger.error(f"Failed to write synthetic audio: {str(e)}")
|
264 |
return f"Error: Failed to generate synthetic audio: {str(e)}"
|
265 |
|
266 |
-
# Verify file existence
|
267 |
if not os.path.exists(sample_audio_path):
|
268 |
logger.error(f"Synthetic audio not created: {sample_audio_path}")
|
269 |
return f"Error: Synthetic audio not created: {sample_audio_path}"
|
|
|
160 |
return "Error: No valid transcription", 0.0
|
161 |
with torch.no_grad():
|
162 |
result = symptom_classifier(text)
|
163 |
+
logger.debug(f"Model output: {result}")
|
164 |
+
if not result or not isinstance(result, list) or len(result) == 0:
|
165 |
+
logger.warning("Invalid model output: empty or not a list")
|
166 |
+
return "No health condition detected", 0.0
|
167 |
+
if not isinstance(result[0], dict) or "label" not in result[0] or "score" not in result[0]:
|
168 |
+
logger.warning(f"Invalid result structure: {result[0]}")
|
169 |
+
return "No health condition detected", 0.0
|
170 |
+
prediction = result[0]["label"]
|
171 |
+
score = result[0]["score"]
|
172 |
+
if is_fallback_model:
|
173 |
+
logger.warning("Using fallback model")
|
174 |
+
prediction = f"{prediction} (fallback)"
|
175 |
+
logger.info(f"Prediction: {prediction}, Score: {score:.4f}")
|
176 |
+
return prediction, score
|
177 |
except Exception as e:
|
178 |
logger.error(f"Symptom analysis failed: {str(e)}")
|
179 |
return f"Error: {str(e)}", 0.0
|
|
|
186 |
logger.error(f"Audio file not found: {audio_file}")
|
187 |
return "Error: Audio file not found"
|
188 |
|
|
|
189 |
temp_dir = os.path.join(tempfile.gettempdir(), "gradio")
|
190 |
if not ensure_writable_dir(temp_dir):
|
191 |
logger.error(f"Temp directory {temp_dir} not writable")
|
192 |
return f"Error: Temp directory {temp_dir} not writable"
|
193 |
|
|
|
194 |
unique_path = os.path.join(
|
195 |
temp_dir,
|
196 |
f"gradio_{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
|
|
200 |
audio_file = unique_path
|
201 |
logger.debug(f"Moved to: {audio_file}")
|
202 |
except Exception as e:
|
203 |
+
logger.error(f"Failed to move audio file: {str(e)}")
|
204 |
+
return f"Error: Failed to move audio file: {str(e)}"
|
205 |
|
206 |
file_hash = compute_file_hash(audio_file)
|
207 |
logger.info(f"Processing audio, Hash: {file_hash}")
|
|
|
243 |
|
244 |
def test_with_sample_audio():
|
245 |
"""Test with sample or synthetic audio."""
|
|
|
246 |
temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
|
247 |
if not ensure_writable_dir(temp_dir):
|
248 |
logger.error(f"Temp directory {temp_dir} not writable")
|
249 |
return f"Error: Temp directory {temp_dir} not writable"
|
250 |
|
251 |
+
sample_audio_path = os.path.join(temp_dir, "sample.wav")
|
252 |
if not os.path.exists(sample_audio_path):
|
253 |
logger.warning("Sample audio not found; generating synthetic audio")
|
254 |
sr = 16000
|
|
|
265 |
logger.error(f"Failed to write synthetic audio: {str(e)}")
|
266 |
return f"Error: Failed to generate synthetic audio: {str(e)}"
|
267 |
|
|
|
268 |
if not os.path.exists(sample_audio_path):
|
269 |
logger.error(f"Synthetic audio not created: {sample_audio_path}")
|
270 |
return f"Error: Synthetic audio not created: {sample_audio_path}"
|