Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "jq4VVHUFti6TmzJDjjegv2h6b")
|
|
| 14 |
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL", "https://voicebot-dev-ed.my.salesforce.com") # Verify correct API URL
|
| 15 |
|
| 16 |
# Hugging Face Inference API token (store in environment variables)
|
| 17 |
-
HF_TOKEN = os.getenv("HF_TOKEN"
|
| 18 |
|
| 19 |
# Initialize Salesforce connection
|
| 20 |
try:
|
|
@@ -43,14 +43,21 @@ def compute_file_hash(file_path):
|
|
| 43 |
|
| 44 |
def transcribe_audio(audio_file):
|
| 45 |
"""Transcribe audio using Whisper API."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
with open(audio_file, "rb") as f:
|
| 48 |
data = f.read()
|
| 49 |
response = requests.post(WHISPER_API_URL, headers=HEADERS, data=data)
|
| 50 |
response.raise_for_status()
|
| 51 |
result = response.json()
|
| 52 |
-
transcription = result.get("text", "").strip()
|
| 53 |
print(f"Whisper API response: {result}")
|
|
|
|
| 54 |
if not transcription:
|
| 55 |
return "Transcription empty. Please provide clear audio describing symptoms in English."
|
| 56 |
print(f"Transcription: {transcription}")
|
|
@@ -63,7 +70,7 @@ def transcribe_audio(audio_file):
|
|
| 63 |
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings. "
|
| 64 |
"Ensure token has Inference API access (get at https://huggingface.co/settings/tokens)."
|
| 65 |
)
|
| 66 |
-
print(error_msg)
|
| 67 |
return error_msg
|
| 68 |
except Exception as e:
|
| 69 |
error_msg = f"Error transcribing audio: {str(e)}"
|
|
@@ -72,6 +79,13 @@ def transcribe_audio(audio_file):
|
|
| 72 |
|
| 73 |
def analyze_symptoms(text):
|
| 74 |
"""Analyze symptoms using Symptom-2-Disease API."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
try:
|
| 76 |
if not text or "Error transcribing" in text:
|
| 77 |
return "No valid transcription for analysis.", 0.0
|
|
@@ -94,7 +108,7 @@ def analyze_symptoms(text):
|
|
| 94 |
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings. "
|
| 95 |
"Ensure token has Inference API access (get at https://huggingface.co/settings/tokens)."
|
| 96 |
)
|
| 97 |
-
print(error_msg)
|
| 98 |
return error_msg, 0.0
|
| 99 |
except Exception as e:
|
| 100 |
error_msg = f"Error analyzing symptoms: {str(e)}"
|
|
@@ -182,7 +196,7 @@ iface = gr.Interface(
|
|
| 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. Ensure HF_TOKEN is set in Space secrets
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
|
|
|
| 14 |
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL", "https://voicebot-dev-ed.my.salesforce.com") # Verify correct API URL
|
| 15 |
|
| 16 |
# Hugging Face Inference API token (store in environment variables)
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # No default; must be set in Space secrets
|
| 18 |
|
| 19 |
# Initialize Salesforce connection
|
| 20 |
try:
|
|
|
|
| 43 |
|
| 44 |
def transcribe_audio(audio_file):
|
| 45 |
"""Transcribe audio using Whisper API."""
|
| 46 |
+
if not HF_TOKEN:
|
| 47 |
+
error_msg = (
|
| 48 |
+
"Error transcribing audio: HF_TOKEN not set. Please set HF_TOKEN in Space secrets at "
|
| 49 |
+
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings."
|
| 50 |
+
)
|
| 51 |
+
print(error_msg)
|
| 52 |
+
return error_msg
|
| 53 |
try:
|
| 54 |
with open(audio_file, "rb") as f:
|
| 55 |
data = f.read()
|
| 56 |
response = requests.post(WHISPER_API_URL, headers=HEADERS, data=data)
|
| 57 |
response.raise_for_status()
|
| 58 |
result = response.json()
|
|
|
|
| 59 |
print(f"Whisper API response: {result}")
|
| 60 |
+
transcription = result.get("text", "").strip()
|
| 61 |
if not transcription:
|
| 62 |
return "Transcription empty. Please provide clear audio describing symptoms in English."
|
| 63 |
print(f"Transcription: {transcription}")
|
|
|
|
| 70 |
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings. "
|
| 71 |
"Ensure token has Inference API access (get at https://huggingface.co/settings/tokens)."
|
| 72 |
)
|
| 73 |
+
print(f"Whisper API error: {error_msg}, Status: {e.response.status_code}")
|
| 74 |
return error_msg
|
| 75 |
except Exception as e:
|
| 76 |
error_msg = f"Error transcribing audio: {str(e)}"
|
|
|
|
| 79 |
|
| 80 |
def analyze_symptoms(text):
|
| 81 |
"""Analyze symptoms using Symptom-2-Disease API."""
|
| 82 |
+
if not HF_TOKEN:
|
| 83 |
+
error_msg = (
|
| 84 |
+
"Error analyzing symptoms: HF_TOKEN not set. Please set HF_TOKEN in Space secrets at "
|
| 85 |
+
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings."
|
| 86 |
+
)
|
| 87 |
+
print(error_msg)
|
| 88 |
+
return error_msg, 0.0
|
| 89 |
try:
|
| 90 |
if not text or "Error transcribing" in text:
|
| 91 |
return "No valid transcription for analysis.", 0.0
|
|
|
|
| 108 |
"https://huggingface.co/spaces/your-username/HealthVoiceAnalyzer/settings. "
|
| 109 |
"Ensure token has Inference API access (get at https://huggingface.co/settings/tokens)."
|
| 110 |
)
|
| 111 |
+
print(f"Symptom API error: {error_msg}, Status: {e.response.status_code}")
|
| 112 |
return error_msg, 0.0
|
| 113 |
except Exception as e:
|
| 114 |
error_msg = f"Error analyzing symptoms: {str(e)}"
|
|
|
|
| 196 |
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
|
| 197 |
outputs=gr.Textbox(label="Health Assessment Feedback"),
|
| 198 |
title="Health Voice Analyzer",
|
| 199 |
+
description="Record or upload a voice sample describing symptoms for preliminary health assessment. Supports English (transcription), with symptom analysis in English. Ensure HF_TOKEN is set in Space secrets."
|
| 200 |
)
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|