huohguohbo's picture
Update app.py
ade71a8
raw
history blame
1.35 kB
import openai
import gradio as gr
# Define the chat function
def chat(api_key, model, message):
# Check if an API key has been provided
if api_key is None:
return "Please enter your OpenAI API key and try again."
# Set up the OpenAI API request
response = openai.Completion.create(
engine=model,
prompt=message,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
api_key=api_key,
)
# Extract the bot's response from the API request
bot_response = response.choices[0].text.strip()
return bot_response
# Define the Gradio interface
api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
model_input = gr.inputs.Dropdown(
label="Select model",
choices=["text-davinci-003", "text-davinci-002"],
default="text-davinci-003",
)
message_input = gr.inputs.Textbox(label="Enter your message here")
output = gr.outputs.Textbox(label="Bot response")
chat_button = gr.Interface(
fn=chat,
inputs=[api_key_input, model_input, message_input],
outputs=output,
title="OpenAI Chatbot",
description="Enter your message below to chat with an AI",
theme="huggingface",
layout="vertical",
allow_flagging=False,
allow_screenshot=False,
allow_share=False,
)
# Launch the app
chat_button.launch()