Spaces:
Sleeping
Sleeping
File size: 1,257 Bytes
7b534cf a2ca184 ddeec5f a2ca184 ddeec5f a2ca184 |
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 |
import os
import gradio as gr
import random
import time
from pydantic import BaseModel
import openai
openai.api_key = os.environ["apikey"]
def chatgpt(input, chat_history):
try:
messages = []
for message, bot_message in chat_history:
messages.append({"role": "user", "content": message})
messages.append({"role": "assistant", "content": bot_message})
prompt = {"role": "user", "content": input}
messages.append(prompt)
result = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
output = result.get('choices')[0].get('message').get('content')
except Exception as e:
output = str(e)
return output
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def respond(message, chat_history):
# bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
bot_message = chatgpt(message, chat_history)
chat_history.append((message, bot_message))
time.sleep(1)
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch(server_name='0.0.0.0') |