Spaces:
Runtime error
Runtime error
import os | |
import openai | |
import gradio as gr | |
openai.ChatCompletion.create( | |
model="gpt-3.5-turbo0301", | |
# Set OpenAI API key from environment variable | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
# Define GPT-3.5 Turbo model | |
model = "gpt-3.5-turbo-0301" | |
# Define function for generating responses | |
def generate_response(prompt): | |
try: | |
response = openai.Completion.create( | |
engine=model, | |
prompt=prompt, | |
max_tokens=1024, | |
n=1, | |
stop=None, | |
temperature=0.7, | |
) | |
return response.choices[0].text.strip() | |
except Exception as e: | |
return f"Error: {e}" | |
# Define input and output interfaces | |
input_text = gr.inputs.Textbox(label="Enter your message:") | |
output_text = gr.outputs.Textbox(label="AI response:") | |
# Define Gradio app | |
gradio_app = gr.Interface( | |
fn=generate_response, | |
inputs=input_text, | |
outputs=output_text, | |
title="GPT-3.5 Turbo Chatbot", | |
description="Enter a message and get a response from an AI chatbot powered by GPT-3.5 Turbo.", | |
theme="compact", | |
) | |
# Launch Gradio app | |
gradio_app.launch(share=True) |