huohguohbo's picture
Update app.py
3d1a684
raw
history blame contribute delete
970 Bytes
import openai
import gradio as gr
def chat(api_key, message, model):
if not api_key:
return "Please enter a valid API key."
openai.api_key = api_key
try:
response = openai.Completion.create(
engine=model,
prompt=message,
max_tokens=50,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0].text.strip()
except Exception as e:
return f"Error: {str(e)}"
models = ["gpt-4", "text-davinci-002", "text-curie-002", "text-babbage-002", "text-ada-002"]
iface = gr.Interface(
fn=chat,
inputs=[
gr.inputs.Textbox(lines=1, label="API Key"),
gr.inputs.Textbox(lines=5, label="Message"),
gr.inputs.Dropdown(choices=models, label="Model"),
],
outputs=gr.outputs.Textbox(label="Response"),
title="GPT-4 Chat App",
description="A simple chat app using OpenAI GPT-4 and Gradio.",
)
iface.launch()