Spaces:
Sleeping
Sleeping
Commit
·
ddeec5f
1
Parent(s):
d915dcd
Update main_gradio.py
Browse files- main_gradio.py +20 -1
main_gradio.py
CHANGED
@@ -1,6 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
with gr.Blocks() as demo:
|
6 |
chatbot = gr.Chatbot()
|
@@ -8,7 +26,8 @@ with gr.Blocks() as demo:
|
|
8 |
clear = gr.Button("Clear")
|
9 |
|
10 |
def respond(message, chat_history):
|
11 |
-
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
|
|
12 |
chat_history.append((message, bot_message))
|
13 |
time.sleep(1)
|
14 |
return "", chat_history
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import time
|
4 |
+
from pydantic import BaseModel
|
5 |
+
import openai
|
6 |
+
openai.api_key = os.environ["apikey"]
|
7 |
+
|
8 |
+
def chatgpt(input, chat_history):
|
9 |
+
try:
|
10 |
+
messages = []
|
11 |
+
for message, bot_message in chat_history:
|
12 |
+
messages.append({"role": "user", "content": message})
|
13 |
+
messages.append({"role": "assistant", "content": bot_message})
|
14 |
+
prompt = {"role": "user", "content": input}
|
15 |
+
messages.append(prompt)
|
16 |
+
result = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
17 |
+
output = result.get('choices')[0].get('message').get('content')
|
18 |
+
except Exception as e:
|
19 |
+
output = str(e)
|
20 |
+
return output
|
21 |
+
|
22 |
|
23 |
with gr.Blocks() as demo:
|
24 |
chatbot = gr.Chatbot()
|
|
|
26 |
clear = gr.Button("Clear")
|
27 |
|
28 |
def respond(message, chat_history):
|
29 |
+
# bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
30 |
+
bot_message = chatgpt(message, chat_history)
|
31 |
chat_history.append((message, bot_message))
|
32 |
time.sleep(1)
|
33 |
return "", chat_history
|