Spaces:
Configuration error
Configuration error
File size: 9,170 Bytes
3a6775a |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import streamlit as st
import time
import requests
import voicetotext
from audio_recorder_streamlit import audio_recorder
import os
from openai import OpenAI
import base64
import subprocess
# openai.api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key="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")
# Create a container for the microphone and audio recording
# footer_container = st.container()
# with footer_container:
# audio_bytes = audio_recorder()
# Start Server.py as a background process
if "server_started" not in st.session_state:
st.session_state.server_started = True
subprocess.Popen(["python", "ServerV2.py"])
def generate_response(message, value):
new_data = {
"Question": message,
"User_Name" : st.session_state.user_name,
"User_Phone" : st.session_state.user_phone,
"User_Email" : st.session_state.user_email,
"Pincode" : st.session_state.pincode,
"Language":st.session_state.language
}
if value == 0:
url_post = "http://localhost:8088/askquery"
post_response = requests.post(url_post, json=new_data)
response_json = post_response.json()
return response_json
else:
url_post = "http://localhost:8088/askvector"
post_response = requests.post(url_post, json=new_data)
response_json = post_response.json()
return response_json
def msendpin(pincode):
new_data={
"Pincode":st.session_state.pincode
}
url_post = "http://localhost:8088/maps"
post_response = requests.post(url_post, json=new_data)
response_json = post_response.json()
return response_json
def senddetail(user_name,user_phone, user_email, pincode,language):
new_data = {
"User_Name" : user_name,
"User_Phone" : user_phone,
"User_Email" : user_email,
"Pincode" : pincode,
"Language":language
}
url_post = "http://localhost:8088/askd"
post_response = requests.post(url_post, json=new_data)
response_json = post_response.json()
return response_json
def getdetail():
user_name = st.text_input("Enter your Name: ", type="default")
user_phone = st.text_input("Enter your phone number: ", type="default")
user_email = st.text_input("Enter your email: ", type="default")
pincode = st.text_input("Enter your pincode: ", type="default")
language = "English"
# language=st.selectbox("Select a language...",("English","Hindi","Kannada","tamil"))
if st.button("Submit"):
if (user_name and user_phone and user_email and pincode and language):
response = senddetail(user_name,user_phone, user_email, pincode,language)
print(response)
if (response['status']==1):
st.success("Successfully sent data")
else:
st.error("Error in sending data")
else:
st.error("Please enter all the details")
return user_name,user_phone,user_email,pincode,language
def createside():
with st.sidebar:
v1 = st.checkbox("Search Trusted Sources")
if v1 not in st.session_state:
st.session_state.v1 = v1
if(int(st.session_state.v1)==0):
st.session_state.img="./assets/red.png"
else:
st.session_state.img="./assets/blue.png"
# st.button("Flag Content",on_click=flag())
pages = ["User info", "Chat","Services"]
page = st.radio("Select a page:", pages)
return page
def services_page():
maps= st.button("Find nearest hospitals")
if maps:
hospital_info = msendpin(st.session_state.pincode)
st.markdown("<h3 style='text-align: left;'>Hospital 1 details </h3>", unsafe_allow_html=True)
st.markdown("Name: "+hospital_info['data']['hospital1'][0])
st.markdown("Address: "+hospital_info['data']['hospital1'][1]+" Phone Number:"+hospital_info['data']['hospital1'][2])
st.markdown("<h3 style='text-align: left;'>Hospital 2 details </h3>", unsafe_allow_html=True)
st.markdown("Name: "+hospital_info['data']['hospital2'][0])
st.markdown("Address: "+hospital_info['data']['hospital2'][1]+" Phone Number:"+hospital_info['data']['hospital2'][2])
st.markdown("<h3 style='text-align: left;'>Hospital 3 details </h3>", unsafe_allow_html=True)
st.markdown("Name: "+hospital_info['data']['hospital3'][0])
st.markdown("Address: "+hospital_info['data']['hospital3'][1]+" Phone Number:"+hospital_info['data']['hospital3'][2])
ssession= st.button("Book a session")
if ssession:
sendm()
#mailer.sendem("name","chathistory")
#mailer.sendem()
#if res==1:
st.success("Successfully booked a session, please check your email for further details")
def sendm():
new_data={
"User_name":st.session_state.user_name,
}
print("sendm called")
url_post = "http://localhost:8088/maill"
post_response = requests.post(url_post, json=new_data)
#response_json = post_response.json()
# return #response_json
def text_to_speech(input_text):
response = client.audio.speech.create(
model="tts-1",
voice="nova",
input=input_text
)
webm_file_path = "temp_audio_play.mp3"
with open(webm_file_path, "wb") as f:
response.stream_to_file(webm_file_path)
return webm_file_path
def autoplay_audio(file_path: str):
with open(file_path, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode("utf-8")
md = f"""
<audio autoplay>
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
</audio>
"""
st.markdown(md, unsafe_allow_html=True)
def sendinput(question,avatar, audio_output=False):
with st.chat_message("user",avatar=st.session_state.userimg):
st.markdown(question)
st.session_state.messages.append({"role": "user", "content": question,"avatar":st.session_state.userimg})
response = generate_response(question, int(st.session_state.v1))
answer = response['data']['Answer']
if audio_output == True:
audio_file = text_to_speech(answer)
autoplay_audio(audio_file)
os.remove(audio_file)
full_response = ""
with st.chat_message("assistant",avatar=avatar):
message_placeholder = st.empty()
for chunk in answer.split():
full_response += chunk + " "
time.sleep(0.05)
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
if(response['data']['Source1']!=""):
st.info(response['data']['Source1'])
st.info(response['data']['Source2'])
follow = response['data']['Followup']
#follow="Follow up question:"+follow
st.success("**Follow up question:**")
st.success(follow)
st.session_state.messages.append({"role": "assistant", "content": answer,"avatar":st.session_state.img})
def chat_page():
# Create messages list in sessionstate
if "messages" not in st.session_state:
st.session_state.messages = []
if "help" not in st.session_state:
st.session_state.help = []
## Print messages from chat history on rerun
for message in st.session_state.messages:
with st.chat_message(message["role"],avatar=message["avatar"]):
st.markdown(message["content"])
if st.sidebar.button("Start Audio Input"):
audio_output = True
with st.spinner("Listening..."):
recognized_text = voicetotext.listen()
sendinput(recognized_text,st.session_state.img, audio_output)
var = "Ask a mental health question..."
if a := st.chat_input(var):
sendinput(a,st.session_state.img)
def main():
st.title("MindEase")
if "user_name" not in st.session_state:
st.session_state.user_name = ""
if "user_phone" not in st.session_state:
st.session_state.user_phone = ""
if "user_email" not in st.session_state:
st.session_state.user_email = ""
if "pincode" not in st.session_state:
st.session_state.pincode = ""
if "userimg" not in st.session_state:
st.session_state.userimg="./assets/img.png"
if "language" not in st.session_state:
st.session_state.language="English"
page=createside()
# st.sidebar.button("Flag content")
if page == "User info":
user_detail = getdetail()
if (user_detail[0] and user_detail[1] and user_detail[2] and user_detail[3] and user_detail[4]):
st.session_state.user_name = user_detail[0]
st.session_state.user_phone = user_detail[1]
st.session_state.user_email = user_detail[2]
st.session_state.pincode = user_detail[3]
st.session_state.language=user_detail[4]
elif page == "Chat":
if (st.session_state.user_phone!=""):
chat_page()
else:
st.warning("Please enter your details first on the 'User info' page.")
elif page == "Services": # Add a new condition for "Help Services"
services_page()
if __name__ == "__main__":
main()
|