File size: 1,225 Bytes
b7c7b2d a943362 2293c14 a943362 9817165 a943362 6d5283f a943362 6d5283f a943362 6d5283f a943362 6d5283f baa3d07 9817165 6d5283f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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()
|