Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from poe_api_wrapper import PoeApi
|
4 |
+
from typing import Generator
|
5 |
+
|
6 |
+
def init_client(p_b: str, p_lat: str) -> PoeApi:
|
7 |
+
return PoeApi(token={"p-b": p_b, "p-lat": p_lat})
|
8 |
+
|
9 |
+
custom_css = """
|
10 |
+
#component-0 {
|
11 |
+
background: linear-gradient(135deg, #8B5CF6 0%, #FCD34D 100%);
|
12 |
+
padding: 20px;
|
13 |
+
border-radius: 15px;
|
14 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
15 |
+
}
|
16 |
+
.gradio-container {
|
17 |
+
background: linear-gradient(135deg, #8B5CF6 0%, #FCD34D 100%);
|
18 |
+
}
|
19 |
+
.chat-message {
|
20 |
+
padding: 15px;
|
21 |
+
margin: 5px;
|
22 |
+
border-radius: 10px;
|
23 |
+
background-color: rgba(255, 255, 255, 0.9);
|
24 |
+
}
|
25 |
+
.assistant-message {
|
26 |
+
border-left: 4px solid #8B5CF6;
|
27 |
+
}
|
28 |
+
.user-message {
|
29 |
+
border-left: 4px solid #FCD34D;
|
30 |
+
}
|
31 |
+
"""
|
32 |
+
|
33 |
+
def chat_stream(client: PoeApi, message: str, bot: str, history: list) -> Generator:
|
34 |
+
response = ""
|
35 |
+
for chunk in client.send_message(bot, message):
|
36 |
+
response += chunk["response"]
|
37 |
+
yield response
|
38 |
+
|
39 |
+
def create_app():
|
40 |
+
with gr.Blocks(css=custom_css) as app:
|
41 |
+
gr.Markdown("# 🤖 Poe Chat Interface")
|
42 |
+
|
43 |
+
with gr.Row():
|
44 |
+
with gr.Column():
|
45 |
+
p_b = gr.Textbox(label="P-B Token", type="password")
|
46 |
+
p_lat = gr.Textbox(label="P-LAT Token", type="password")
|
47 |
+
bot_select = gr.Dropdown(
|
48 |
+
choices=["Claude-3-Opus", "Claude-3-Sonnet", "GPT-4", "GPT-3.5-Turbo", "Claude-2", "Google-PaLM"],
|
49 |
+
label="Select Bot",
|
50 |
+
value="Claude-3-Opus"
|
51 |
+
)
|
52 |
+
connect_btn = gr.Button("Connect", variant="primary")
|
53 |
+
|
54 |
+
chatbot = gr.Chatbot(
|
55 |
+
[],
|
56 |
+
elem_classes=["chat-message", "assistant-message", "user-message"],
|
57 |
+
height=500
|
58 |
+
)
|
59 |
+
msg = gr.Textbox(
|
60 |
+
label="Type your message here...",
|
61 |
+
placeholder="Enter your message and press enter...",
|
62 |
+
lines=3
|
63 |
+
)
|
64 |
+
clear = gr.Button("Clear Chat")
|
65 |
+
|
66 |
+
client = {"instance": None}
|
67 |
+
|
68 |
+
def connect(p_b_val: str, p_lat_val: str) -> str:
|
69 |
+
try:
|
70 |
+
client["instance"] = init_client(p_b_val, p_lat_val)
|
71 |
+
return "✅ Successfully connected!"
|
72 |
+
except Exception as e:
|
73 |
+
return f"❌ Connection failed: {str(e)}"
|
74 |
+
|
75 |
+
def respond(message: str, chat_history: list) -> tuple:
|
76 |
+
if not client["instance"]:
|
77 |
+
return chat_history + [[message, "Please connect to Poe first!"]]
|
78 |
+
|
79 |
+
bot = bot_select.value.lower().replace("-", "").replace(" ", "")
|
80 |
+
history = chat_history + [[message, ""]]
|
81 |
+
|
82 |
+
for response in chat_stream(client["instance"], message, bot, history):
|
83 |
+
history[-1][1] = response
|
84 |
+
yield history
|
85 |
+
|
86 |
+
def clear_chat() -> tuple:
|
87 |
+
return [], []
|
88 |
+
|
89 |
+
connect_btn.click(connect, [p_b, p_lat], gr.Textbox(label="Connection Status"))
|
90 |
+
msg.submit(respond, [msg, chatbot], [chatbot])
|
91 |
+
clear.click(clear_chat, None, [chatbot, msg])
|
92 |
+
|
93 |
+
return app
|
94 |
+
|
95 |
+
demo = create_app()
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
demo.launch()
|