File size: 1,897 Bytes
be9a30f
 
 
 
8d6d690
 
be9a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d6d690
 
 
 
 
 
be9a30f
 
 
 
 
 
 
6d81cbc
8d6d690
be9a30f
 
8d6d690
be9a30f
 
 
 
8d6d690
 
 
 
be9a30f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()