Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,19 @@ from gtts import gTTS
|
|
4 |
from simple_salesforce import Salesforce
|
5 |
import soundfile as sf
|
6 |
|
7 |
-
|
8 |
# Step 1: Hugging Face Speech-to-Text Pipeline Setup
|
9 |
speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-960h")
|
10 |
|
11 |
# Step 2: Convert Speech to Text
|
12 |
def convert_speech_to_text(audio_file):
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Step 3: Analyze the text for health-related indicators (e.g., respiratory issues)
|
18 |
health_assessment = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
@@ -21,45 +25,73 @@ health_assessment = pipeline("zero-shot-classification", model="facebook/bart-la
|
|
21 |
health_conditions = ["respiratory issues", "mental health conditions", "fever", "asthma", "coughing"]
|
22 |
|
23 |
def analyze_health_condition(text):
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Step 4: Provide Feedback to the User Based on Health Assessment
|
28 |
def provide_feedback(health_assessment_result):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Step 5: Convert Text Feedback to Speech (Text-to-Speech)
|
37 |
def text_to_speech(text):
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Step 6: Integration with Salesforce for Storing User Data
|
43 |
def store_user_data_to_salesforce(user_first_name, user_last_name, user_email, feedback):
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Step 7: Main Function to Process User's Voice Input
|
56 |
def analyze_voice_health(audio_file, user_first_name, user_last_name, user_email):
|
|
|
57 |
# Step 1: Convert speech to text
|
58 |
text = convert_speech_to_text(audio_file)
|
|
|
|
|
|
|
59 |
print(f"User's Speech Transcription: {text}")
|
60 |
|
61 |
# Step 2: Analyze the transcribed text for health conditions
|
62 |
health_feedback = analyze_health_condition(text)
|
|
|
|
|
|
|
63 |
print(f"Health assessment: {health_feedback}")
|
64 |
|
65 |
# Step 3: Provide feedback based on the health analysis
|
|
|
4 |
from simple_salesforce import Salesforce
|
5 |
import soundfile as sf
|
6 |
|
|
|
7 |
# Step 1: Hugging Face Speech-to-Text Pipeline Setup
|
8 |
speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-960h")
|
9 |
|
10 |
# Step 2: Convert Speech to Text
|
11 |
def convert_speech_to_text(audio_file):
|
12 |
+
""" Convert audio file to text """
|
13 |
+
try:
|
14 |
+
with open(audio_file, "rb") as audio:
|
15 |
+
transcription = speech_to_text(audio.read())
|
16 |
+
return transcription['text']
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error in converting speech to text: {e}")
|
19 |
+
return None
|
20 |
|
21 |
# Step 3: Analyze the text for health-related indicators (e.g., respiratory issues)
|
22 |
health_assessment = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
|
|
25 |
health_conditions = ["respiratory issues", "mental health conditions", "fever", "asthma", "coughing"]
|
26 |
|
27 |
def analyze_health_condition(text):
|
28 |
+
""" Analyze the health condition based on transcribed text """
|
29 |
+
try:
|
30 |
+
result = health_assessment(text, candidate_labels=health_conditions)
|
31 |
+
return result
|
32 |
+
except Exception as e:
|
33 |
+
print(f"Error in analyzing health condition: {e}")
|
34 |
+
return None
|
35 |
|
36 |
# Step 4: Provide Feedback to the User Based on Health Assessment
|
37 |
def provide_feedback(health_assessment_result):
|
38 |
+
""" Provide feedback based on health assessment """
|
39 |
+
if health_assessment_result is None:
|
40 |
+
return "Error in analyzing health, please try again later."
|
41 |
+
|
42 |
+
try:
|
43 |
+
if 'respiratory issues' in health_assessment_result['labels']:
|
44 |
+
return "Possible respiratory issue detected, consult a doctor."
|
45 |
+
elif 'mental health conditions' in health_assessment_result['labels']:
|
46 |
+
return "Possible mental health concern detected, seek professional help."
|
47 |
+
else:
|
48 |
+
return "No significant health concerns detected. Keep monitoring your health."
|
49 |
+
except Exception as e:
|
50 |
+
print(f"Error in providing feedback: {e}")
|
51 |
+
return "An error occurred while processing your health assessment."
|
52 |
|
53 |
# Step 5: Convert Text Feedback to Speech (Text-to-Speech)
|
54 |
def text_to_speech(text):
|
55 |
+
""" Convert text feedback to speech """
|
56 |
+
try:
|
57 |
+
tts = gTTS(text, lang='en')
|
58 |
+
tts.save("response.mp3")
|
59 |
+
os.system("start response.mp3") # Play the audio file (Windows-specific command)
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Error in text to speech: {e}")
|
62 |
|
63 |
# Step 6: Integration with Salesforce for Storing User Data
|
64 |
def store_user_data_to_salesforce(user_first_name, user_last_name, user_email, feedback):
|
65 |
+
""" Store user data to Salesforce """
|
66 |
+
try:
|
67 |
+
sf = Salesforce(username='your_username', password='your_password', security_token='your_token')
|
68 |
+
|
69 |
+
# Create a new record for the user interaction in Salesforce
|
70 |
+
sf.Contact.create({
|
71 |
+
'FirstName': user_first_name,
|
72 |
+
'LastName': user_last_name,
|
73 |
+
'Email': user_email,
|
74 |
+
'VoiceAnalysisResult': feedback,
|
75 |
+
})
|
76 |
+
print(f"Data stored successfully for {user_first_name} {user_last_name}.")
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Error in storing data to Salesforce: {e}")
|
79 |
|
80 |
# Step 7: Main Function to Process User's Voice Input
|
81 |
def analyze_voice_health(audio_file, user_first_name, user_last_name, user_email):
|
82 |
+
""" Main function to analyze voice and provide feedback """
|
83 |
# Step 1: Convert speech to text
|
84 |
text = convert_speech_to_text(audio_file)
|
85 |
+
if text is None:
|
86 |
+
return "Error in transcribing the audio."
|
87 |
+
|
88 |
print(f"User's Speech Transcription: {text}")
|
89 |
|
90 |
# Step 2: Analyze the transcribed text for health conditions
|
91 |
health_feedback = analyze_health_condition(text)
|
92 |
+
if health_feedback is None:
|
93 |
+
return "Error in analyzing health conditions."
|
94 |
+
|
95 |
print(f"Health assessment: {health_feedback}")
|
96 |
|
97 |
# Step 3: Provide feedback based on the health analysis
|