Spaces:
Sleeping
Sleeping
| # Import required libraries | |
| import gradio as gr | |
| from openai import OpenAI | |
| # ------------------------- | |
| # 🛠️ Poe API Setup | |
| # ------------------------- | |
| POE_API_KEY = "1M-vj81pjfDeddkHOtcovh-NZb_ltZJLNojrm_Px5oIY" | |
| # Create Poe client (Poe uses OpenAI-compatible API) | |
| client = OpenAI( | |
| api_key=POE_API_KEY, | |
| base_url="https://api.poe.com/v1" | |
| ) | |
| # ------------------------- | |
| # 💬 Chat Function | |
| # ------------------------- | |
| def chat_with_poe(history, message): | |
| messages = [] | |
| # Convert Gradio history → OpenAI format | |
| for user_msg, bot_msg in history: | |
| messages.append({"role": "user", "content": user_msg}) | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| try: | |
| response = client.chat.completions.create( | |
| model="EchoForge_20", | |
| messages=messages | |
| ) | |
| bot_reply = response.choices[0].message.content | |
| except Exception as e: | |
| bot_reply = f"⚠️ Error: {e}" | |
| history.append((message, bot_reply)) | |
| return history | |
| # ------------------------- | |
| # 🖼️ Gradio UI | |
| # ------------------------- | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🤖 test_chatbot") | |
| chatbot = gr.Chatbot([], height=400) | |
| msg = gr.Textbox( | |
| placeholder="Type your message and press Enter...", | |
| show_label=False | |
| ) | |
| clear = gr.Button("Clear Chat") | |
| msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot]) | |
| clear.click(lambda: [], outputs=[chatbot]) | |
| # ------------------------- | |
| # 🚀 Launch App | |
| # ------------------------- | |
| demo.launch() | |