Spaces:
Sleeping
Sleeping
Alexis Roldan
commited on
Commit
·
c946ef8
1
Parent(s):
af9038a
Chatbot Update
Browse files- chatgui.py +49 -0
- requirements.txt +2 -0
chatgui.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load package to use .env variables
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Load openai key
|
10 |
+
openai.api_key = os.getenv('OPENAI_KEY')
|
11 |
+
|
12 |
+
# Initialize message history array
|
13 |
+
message_history = []
|
14 |
+
initial_message = "Please write here your prompt and press 'enter'"
|
15 |
+
|
16 |
+
# Create function to process prompt and append previous prompts as "context"
|
17 |
+
def predict_prompt(input):
|
18 |
+
|
19 |
+
global message_history
|
20 |
+
message_history.append({"role": "user", "content": input})
|
21 |
+
create_prompt = openai.ChatCompletion.create(
|
22 |
+
model = "gpt-3.5-turbo",
|
23 |
+
messages = message_history
|
24 |
+
)
|
25 |
+
|
26 |
+
reply_prompt = create_prompt.choices[0].message.content
|
27 |
+
# print(reply_prompt)
|
28 |
+
|
29 |
+
# Append answer as assistant reply to keep history of prompts
|
30 |
+
message_history.append({"role": "assistant", "content": reply_prompt})
|
31 |
+
response = [(message_history[i]["content"], message_history[i+1]["content"]) for i in range(0, len(message_history) -1, 2)]
|
32 |
+
|
33 |
+
return response
|
34 |
+
|
35 |
+
# Create UI using gradio
|
36 |
+
with gr.Blocks() as chatblock:
|
37 |
+
|
38 |
+
gr.Markdown("<h1><center>Welcome to Alexis' Personal AI Assistant (powered by OpenAI API)</center></h1>")
|
39 |
+
|
40 |
+
Chatbot = gr.Chatbot()
|
41 |
+
with gr.Row():
|
42 |
+
txt = gr.Textbox(
|
43 |
+
show_label=False,
|
44 |
+
placeholder = initial_message).style(container=False)
|
45 |
+
state = gr.State()
|
46 |
+
txt.submit(predict_prompt, txt, Chatbot)
|
47 |
+
txt.submit(None, None, txt, _js="() => {''}")
|
48 |
+
|
49 |
+
chatblock.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==3.22.1
|
2 |
+
openai==0.27.2
|