Update app.py
Browse files
app.py
CHANGED
|
@@ -7,23 +7,53 @@ from datetime import datetime
|
|
| 7 |
from transformers import pipeline
|
| 8 |
import soundfile as sf
|
| 9 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Initialize local models
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en", device=-1) # CPU; use device=0 for GPU
|
| 15 |
-
print("Whisper model loaded successfully.")
|
| 16 |
except Exception as e:
|
| 17 |
-
print(f"
|
| 18 |
-
whisper = None
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
symptom_classifier = pipeline("text-classification", model="abhirajeshbhai/symptom-2-disease-net", device=-1) # CPU
|
| 23 |
-
print("Symptom-2-Disease model loaded successfully.")
|
| 24 |
except Exception as e:
|
| 25 |
-
print(f"
|
| 26 |
-
symptom_classifier = None
|
| 27 |
|
| 28 |
def compute_file_hash(file_path):
|
| 29 |
"""Compute MD5 hash of a file to check uniqueness."""
|
|
@@ -36,16 +66,22 @@ def compute_file_hash(file_path):
|
|
| 36 |
def transcribe_audio(audio_file):
|
| 37 |
"""Transcribe audio using local Whisper model."""
|
| 38 |
if not whisper:
|
| 39 |
-
return "Error: Whisper model not loaded. Check logs for details."
|
| 40 |
try:
|
| 41 |
-
# Load and
|
| 42 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
temp_wav = f"/tmp/{os.path.basename(audio_file)}.wav"
|
| 45 |
sf.write(temp_wav, audio, sr)
|
| 46 |
|
| 47 |
-
# Transcribe
|
| 48 |
-
|
|
|
|
| 49 |
transcription = result.get("text", "").strip()
|
| 50 |
print(f"Transcription: {transcription}")
|
| 51 |
|
|
@@ -57,6 +93,10 @@ def transcribe_audio(audio_file):
|
|
| 57 |
|
| 58 |
if not transcription:
|
| 59 |
return "Transcription empty. Please provide clear audio describing symptoms in English."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return transcription
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error transcribing audio: {str(e)}"
|
|
@@ -64,11 +104,12 @@ def transcribe_audio(audio_file):
|
|
| 64 |
def analyze_symptoms(text):
|
| 65 |
"""Analyze symptoms using local Symptom-2-Disease model."""
|
| 66 |
if not symptom_classifier:
|
| 67 |
-
return "Error: Symptom-2-Disease model not loaded. Check logs for details.", 0.0
|
| 68 |
try:
|
| 69 |
if not text or "Error transcribing" in text:
|
| 70 |
return "No valid transcription for analysis.", 0.0
|
| 71 |
-
|
|
|
|
| 72 |
if result and isinstance(result, list) and len(result) > 0:
|
| 73 |
prediction = result[0]["label"]
|
| 74 |
score = result[0]["score"]
|
|
@@ -141,7 +182,7 @@ iface = gr.Interface(
|
|
| 141 |
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
|
| 142 |
outputs=gr.Textbox(label="Health Assessment Feedback"),
|
| 143 |
title="Health Voice Analyzer",
|
| 144 |
-
description="Record or upload a voice sample describing symptoms for preliminary health assessment. Supports English (transcription), with symptom analysis in English."
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
|
|
|
| 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))
|
| 14 |
+
def load_whisper_model():
|
| 15 |
+
try:
|
| 16 |
+
# Whisper for speech-to-text (English-only)
|
| 17 |
+
model = pipeline(
|
| 18 |
+
"automatic-speech-recognition",
|
| 19 |
+
model="openai/whisper-tiny.en",
|
| 20 |
+
device=-1, # CPU; use device=0 for GPU if available
|
| 21 |
+
model_kwargs={"use_safetensors": True}
|
| 22 |
+
)
|
| 23 |
+
print("Whisper model loaded successfully.")
|
| 24 |
+
return model
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Failed to load Whisper model: {str(e)}")
|
| 27 |
+
raise
|
| 28 |
+
|
| 29 |
+
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
|
| 30 |
+
def load_symptom_model():
|
| 31 |
+
try:
|
| 32 |
+
# Symptom-2-Disease for health analysis
|
| 33 |
+
model = pipeline(
|
| 34 |
+
"text-classification",
|
| 35 |
+
model="abhirajeshbhai/symptom-2-disease-net",
|
| 36 |
+
device=-1, # CPU
|
| 37 |
+
model_kwargs={"use_safetensors": True}
|
| 38 |
+
)
|
| 39 |
+
print("Symptom-2-Disease model loaded successfully.")
|
| 40 |
+
return model
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Failed to load Symptom-2-Disease model: {str(e)}")
|
| 43 |
+
raise
|
| 44 |
+
|
| 45 |
+
whisper = None
|
| 46 |
+
symptom_classifier = None
|
| 47 |
|
|
|
|
| 48 |
try:
|
| 49 |
+
whisper = load_whisper_model()
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
+
print(f"Whisper model initialization failed after retries: {str(e)}")
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
symptom_classifier = load_symptom_model()
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
+
print(f"Symptom-2-Disease model initialization failed after retries: {str(e)}")
|
|
|
|
| 57 |
|
| 58 |
def compute_file_hash(file_path):
|
| 59 |
"""Compute MD5 hash of a file to check uniqueness."""
|
|
|
|
| 66 |
def transcribe_audio(audio_file):
|
| 67 |
"""Transcribe audio using local Whisper model."""
|
| 68 |
if not whisper:
|
| 69 |
+
return "Error: Whisper model not loaded. Check logs for details or ensure sufficient compute resources."
|
| 70 |
try:
|
| 71 |
+
# Load and validate audio
|
| 72 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 73 |
+
if len(audio) < 1600: # Less than 0.1s
|
| 74 |
+
return "Error: Audio too short. Please provide audio of at least 1 second."
|
| 75 |
+
if np.max(np.abs(audio)) < 1e-4: # Too quiet
|
| 76 |
+
return "Error: Audio too quiet. Please provide clear audio describing symptoms in English."
|
| 77 |
+
|
| 78 |
+
# Save as WAV for Whisper
|
| 79 |
temp_wav = f"/tmp/{os.path.basename(audio_file)}.wav"
|
| 80 |
sf.write(temp_wav, audio, sr)
|
| 81 |
|
| 82 |
+
# Transcribe with beam search for accuracy
|
| 83 |
+
with torch.no_grad():
|
| 84 |
+
result = whisper(temp_wav, generate_kwargs={"num_beams": 5})
|
| 85 |
transcription = result.get("text", "").strip()
|
| 86 |
print(f"Transcription: {transcription}")
|
| 87 |
|
|
|
|
| 93 |
|
| 94 |
if not transcription:
|
| 95 |
return "Transcription empty. Please provide clear audio describing symptoms in English."
|
| 96 |
+
# Check for repetitive transcription
|
| 97 |
+
words = transcription.split()
|
| 98 |
+
if len(words) > 5 and len(set(words)) < len(words) / 2:
|
| 99 |
+
return "Error: Transcription appears repetitive. Please provide clear, non-repetitive audio describing symptoms."
|
| 100 |
return transcription
|
| 101 |
except Exception as e:
|
| 102 |
return f"Error transcribing audio: {str(e)}"
|
|
|
|
| 104 |
def analyze_symptoms(text):
|
| 105 |
"""Analyze symptoms using local Symptom-2-Disease model."""
|
| 106 |
if not symptom_classifier:
|
| 107 |
+
return "Error: Symptom-2-Disease model not loaded. Check logs for details or ensure sufficient compute resources.", 0.0
|
| 108 |
try:
|
| 109 |
if not text or "Error transcribing" in text:
|
| 110 |
return "No valid transcription for analysis.", 0.0
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
result = symptom_classifier(text)
|
| 113 |
if result and isinstance(result, list) and len(result) > 0:
|
| 114 |
prediction = result[0]["label"]
|
| 115 |
score = result[0]["score"]
|
|
|
|
| 182 |
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
|
| 183 |
outputs=gr.Textbox(label="Health Assessment Feedback"),
|
| 184 |
title="Health Voice Analyzer",
|
| 185 |
+
description="Record or upload a voice sample describing symptoms for preliminary health assessment. Supports English (transcription), with symptom analysis in English. Use clear audio (WAV, 16kHz) describing symptoms like 'I have a cough.'"
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|