Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from transformers import pipeline
|
|
| 8 |
import soundfile as sf
|
| 9 |
import torch
|
| 10 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
|
|
|
| 11 |
|
| 12 |
# Initialize local models with retry logic
|
| 13 |
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
|
@@ -134,8 +135,21 @@ def analyze_symptoms(text):
|
|
| 134 |
except Exception as e:
|
| 135 |
return f"Error analyzing symptoms: {str(e)}", 0.0
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def analyze_voice(audio_file):
|
| 138 |
-
"""Analyze voice for health indicators."""
|
| 139 |
try:
|
| 140 |
# Ensure unique file name to avoid Gradio reuse
|
| 141 |
unique_path = f"/tmp/gradio/{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
|
@@ -153,19 +167,22 @@ def analyze_voice(audio_file):
|
|
| 153 |
# Transcribe audio
|
| 154 |
transcription = transcribe_audio(audio_file)
|
| 155 |
if "Error transcribing" in transcription:
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
# Check for medication-related queries
|
| 159 |
if "medicine" in transcription.lower() or "treatment" in transcription.lower():
|
| 160 |
feedback = "Error: This tool does not provide medication or treatment advice. Please describe symptoms only (e.g., 'I have a fever')."
|
| 161 |
feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', File Hash = {file_hash}"
|
| 162 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
# Analyze symptoms
|
| 166 |
prediction, score = analyze_symptoms(transcription)
|
| 167 |
if "Error analyzing" in prediction:
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
# Generate feedback
|
| 171 |
if prediction == "No health condition predicted":
|
|
@@ -176,6 +193,9 @@ def analyze_voice(audio_file):
|
|
| 176 |
feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', Prediction = {prediction}, Confidence = {score:.4f}, File Hash = {file_hash}"
|
| 177 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
# Clean up temporary audio file
|
| 180 |
try:
|
| 181 |
os.remove(audio_file)
|
|
@@ -183,9 +203,11 @@ def analyze_voice(audio_file):
|
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Failed to delete audio file: {str(e)}")
|
| 185 |
|
| 186 |
-
return feedback
|
| 187 |
except Exception as e:
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
|
| 190 |
def test_with_sample_audio():
|
| 191 |
"""Test the app with sample audio files."""
|
|
@@ -193,7 +215,8 @@ def test_with_sample_audio():
|
|
| 193 |
results = []
|
| 194 |
for sample in samples:
|
| 195 |
if os.path.exists(sample):
|
| 196 |
-
|
|
|
|
| 197 |
else:
|
| 198 |
results.append(f"Sample not found: {sample}")
|
| 199 |
return "\n".join(results)
|
|
@@ -202,7 +225,10 @@ def test_with_sample_audio():
|
|
| 202 |
iface = gr.Interface(
|
| 203 |
fn=analyze_voice,
|
| 204 |
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
|
| 205 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 206 |
title="Health Voice Analyzer",
|
| 207 |
description="Record or upload a voice sample describing symptoms (e.g., 'I have a fever') for preliminary health assessment. Supports English only. Use clear audio (WAV, 16kHz). Do not ask for medication or treatment advice."
|
| 208 |
)
|
|
|
|
| 8 |
import soundfile as sf
|
| 9 |
import torch
|
| 10 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
| 11 |
+
from gtts import gTTS
|
| 12 |
|
| 13 |
# Initialize local models with retry logic
|
| 14 |
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
return f"Error analyzing symptoms: {str(e)}", 0.0
|
| 137 |
|
| 138 |
+
def generate_voice_feedback(text):
|
| 139 |
+
"""Generate voice feedback from text using gTTS."""
|
| 140 |
+
try:
|
| 141 |
+
# Remove debug info and disclaimer for cleaner voice output
|
| 142 |
+
clean_text = text.split("\n\n**Debug Info**")[0]
|
| 143 |
+
tts = gTTS(text=clean_text, lang='en')
|
| 144 |
+
output_file = f"/tmp/feedback_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.mp3"
|
| 145 |
+
tts.save(output_file)
|
| 146 |
+
return output_file
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Error generating voice feedback: {str(e)}")
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
def analyze_voice(audio_file):
|
| 152 |
+
"""Analyze voice for health indicators and provide text and voice feedback."""
|
| 153 |
try:
|
| 154 |
# Ensure unique file name to avoid Gradio reuse
|
| 155 |
unique_path = f"/tmp/gradio/{datetime.now().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
|
|
|
|
| 167 |
# Transcribe audio
|
| 168 |
transcription = transcribe_audio(audio_file)
|
| 169 |
if "Error transcribing" in transcription:
|
| 170 |
+
voice_file = generate_voice_feedback(transcription)
|
| 171 |
+
return transcription, voice_file
|
| 172 |
|
| 173 |
# Check for medication-related queries
|
| 174 |
if "medicine" in transcription.lower() or "treatment" in transcription.lower():
|
| 175 |
feedback = "Error: This tool does not provide medication or treatment advice. Please describe symptoms only (e.g., 'I have a fever')."
|
| 176 |
feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', File Hash = {file_hash}"
|
| 177 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
| 178 |
+
voice_file = generate_voice_feedback(feedback)
|
| 179 |
+
return feedback, voice_file
|
| 180 |
|
| 181 |
# Analyze symptoms
|
| 182 |
prediction, score = analyze_symptoms(transcription)
|
| 183 |
if "Error analyzing" in prediction:
|
| 184 |
+
voice_file = generate_voice_feedback(prediction)
|
| 185 |
+
return prediction, voice_file
|
| 186 |
|
| 187 |
# Generate feedback
|
| 188 |
if prediction == "No health condition predicted":
|
|
|
|
| 193 |
feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', Prediction = {prediction}, Confidence = {score:.4f}, File Hash = {file_hash}"
|
| 194 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
| 195 |
|
| 196 |
+
# Generate voice feedback
|
| 197 |
+
voice_file = generate_voice_feedback(feedback)
|
| 198 |
+
|
| 199 |
# Clean up temporary audio file
|
| 200 |
try:
|
| 201 |
os.remove(audio_file)
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
print(f"Failed to delete audio file: {str(e)}")
|
| 205 |
|
| 206 |
+
return feedback, voice_file
|
| 207 |
except Exception as e:
|
| 208 |
+
feedback = f"Error processing audio: {str(e)}"
|
| 209 |
+
voice_file = generate_voice_feedback(feedback)
|
| 210 |
+
return feedback, voice_file
|
| 211 |
|
| 212 |
def test_with_sample_audio():
|
| 213 |
"""Test the app with sample audio files."""
|
|
|
|
| 215 |
results = []
|
| 216 |
for sample in samples:
|
| 217 |
if os.path.exists(sample):
|
| 218 |
+
text, voice = analyze_voice(sample)
|
| 219 |
+
results.append(f"Text: {text}\nVoice: {voice}")
|
| 220 |
else:
|
| 221 |
results.append(f"Sample not found: {sample}")
|
| 222 |
return "\n".join(results)
|
|
|
|
| 225 |
iface = gr.Interface(
|
| 226 |
fn=analyze_voice,
|
| 227 |
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
|
| 228 |
+
outputs=[
|
| 229 |
+
gr.Textbox(label="Health Assessment Feedback"),
|
| 230 |
+
gr.Audio(label="Voice Feedback", type="filepath")
|
| 231 |
+
],
|
| 232 |
title="Health Voice Analyzer",
|
| 233 |
description="Record or upload a voice sample describing symptoms (e.g., 'I have a fever') for preliminary health assessment. Supports English only. Use clear audio (WAV, 16kHz). Do not ask for medication or treatment advice."
|
| 234 |
)
|