Spaces:
Build error
Build error
Rename app.py: to app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import pipeline
|
3 |
+
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 |
+
with open(audio_file, "rb") as audio:
|
14 |
+
transcription = speech_to_text(audio.read())
|
15 |
+
return transcription['text']
|
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")
|
19 |
+
|
20 |
+
# Define possible health conditions
|
21 |
+
health_conditions = ["respiratory issues", "mental health conditions", "fever", "asthma", "coughing"]
|
22 |
+
|
23 |
+
def analyze_health_condition(text):
|
24 |
+
result = health_assessment(text, candidate_labels=health_conditions)
|
25 |
+
return result
|
26 |
+
|
27 |
+
# Step 4: Provide Feedback to the User Based on Health Assessment
|
28 |
+
def provide_feedback(health_assessment_result):
|
29 |
+
if 'respiratory issues' in health_assessment_result['labels']:
|
30 |
+
return "Possible respiratory issue detected, consult a doctor."
|
31 |
+
elif 'mental health conditions' in health_assessment_result['labels']:
|
32 |
+
return "Possible mental health concern detected, seek professional help."
|
33 |
+
else:
|
34 |
+
return "No significant health concerns detected. Keep monitoring your health."
|
35 |
+
|
36 |
+
# Step 5: Convert Text Feedback to Speech (Text-to-Speech)
|
37 |
+
def text_to_speech(text):
|
38 |
+
tts = gTTS(text, lang='en')
|
39 |
+
tts.save("response.mp3")
|
40 |
+
os.system("start response.mp3") # Play the audio file (Windows-specific command)
|
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 |
+
sf = Salesforce(username='your_username', password='your_password', security_token='your_token')
|
45 |
+
|
46 |
+
# Create a new record for the user interaction in Salesforce
|
47 |
+
sf.Contact.create({
|
48 |
+
'FirstName': user_first_name,
|
49 |
+
'LastName': user_last_name,
|
50 |
+
'Email': user_email,
|
51 |
+
'VoiceAnalysisResult': feedback,
|
52 |
+
})
|
53 |
+
print(f"Data stored successfully for {user_first_name} {user_last_name}.")
|
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
|
66 |
+
feedback = provide_feedback(health_feedback)
|
67 |
+
print(f"Feedback: {feedback}")
|
68 |
+
|
69 |
+
# Step 4: Convert the feedback to speech for accessibility
|
70 |
+
text_to_speech(feedback)
|
71 |
+
|
72 |
+
# Step 5: Store the user interaction data in Salesforce (optional)
|
73 |
+
store_user_data_to_salesforce(user_first_name, user_last_name, user_email, feedback)
|
74 |
+
|
75 |
+
return feedback
|
76 |
+
|
77 |
+
|
78 |
+
# Example Usage:
|
79 |
+
# Assuming you have a user's voice recorded in "user_voice.wav"
|
80 |
+
audio_file = "user_voice.wav"
|
81 |
+
user_first_name = "John"
|
82 |
+
user_last_name = "Doe"
|
83 |
+
user_email = "[email protected]"
|
84 |
+
|
85 |
+
# Analyze the user's voice and provide feedback
|
86 |
+
feedback = analyze_voice_health(audio_file, user_first_name, user_last_name, user_email)
|
87 |
+
print(f"Final Feedback: {feedback}")
|
app.py:
DELETED
File without changes
|