File size: 1,348 Bytes
f90c66a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai

openai.api_key = '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'
message_history = []

# message_history = [
#     {
#         "role": "user",
#         "content": f"You are a joke bot, but I'll specify a subject matter in messages, and you'll reply with a jokes that includes the subjects I mention in my messages. Reply only with jokes to further input, If you understand, say OK."},
#     {
#         "role": "assistant",
#         "content": f"OK"
#     } 
# ]

def predict(input):
    global message_history
    message_history.append({'role': 'user', 'content': input})
    completion = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        messages = message_history
    )
    replContent = completion.choices[0].message.content
    print(replContent)
    message_history.append({'role': 'assistant', 'content': replContent })
    response = [(message_history[i]['content'], message_history[i+1]['content']) for i in range(0, len(message_history)-1, 2)]
    return response

with gr.Blocks() as d:
    chatbot = gr.Chatbot()
    with gr.Row():
        textbox = gr.Textbox(show_lable = False, placeholder = "Type your message here").style(container = False)
        textbox.submit(predict, textbox, chatbot)
        textbox.submit(None, None, textbox, _js = "() => {''}")

d.launch()