File size: 6,557 Bytes
af0c0e2 d359a51 af0c0e2 d359a51 af0c0e2 d6f7b0e af0c0e2 d6f7b0e af0c0e2 d6f7b0e af0c0e2 d6f7b0e af0c0e2 d6f7b0e af0c0e2 d6f7b0e af0c0e2 d359a51 d6f7b0e af0c0e2 d359a51 af0c0e2 d6f7b0e af0c0e2 d359a51 af0c0e2 d6f7b0e |
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 |
# Main driver code of chatbot
from cgitb import text
import streamlit as st
from streamlit_option_menu import option_menu
from src.components.avatarsys import AvatarSystem
from src.exception.exception import customexception
from src.utils.accessory import play_speech, listen, save_output, load_output
import speech_recognition as sr
# Global
salutation = "Pleasure meeting you. Have a nice day!"
# Page title
st.markdown("<h1 style='text-align: center;'>Hi, Dr Bot Junior here.</h1>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>I am your chatbot assistant.</h3>", unsafe_allow_html=True)
mode = option_menu("Choose mode of interaction", ["Text", "Voice"],
icons=['heart-pulse', 'mic'],
menu_icon="cast", default_index=0, orientation="horizontal")
if "HF_TOKEN" not in st.session_state:
st.session_state.HF_TOKEN = ''
# st.write("Add your Huggingface Access Token to use the chatbot.")
# st.session_state.HF_TOKEN = st.text_input("Your Access Token: ")
# Chatbot configuration initiation
if st.session_state.HF_TOKEN == '':
avatar = AvatarSystem(st.session_state.HF_TOKEN)
def chat_history(input, response, sentiment):
if 'history' not in st.session_state:
st.session_state.history = dict()
st.session_state.history[input] = [response, sentiment]
return st.session_state.history
def response(input_text):
# Getting response and sentiment of response
output = avatar.process_input(input_text)
# Save output response in txt
save_output(output)
response_sentiment = output['emotion']
ans = load_output()
return ans, response_sentiment
# Voice to text conversion
r = sr.Recognizer()
def record_voice():
try:
with sr.Microphone() as source:
st.write("Speak: ") # print("Say something!")
st.write("Please wait, response under process...")
audio = r.listen(source)
# r.adjust_for_ambient_noise(source, duration=0.2)
text = r.recognize_google(audio)
user_input = text + '?'
return user_input
except sr.RequestError as e:
st.write("Could not request results: {0}".format(e))
except sr.UnknownValueError:
st.write("Unknown error occurred.")
if mode == "Text" and st.session_state.HF_TOKEN == '':
if 'chathist' not in st.session_state:
st.session_state.chathist = dict()
# Form requires unique key
with st.form(key=f'Chat form', clear_on_submit=True):
user_input = st.text_input("You: ", value="", placeholder="Ask anything or Type 'Exit'")
col1, col2, col3, col4, col5, col6 = st.columns(6)
save = col6.form_submit_button("Click here")
if save and user_input != "":
user_input = user_input.lower() + '?'
# Exiting the chat
if 'exit' in user_input:
st.write(salutation)
# Getting response and sentiment of response
ans, senti = response(user_input)
# Chat history
st.session_state.chathist = chat_history(user_input, ans, senti)
# # Chat history display
# if st.button("View chat")
# st.markdown("### Chat History: ")
# with st.container(border=True):
# for key in st.session_state.chathist.keys():
# user_col1, user_col2, user_col3 = st.columns(3, vertical_alignment="center")
# user = user_col3.container(border=True)
# user.write(key)
# bot_col1, bot_col2, bot_col3 = st.columns([4, 1, 1], vertical_alignment='center')
# bot = bot_col1.container(border=True)
# bot.write(st.session_state.chathist[key][0])
elif mode == "Voice" and st.session_state.HF_TOKEN == '':
if 'chathist' not in st.session_state:
st.session_state.chathist = dict()
if st.button("speak"):
user_input = record_voice()
if user_input != "":
# Exiting the chat
if 'exit' in user_input:
st.write(salutation)
st.stop()
# Getting response and sentiment of response
ans, senti = response(user_input)
st.write(f"You: {user_input}")
st.write(f"Bot: {ans}")
play_speech(ans)
# Chat history
st.session_state.chathist = chat_history(user_input, ans, senti)
# Chat history display
if st.button("View Chat history"):
st.markdown("### Chat History: ")
with st.container(border=True):
for key in st.session_state.chathist.keys():
user_col1, user_col2, user_col3 = st.columns(3, vertical_alignment="center")
user = user_col3.container(border=True)
user.write(key)
bot_col1, bot_col2, bot_col3 = st.columns([4, 1, 1], vertical_alignment='center')
bot = bot_col1.container(border=True)
bot.write(st.session_state.chathist[key][0])
# if mode == "Doc-Bot" and st.session_state.HF_TOKEN != '':
# st.write("Doc-Bot implementation")
# if 'doc_chat_hist' not in st.session_state:
# st.session_state.doc_chat_hist = dict()
# # Form requires unique key
# with st.form(key=f'Chat form', clear_on_submit=True):
# user_input = st.text_input("You: ", value="", placeholder="Ask anything or Type 'Exit'")
# col1, col2, col3, col4, col5, col6 = st.columns(6)
# save = col6.form_submit_button("Click here")
# if save and user_input != "":
# user_input = user_input.lower() + '?'
# # Exiting the chat
# if 'exit' in user_input:
# st.write(salutation)
# play_speech(salutation)
# # Getting response and sentiment of response
# ans, senti = response(user_input, docbot=True)
# # Chat history
# st.session_state.doc_chat_hist = chat_history(user_input, ans, senti)
# # Chat history display
# st.markdown("### Chat History: ")
# with st.container(border=True):
# for key in st.session_state.doc_chat_hist.keys():
# user_col1, user_col2, user_col3 = st.columns(3, vertical_alignment="center")
# user = user_col3.container(border=True)
# user.write(key)
# bot_col1, bot_col2, bot_col3 = st.columns([4, 1, 1], vertical_alignment='center')
# bot = bot_col1.container(border=True)
# bot.write(st.session_state.doc_chat_hist[key][0]) |