forked from aarmst24/Personalized_Mental_Healthcare-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonalized_Mental_Healthcare-Chatbot.py
53 lines (45 loc) · 1.83 KB
/
Personalized_Mental_Healthcare-Chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import openai
import pandas as pd
import speech_recognition as sr
import streamlit as st
# Set OpenAI API key
openai.api_key = "API_KEY"
# Load mental health dataset
mentalhealth = pd.read_csv("AI_Mental_Health.csv")
def generate_response(input_text):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": input_text}],
temperature=0.7,
max_tokens=100,
)
st.write("Chatbot Response:")
st.write(response.choices[0].message["content"])
except Exception as e:
st.write(f"Error generating response: {e}")
def main():
st.title("Personalized Mental HealthCare Chatbot App")
st.text("By: Sohaib Aamir, Team: AI-HACKERS")
input_type = st.radio("Select input type", ("Text", "Voice"))
if input_type == "Text":
user_input = st.text_input("Enter your message:")
if st.button("Send"):
generate_response(user_input)
elif input_type == "Voice":
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Adjusting for ambient noise. Speak after 3 seconds...")
recognizer.adjust_for_ambient_noise(source, duration=3)
try:
st.write("Listening...")
audio = recognizer.listen(source, timeout=5)
query = recognizer.recognize_google(audio)
st.write(f"You said: {query}")
generate_response(query)
except sr.UnknownValueError:
st.write("Sorry, I couldn't understand the audio. Please try again.")
except sr.RequestError as e:
st.write(f"Could not request results; check your internet connection: {e}")
if __name__ == '__main__':
main()