chatgpt-clone / app.py
Blane187's picture
Update app.py
757b7a1 verified
import openai
import gradio as gr
import os
# Set OpenAI API key from environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
# Define a function to interact with OpenAI's ChatGPT
def chat_with_openai(input_text, gpt_model):
try:
response = openai.Completion.create(
engine=gpt_model, # Use the model you want (e.g., 'gpt-3.5-turbo', 'gpt-4')
prompt=input_text,
max_tokens=150,
n=1,
stop=None,
temperature=0.7,
)
answer = response.choices[0].text.strip()
return answer
except Exception as e:
return str(e)
def on_submit(message, gpt_model, chat_history):
response = chat_with_openai(message, gpt_model)
chat_history.append((message, response))
return chat_history, ""
# Define the Gradio interface
with gr.Blocks(theme="Hev832/niceandsimple") as ui:
gr.Markdown("<h1><center> ChatGPT Clone")
chatbot = gr.Chatbot(label="OpenAI Chatbot")
with gr.Row():
msg = gr.Textbox(label="Enter your message here:")
model = gr.Dropdown(["gpt-4o-mini", "gpt-3.5-turbo", "gpt-4o"], label="Your GPT model")
submit_btn = gr.Button("Submit")
submit_btn.click(on_submit, inputs=[msg, model, chatbot], outputs=[chatbot, msg])
# Launch the Gradio app
ui.launch()