File size: 1,436 Bytes
c946ef8
 
 
 
 
 
 
 
 
 
47d626a
c946ef8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b859f61
c946ef8
47d626a
c946ef8
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
import os


# Load openai key
openai.api_key = os.getenv('OPENAI_KEY')

# Initialize message history array
message_history = []
initial_message = "Please write your prompt here and press 'enter'"

# Create function to process prompt and append previous prompts as "context"
def predict_prompt(input):

    global message_history
    message_history.append({"role": "user", "content": input})
    create_prompt = openai.ChatCompletion.create(
        model = "gpt-3.5-turbo",
        messages = message_history
    )

    reply_prompt = create_prompt.choices[0].message.content
    # print(reply_prompt)

    # Append answer as assistant reply to keep history of prompts
    message_history.append({"role": "assistant", "content": reply_prompt}) 
    response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(0, len(message_history) -1, 2)]

    return response

# Create UI using gradio
with gr.Blocks(theme='abidlabs/dracula_test') as chatblock:

    gr.Markdown("<h1><center>Welcome to my personal AI assistant (powered by OpenAI)</center></h1>")

    Chatbot = gr.Chatbot()
    with gr.Row():
        txt = gr.Textbox(
            show_label=False, 
            placeholder = initial_message).style(container=False)
        state = gr.State()
        txt.submit(predict_prompt, txt, Chatbot)
        txt.submit(None, None, txt, _js="() => {''}")

chatblock.launch()