File size: 2,529 Bytes
e0169c8
 
 
 
34b78ab
e0169c8
 
 
 
 
 
 
 
8b1c859
e0169c8
 
 
a37b98a
73fea8e
 
 
 
 
 
a37b98a
73fea8e
e0169c8
34b78ab
10ddae5
 
34b78ab
8b1c859
a37b98a
8b1c859
10ddae5
 
e0169c8
 
 
 
8b1c859
 
 
 
 
 
 
 
 
a37b98a
8b1c859
 
a37b98a
 
 
 
 
 
2dfb3d3
a37b98a
 
35d616f
e0169c8
 
35d616f
 
 
 
 
 
e0169c8
35d616f
 
 
 
 
e0169c8
8b1c859
e0169c8
 
 
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
from time import perf_counter

import gradio as gr

from gradio_app.backend.query_llm import *


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def add_text(history, text):
    history = [] if history is None else history
    history = history + [(text, "")]
    return history, gr.Textbox(value="", interactive=False)


def bot(history):
    history[-1][1] = ""
    query = history[-1][0]

    if not query:
        raise gr.Error("Empty string was submitted")

    llm = 'gpt-4-turbo-preview'
    messages = get_message_constructor(llm)('', history)

    llm_gen = get_llm_generator(llm)
    logger.info('Generating answer...')
    t = perf_counter()
    for part in llm_gen(messages):
        history[-1][1] += part
        yield history
    else:
        t = perf_counter() - t
        logger.info(f'Finished Generating answer in {round(t, 2)} seconds...')


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            chatbot = gr.Chatbot(
                [],
                elem_id="chatbot",
                avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
                               'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
                bubble_full_width=False,
                show_copy_button=True,
                show_share_button=True,
                height=800
            )
        with gr.Column():
            input_textbox = gr.Textbox(
                interactive=True,
                show_label=False,
                placeholder="Enter text and press enter",
                container=False,
                autofocus=True,
                lines=35,
                max_lines=100,
            )
            txt_btn = gr.Button(value="Send", scale=1)

    # Turn off interactivity while generating if you hit enter
    txt_msg = input_textbox.submit(
        add_text, [chatbot, input_textbox], [chatbot, input_textbox], queue=False).then(
        bot, [chatbot], [chatbot]
    )
    # Turn it back on
    txt_msg.then(lambda: gr.Textbox(interactive=True), None, [input_textbox], queue=False)

    # Turn off interactivity while generating if you hit enter
    txt_msg = txt_btn.click(
        add_text, [chatbot, input_textbox], [chatbot, input_textbox], queue=False).then(
        bot, [chatbot], [chatbot]
    )
    # Turn it back on
    txt_msg.then(lambda: gr.Textbox(interactive=True), None, [input_textbox], queue=False)

demo.queue()
demo.launch(debug=True)