Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,71 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
for user_msg, bot_msg in history:
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
bot_reply = f"⚠️ Error: {e}"
|
| 25 |
|
| 26 |
history.append((message, bot_reply))
|
| 27 |
return history, history
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 30 |
-
gr.Markdown("# 🤖
|
|
|
|
| 31 |
chatbot = gr.Chatbot([], height=400)
|
| 32 |
-
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 33 |
clear = gr.Button("Clear Chat")
|
| 34 |
|
| 35 |
-
msg.submit(
|
| 36 |
clear.click(lambda: [], outputs=[chatbot])
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
demo.launch()
|
|
|
|
| 1 |
+
# Import required libraries
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# -------------------------
|
| 7 |
+
# 🛠️ Poe API Setup
|
| 8 |
+
# -------------------------
|
| 9 |
|
| 10 |
+
# Get Poe API key from Hugging Face Secrets
|
| 11 |
+
POE_API_KEY = os.environ.get("1M-vj81pjfDeddkHOtcovh-NZb_ltZJLNojrm_Px5oI")
|
| 12 |
|
| 13 |
+
if not POE_API_KEY:
|
| 14 |
+
raise ValueError("POE_API_KEY is missing. Add it in HF Spaces → Settings → Secrets.")
|
| 15 |
|
| 16 |
+
# Create Poe client (Poe uses OpenAI-compatible API)
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
api_key=POE_API_KEY,
|
| 19 |
+
base_url="https://api.poe.com/v1"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# -------------------------
|
| 23 |
+
# 💬 Chat Function
|
| 24 |
+
# -------------------------
|
| 25 |
+
|
| 26 |
+
def chat_with_poe(history, message):
|
| 27 |
+
messages = []
|
| 28 |
+
|
| 29 |
+
# Convert Gradio history to Poe / OpenAI format
|
| 30 |
for user_msg, bot_msg in history:
|
| 31 |
+
messages.append({"role": "user", "content": user_msg})
|
| 32 |
+
if bot_msg:
|
| 33 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 34 |
|
| 35 |
+
# Add new user message
|
| 36 |
+
messages.append({"role": "user", "content": message})
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
response = client.chat.completions.create(
|
| 40 |
+
model="EchoForge_20", # EXACT Poe bot name
|
| 41 |
+
messages=messages
|
| 42 |
+
)
|
| 43 |
+
bot_reply = response.choices[0].message.content.strip()
|
| 44 |
except Exception as e:
|
| 45 |
bot_reply = f"⚠️ Error: {e}"
|
| 46 |
|
| 47 |
history.append((message, bot_reply))
|
| 48 |
return history, history
|
| 49 |
|
| 50 |
+
# -------------------------
|
| 51 |
+
# 🖼️ Gradio UI
|
| 52 |
+
# -------------------------
|
| 53 |
+
|
| 54 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 55 |
+
gr.Markdown("# 🤖 Python Tutor Bot")
|
| 56 |
+
|
| 57 |
chatbot = gr.Chatbot([], height=400)
|
| 58 |
+
msg = gr.Textbox(
|
| 59 |
+
placeholder="Type your message and press Enter...",
|
| 60 |
+
show_label=False
|
| 61 |
+
)
|
| 62 |
clear = gr.Button("Clear Chat")
|
| 63 |
|
| 64 |
+
msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])
|
| 65 |
clear.click(lambda: [], outputs=[chatbot])
|
| 66 |
|
| 67 |
+
# -------------------------
|
| 68 |
+
# 🚀 Launch App
|
| 69 |
+
# -------------------------
|
| 70 |
+
|
| 71 |
demo.launch()
|