Update app.py
Browse files
app.py
CHANGED
@@ -210,17 +210,17 @@ def analyze_voice(audio_file):
|
|
210 |
file_hash = compute_file_hash(audio_file)
|
211 |
logger.info(f"Processing audio, Hash: {file_hash}")
|
212 |
|
213 |
-
audio,
|
214 |
-
logger.info(f"Audio loaded: shape={audio.shape}, SR=
|
215 |
|
216 |
-
transcription = transcribe_audio(audio_file)
|
217 |
if "Error" in transcription:
|
218 |
logger.error(f"Transcription error: {transcription}")
|
219 |
return transcription
|
220 |
|
221 |
-
if any(keyword in transcription.lower() for keyword in ["medicine", "treatment"])
|
222 |
logger.warning("Medication query detected")
|
223 |
-
return
|
224 |
|
225 |
prediction, score = analyze_symptoms(transcription)
|
226 |
if "Error" in prediction:
|
@@ -228,11 +228,11 @@ def analyze_voice(audio_file):
|
|
228 |
return prediction
|
229 |
|
230 |
feedback = (
|
231 |
-
|
232 |
if prediction == "No health condition detected"
|
233 |
-
else
|
234 |
)
|
235 |
-
logger.info(f"Feedback: {feedback}, Transcription: {transcription}, Prediction: {prediction}, Score: {score:.
|
236 |
|
237 |
try:
|
238 |
os.remove(audio_file)
|
@@ -243,21 +243,21 @@ def analyze_voice(audio_file):
|
|
243 |
return feedback
|
244 |
except Exception as e:
|
245 |
logger.error(f"Voice analysis failed: {str(e)}")
|
246 |
-
return "Error: {str(e)}"
|
247 |
|
248 |
def test_with_sample_audio():
|
249 |
"""Test with sample or synthetic audio."""
|
250 |
temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
|
251 |
if not ensure_writable_dir(temp_dir):
|
252 |
-
logger.error(f"
|
253 |
-
return f"Error:
|
254 |
|
255 |
sample_audio_path = os.path.join(temp_dir, "sample.wav")
|
256 |
if not os.path.exists(sample_audio_path):
|
257 |
logger.warning("Sample audio not found; generating synthetic audio")
|
258 |
sr = 16000
|
259 |
t = np.linspace(0, 2, 2 * sr)
|
260 |
-
freq_mod = 440 + 10 * np.sin(2 * np.pi * 0.5 *
|
261 |
amplitude_mod = 0.5 + 0.1 * np.sin(2 * np.pi * 0.3 * t)
|
262 |
noise = 0.01 * np.random.normal(0, 1, len(t))
|
263 |
dummy_audio = amplitude_mod * np.sin(2 * np.pi * freq_mod * t) + noise
|
@@ -267,9 +267,9 @@ def test_with_sample_audio():
|
|
267 |
logger.info(f"Generated synthetic audio: {sample_audio_path}")
|
268 |
except Exception as e:
|
269 |
logger.error(f"Failed to write synthetic audio: {str(e)}")
|
270 |
-
return f"Error: Failed to generate synthetic audio: {str(e)}"
|
271 |
|
272 |
-
if not os.path.exists(sample_audio_path)
|
273 |
logger.error(f"Synthetic audio not created: {sample_audio_path}")
|
274 |
return f"Error: Synthetic audio not created: {sample_audio_path}"
|
275 |
|
|
|
210 |
file_hash = compute_file_hash(audio_file)
|
211 |
logger.info(f"Processing audio, Hash: {file_hash}")
|
212 |
|
213 |
+
audio, sr = librosa.load(audio_file, sr=16000)
|
214 |
+
logger.info(f"Audio loaded: shape={audio.shape}, SR={sr}, Duration={len(audio)/sr:.2f}s")
|
215 |
|
216 |
+
transcription = transcribe_audio(audio_file)
|
217 |
if "Error" in transcription:
|
218 |
logger.error(f"Transcription error: {transcription}")
|
219 |
return transcription
|
220 |
|
221 |
+
if any(keyword in transcription.lower() for keyword in ["medicine", "treatment"]):
|
222 |
logger.warning("Medication query detected")
|
223 |
+
return "Error: This tool does not provide medication advice"
|
224 |
|
225 |
prediction, score = analyze_symptoms(transcription)
|
226 |
if "Error" in prediction:
|
|
|
228 |
return prediction
|
229 |
|
230 |
feedback = (
|
231 |
+
"No health condition detected, consult a doctor if symptoms persist."
|
232 |
if prediction == "No health condition detected"
|
233 |
+
else f"Possible {prediction.lower()} detected, consult a doctor."
|
234 |
)
|
235 |
+
logger.info(f"Feedback: {feedback}, Transcription: {transcription}, Prediction: {prediction}, Score: {score:.4f}")
|
236 |
|
237 |
try:
|
238 |
os.remove(audio_file)
|
|
|
243 |
return feedback
|
244 |
except Exception as e:
|
245 |
logger.error(f"Voice analysis failed: {str(e)}")
|
246 |
+
return f"Error: {str(e)}"
|
247 |
|
248 |
def test_with_sample_audio():
|
249 |
"""Test with sample or synthetic audio."""
|
250 |
temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
|
251 |
if not ensure_writable_dir(temp_dir):
|
252 |
+
logger.error(f"Temp directory {temp_dir} not writable")
|
253 |
+
return f"Error: Temp directory {temp_dir} not writable"
|
254 |
|
255 |
sample_audio_path = os.path.join(temp_dir, "sample.wav")
|
256 |
if not os.path.exists(sample_audio_path):
|
257 |
logger.warning("Sample audio not found; generating synthetic audio")
|
258 |
sr = 16000
|
259 |
t = np.linspace(0, 2, 2 * sr)
|
260 |
+
freq_mod = 440 + 10 * np.sin(2 * np.pi * 0.5 * t)
|
261 |
amplitude_mod = 0.5 + 0.1 * np.sin(2 * np.pi * 0.3 * t)
|
262 |
noise = 0.01 * np.random.normal(0, 1, len(t))
|
263 |
dummy_audio = amplitude_mod * np.sin(2 * np.pi * freq_mod * t) + noise
|
|
|
267 |
logger.info(f"Generated synthetic audio: {sample_audio_path}")
|
268 |
except Exception as e:
|
269 |
logger.error(f"Failed to write synthetic audio: {str(e)}")
|
270 |
+
return f"Error: Failed to generate synthetic audio: {str(e)}"
|
271 |
|
272 |
+
if not os.path.exists(sample_audio_path):
|
273 |
logger.error(f"Synthetic audio not created: {sample_audio_path}")
|
274 |
return f"Error: Synthetic audio not created: {sample_audio_path}"
|
275 |
|