alaroye / src /web.py
abdulazeezoj's picture
Add speech output
8d6d690
import os
import sys
import pathlib
import gradio as gr
import threading
# Add Osanyin to the path
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from alaroye.alaroye import Alaroye
# Initialize the OmdenaBot
alaroye = Alaroye(version="v0.0.0")
# Load the vectorstore
alaroye.load()
def add_text(history, text):
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
def bot(history):
# Generate a response
response = alaroye.ask(history[-1][0])
# Get the answer
answer = response.get(
"answer",
"Sorry, I don't know that.",
)
# Speak the answer in a separate thread
def speak_answer():
alaroye._speak(answer)
speak_thread = threading.Thread(target=speak_answer)
speak_thread.start()
# Update the history
history[-1][1] = answer
return history
with gr.Blocks(title="Alaroye") as demo:
chatbot = gr.Chatbot([], elem_id="chatbot", label="Alaroye", height=750)
with gr.Row():
# Column for text input
with gr.Column(scale=0.85):
txt = gr.Textbox(
show_label=False,
placeholder="Enter text and press enter, or upload an image",
container=False,
)
# Column for button
with gr.Column(scale=0.15, min_width=0):
btn = gr.Button(value="Send")
# Button click
btn_msg = btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
bot, chatbot, chatbot
)
btn_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
# Textbox enter
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
bot, chatbot, chatbot
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
if __name__ == "__main__":
demo.launch()