|
|
|
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 |
|
import sys |
|
|
|
|
|
salutation = "Pleasure meeting you. Have a nice day!" |
|
|
|
|
|
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 = '' |
|
|
|
|
|
|
|
|
|
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): |
|
|
|
output = avatar.process_input(input_text) |
|
|
|
save_output(output) |
|
response_sentiment = output['emotion'] |
|
ans = load_output() |
|
|
|
return ans, response_sentiment |
|
|
|
|
|
r = sr.Recognizer() |
|
|
|
def record_voice(): |
|
try: |
|
with sr.Microphone() as source: |
|
st.write("Speak: ") |
|
st.write("Please wait, response under process...") |
|
audio = r.listen(source) |
|
|
|
text = r.recognize_google(audio) |
|
user_input = text + '?' |
|
|
|
return user_input |
|
|
|
except sr.RequestError as e: |
|
raise customexception("Could not request results: {0}".format(e), sys) |
|
|
|
except sr.UnknownValueError: |
|
raise customexception("Unknown error occurred.", sys) |
|
|
|
|
|
if mode == "Text" and st.session_state.HF_TOKEN == '': |
|
if 'chathist' not in st.session_state: |
|
st.session_state.chathist = dict() |
|
|
|
|
|
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() + '?' |
|
|
|
|
|
if 'exit' in user_input: |
|
st.write(salutation) |
|
else: |
|
|
|
ans, senti = response(user_input) |
|
|
|
|
|
st.session_state.chathist = chat_history(user_input, ans, senti) |
|
|
|
with st.container(border=True): |
|
user_col1, user_col2, user_col3 = st.columns([1, 1, 3], vertical_alignment="center") |
|
user = user_col3.container(border=True) |
|
user.write(f"You: {user_input}") |
|
bot_col1, bot_col2, bot_col3 = st.columns([3, 1, 1], vertical_alignment='center') |
|
bot = bot_col1.container(border=True) |
|
bot.write(f"Bot: {ans}") |
|
|
|
elif mode == "Voice" and st.session_state.HF_TOKEN == '': |
|
try: |
|
if 'chathist' not in st.session_state: |
|
st.session_state.chathist = dict() |
|
|
|
if st.button("speak"): |
|
user_input = record_voice() |
|
|
|
if user_input != "": |
|
|
|
|
|
if 'exit' in user_input: |
|
st.write(salutation) |
|
else: |
|
|
|
ans, senti = response(user_input) |
|
|
|
st.write(f"You: {user_input}") |
|
st.write(f"Bot: {ans}") |
|
play_speech(ans) |
|
|
|
|
|
st.session_state.chathist = chat_history(user_input, ans, senti) |
|
except Exception as e: |
|
raise customexception(e, sys) |
|
|
|
|
|
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]) |
|
st.stop() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|