Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -103,23 +103,20 @@ def analyze_symptoms(text):
|
|
| 103 |
feedback = []
|
| 104 |
if "cough" in text or "difficulty breathing" in text:
|
| 105 |
feedback.append("Symptoms like cough or difficulty breathing may indicate a respiratory condition, such as bronchitis or asthma. Consult a doctor.")
|
| 106 |
-
if "stressed" in text or "stress" in text or "fatigue" in text:
|
| 107 |
feedback.append("Reported stress or fatigue may suggest conditions like anxiety or chronic fatigue syndrome. Seek medical advice.")
|
| 108 |
if not feedback:
|
| 109 |
feedback.append("No specific conditions detected from reported symptoms.")
|
| 110 |
return "\n".join(feedback)
|
| 111 |
|
| 112 |
-
def analyze_voice(audio_file=None
|
| 113 |
"""Analyze voice for health indicators."""
|
| 114 |
try:
|
| 115 |
-
#
|
| 116 |
if audio_file and os.path.exists(audio_file):
|
| 117 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 118 |
-
elif audio_data is not None:
|
| 119 |
-
audio = audio_data
|
| 120 |
-
sr = 16000
|
| 121 |
else:
|
| 122 |
-
raise ValueError("No audio
|
| 123 |
|
| 124 |
if len(audio) < sr:
|
| 125 |
raise ValueError("Audio too short (minimum 1 second)")
|
|
@@ -161,7 +158,7 @@ def analyze_voice(audio_file=None, audio_data=None):
|
|
| 161 |
feedback_str = "\n".join(feedback)
|
| 162 |
|
| 163 |
# Store in Salesforce
|
| 164 |
-
if sf
|
| 165 |
store_in_salesforce(audio_file, feedback_str, respiratory_score, mental_health_score, features, transcription)
|
| 166 |
|
| 167 |
return feedback_str
|
|
@@ -177,7 +174,7 @@ def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_s
|
|
| 177 |
"Feedback__c": feedback,
|
| 178 |
"RespiratoryScore__c": float(respiratory_score),
|
| 179 |
"MentalHealthScore__c": float(mental_health_score),
|
| 180 |
-
"AudioFileName__c": os.path.basename(audio_file) if audio_file else "
|
| 181 |
"Pitch__c": float(features["pitch"]),
|
| 182 |
"Jitter__c": float(features["jitter"]),
|
| 183 |
"Shimmer__c": float(features["shimmer"]),
|
|
@@ -188,34 +185,15 @@ def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_s
|
|
| 188 |
except Exception as e:
|
| 189 |
logger.error(f"Salesforce storage failed: {str(e)}")
|
| 190 |
|
| 191 |
-
def test_with_sample_audio():
|
| 192 |
-
"""Test with dummy audio simulating a user's voice saying 'I have a cough and feel stressed'."""
|
| 193 |
-
logger.info("Starting test with in-memory audio simulation")
|
| 194 |
-
# Generate synthetic audio: 150 Hz base frequency with variations to mimic a stressed voice with cough
|
| 195 |
-
sr = 16000
|
| 196 |
-
t = np.linspace(0, 2, 2 * sr)
|
| 197 |
-
freq_mod = 150 + 25 * np.sin(2 * np.pi * 0.5 * t) # Increased jitter for respiratory hint
|
| 198 |
-
amplitude_mod = 0.5 + 0.25 * np.sin(2 * np.pi * 0.3 * t) # Increased shimmer for stress hint
|
| 199 |
-
noise = 0.05 * np.random.normal(0, 1, len(t)) # Moderate noise
|
| 200 |
-
dummy_audio = amplitude_mod * np.sin(2 * np.pi * freq_mod * t) + noise
|
| 201 |
-
# Ensure dummy_audio is a 1D NumPy array
|
| 202 |
-
dummy_audio = np.asarray(dummy_audio, dtype=np.float32).flatten()
|
| 203 |
-
if not isinstance(dummy_audio, np.ndarray) or dummy_audio.ndim != 1:
|
| 204 |
-
logger.error(f"Invalid dummy_audio: type={type(dummy_audio)}, shape={dummy_audio.shape if hasattr(dummy_audio, 'shape') else 'N/A'}")
|
| 205 |
-
raise ValueError("Generated audio is not a 1D NumPy array")
|
| 206 |
-
logger.info(f"Dummy audio shape: {dummy_audio.shape}, type: {type(dummy_audio)}, dtype: {dummy_audio.dtype}")
|
| 207 |
-
return analyze_voice(audio_data=dummy_audio)
|
| 208 |
-
|
| 209 |
# Gradio interface
|
| 210 |
iface = gr.Interface(
|
| 211 |
fn=analyze_voice,
|
| 212 |
-
inputs=gr.Audio(type="filepath", label="Record
|
| 213 |
outputs=gr.Textbox(label="Health Assessment Results"),
|
| 214 |
title="Voice Health Analyzer",
|
| 215 |
-
description="
|
| 216 |
)
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
| 219 |
-
logger.info("Starting Voice Health Analyzer at
|
| 220 |
-
print(test_with_sample_audio())
|
| 221 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 103 |
feedback = []
|
| 104 |
if "cough" in text or "difficulty breathing" in text:
|
| 105 |
feedback.append("Symptoms like cough or difficulty breathing may indicate a respiratory condition, such as bronchitis or asthma. Consult a doctor.")
|
| 106 |
+
if "stressed" in text or "stress" in text or "tired" in text or "fatigue" in text:
|
| 107 |
feedback.append("Reported stress or fatigue may suggest conditions like anxiety or chronic fatigue syndrome. Seek medical advice.")
|
| 108 |
if not feedback:
|
| 109 |
feedback.append("No specific conditions detected from reported symptoms.")
|
| 110 |
return "\n".join(feedback)
|
| 111 |
|
| 112 |
+
def analyze_voice(audio_file=None):
|
| 113 |
"""Analyze voice for health indicators."""
|
| 114 |
try:
|
| 115 |
+
# Load audio from file if provided
|
| 116 |
if audio_file and os.path.exists(audio_file):
|
| 117 |
audio, sr = librosa.load(audio_file, sr=16000)
|
|
|
|
|
|
|
|
|
|
| 118 |
else:
|
| 119 |
+
raise ValueError("No valid audio file provided for analysis")
|
| 120 |
|
| 121 |
if len(audio) < sr:
|
| 122 |
raise ValueError("Audio too short (minimum 1 second)")
|
|
|
|
| 158 |
feedback_str = "\n".join(feedback)
|
| 159 |
|
| 160 |
# Store in Salesforce
|
| 161 |
+
if sf:
|
| 162 |
store_in_salesforce(audio_file, feedback_str, respiratory_score, mental_health_score, features, transcription)
|
| 163 |
|
| 164 |
return feedback_str
|
|
|
|
| 174 |
"Feedback__c": feedback,
|
| 175 |
"RespiratoryScore__c": float(respiratory_score),
|
| 176 |
"MentalHealthScore__c": float(mental_health_score),
|
| 177 |
+
"AudioFileName__c": os.path.basename(audio_file) if audio_file else "user_recorded_audio",
|
| 178 |
"Pitch__c": float(features["pitch"]),
|
| 179 |
"Jitter__c": float(features["jitter"]),
|
| 180 |
"Shimmer__c": float(features["shimmer"]),
|
|
|
|
| 185 |
except Exception as e:
|
| 186 |
logger.error(f"Salesforce storage failed: {str(e)}")
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# Gradio interface
|
| 189 |
iface = gr.Interface(
|
| 190 |
fn=analyze_voice,
|
| 191 |
+
inputs=gr.Audio(type="filepath", label="Record or Upload Your Voice (WAV, MP3, FLAC, 1+ sec)", source="upload", format="wav"),
|
| 192 |
outputs=gr.Textbox(label="Health Assessment Results"),
|
| 193 |
title="Voice Health Analyzer",
|
| 194 |
+
description="Record or upload your voice (minimum 1 second) to receive preliminary health insights. Speak clearly in English about your symptoms."
|
| 195 |
)
|
| 196 |
|
| 197 |
if __name__ == "__main__":
|
| 198 |
+
logger.info("Starting Voice Health Analyzer at 11:58 AM IST, June 23, 2025")
|
|
|
|
| 199 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|