GPT5_Demo / app.py
Abs6187's picture
Update app.py
a943362 verified
raw
history blame
1.23 kB
import gradio as gr
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
base_url="https://api.chatanywhere.tech/v1"
)
def respond(user_message, history):
history = history or []
messages = [{"role": "system", "content": "You are a helpful assistant."}]
for human, ai in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": ai})
messages.append({"role": "user", "content": user_message})
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
bot_reply = response.choices[0].message.content
except Exception as e:
bot_reply = f"❌ Error: {e}"
history.append((user_message, bot_reply))
return history
chat = gr.Chatbot(
label="Conversation",
avatar_images=(None, None),
bubble_full_width=False,
height=420,
show_copy_button=True,
render_markdown=True
)
with gr.Blocks() as demo:
chatbot = chat
msg = gr.Textbox(placeholder="Type your message here...")
msg.submit(respond, [msg, chatbot], chatbot)
if __name__ == "__main__":
demo.launch()