File size: 2,190 Bytes
68ba2e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from openai import OpenAI
import threading

pause_event = threading.Event()
resume_event = threading.Event()

def predict(message, history, character, api_key, progress=gr.Progress()):
    client = OpenAI(api_key=api_key)
    history_openai_format = []
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human})
        history_openai_format.append({"role": "assistant", "content": assistant})
    history_openai_format.append({"role": "user", "content": message})

    response = client.chat.completions.create(
        model='gpt-4o',
        messages=history_openai_format,
        temperature=1.0,
        stream=True
    )

    partial_message = ""
    for chunk in progress.tqdm(response, desc="Generating"):
        while pause_event.is_set():
            resume_event.wait()
        if chunk.choices[0].delta.content:
            partial_message += chunk.choices[0].delta.content
            yield partial_message

def pause():
    pause_event.set()
    resume_event.clear()

def resume():
    pause_event.clear()
    resume_event.set()

def reset(character):
    return [], []

# Gradio app
with gr.Blocks() as demo:
    gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{'My Chatbot'}</h1>")
    bot = gr.Chatbot(render=False)
    dropdown = gr.Dropdown(
        ["Character 1", "Character 2", "Character 3", "Character 4", "Character 5", "Character 6", "Character 7", "Character 8", "Character 9", "Character 10", "Character 11", "Character 12", "Character 13"],
        label="Characters",
        info="Select the character that you'd like to speak to",
        value="Character 1"
    )
    api_key_input = gr.Textbox(label="API Key")
    
    chat = gr.ChatInterface(
        fn=predict,
        chatbot=bot,
        additional_inputs=[dropdown, api_key_input],
    )
    dropdown.change(fn=reset, inputs=dropdown, outputs=[bot, chat.chatbot_state])
    
    pause_button = gr.Button("Pause")
    resume_button = gr.Button("Resume")
    
    pause_button.click(fn=pause, inputs=None, outputs=None)
    resume_button.click(fn=resume, inputs=None, outputs=None)

demo.queue()
demo.launch()