Spaces:
Running
Running
import os | |
import openai | |
import gradio as gr | |
openai.api_key = os.environ["OPENAPI_API_KEY"] | |
messages = [ | |
{"role": "system", "content": ""}, | |
] | |
def chatbot(input): | |
if input: | |
messages.append({"role": "user", "content": input}) | |
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages]) | |
chat = openai.Completion.create( | |
engine="text-davinci-003", prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.7 | |
) | |
reply = chat.choices[0].text.strip() | |
messages.append({"role": "assistant", "content": reply}) | |
return reply | |
inputs = gr.inputs.Textbox(lines=7, label="Query") | |
outputs = gr.outputs.Textbox(label="Response") | |
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, | |
theme="compact").launch() | |