Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
# -------------------------
|
| 6 |
+
# Poe API Setup
|
| 7 |
+
# -------------------------
|
| 8 |
+
POE_API_KEY = os.environ["POE_API_KEY"] # Stored in HF Spaces Secrets
|
| 9 |
+
|
| 10 |
+
client = openai.OpenAI(
|
| 11 |
+
api_key=POE_API_KEY,
|
| 12 |
+
base_url="https://api.poe.com/v1"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# -------------------------
|
| 16 |
+
# Chat function
|
| 17 |
+
# -------------------------
|
| 18 |
+
def chat_with_poe(history, message):
|
| 19 |
+
"""
|
| 20 |
+
history: list of tuples [(user, bot), ...]
|
| 21 |
+
message: latest user input
|
| 22 |
+
"""
|
| 23 |
+
# Convert Gradio's history to Poe's format
|
| 24 |
+
messages = []
|
| 25 |
+
for user_msg, bot_msg in history:
|
| 26 |
+
messages.append({"role": "user", "content": user_msg})
|
| 27 |
+
if bot_msg:
|
| 28 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
+
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
response = client.chat.completions.create(
|
| 34 |
+
model="TechBuddyAI", # Your Poe bot name
|
| 35 |
+
messages=messages
|
| 36 |
+
)
|
| 37 |
+
bot_reply = response.choices[0].message.content
|
| 38 |
+
except Exception as e:
|
| 39 |
+
bot_reply = f"⚠️ Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
history.append((message, bot_reply))
|
| 42 |
+
return history, history
|
| 43 |
+
|
| 44 |
+
# -------------------------
|
| 45 |
+
# Gradio Chat UI
|
| 46 |
+
# -------------------------
|
| 47 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"""
|
| 50 |
+
# 🤖 Tech Buddy AI
|
| 51 |
+
Your friendly tech sidekick — here to make tech simple & fun!
|
| 52 |
+
Type below to start chatting.
|
| 53 |
+
"""
|
| 54 |
+
)
|
| 55 |
+
chatbot = gr.Chatbot([], height=400)
|
| 56 |
+
msg = gr.Textbox(placeholder="Type your message...")
|
| 57 |
+
clear = gr.Button("Clear Chat")
|
| 58 |
+
|
| 59 |
+
msg.submit(chat_with_poe, [chatbot, msg], [chatbot, chatbot])
|
| 60 |
+
clear.click(lambda: [], None, chatbot)
|
| 61 |
+
|
| 62 |
+
# -------------------------
|
| 63 |
+
# Launch App
|
| 64 |
+
# -------------------------
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|