Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,6 @@ import gradio as gr
|
|
| 4 |
|
| 5 |
BASE_URL = "https://router.huggingface.co/v1"
|
| 6 |
|
| 7 |
-
# ----------------------------
|
| 8 |
-
# ChatGPT-like UI + theme
|
| 9 |
-
# ----------------------------
|
| 10 |
CSS = """
|
| 11 |
:root{
|
| 12 |
--bg:#0b0f19;
|
|
@@ -17,7 +14,6 @@ CSS = """
|
|
| 17 |
--border:rgba(255,255,255,.10);
|
| 18 |
--shadow: 0 20px 60px rgba(0,0,0,.45);
|
| 19 |
}
|
| 20 |
-
|
| 21 |
body.light{
|
| 22 |
--bg:#f6f7fb;
|
| 23 |
--panel:rgba(0,0,0,.04);
|
|
@@ -27,70 +23,36 @@ body.light{
|
|
| 27 |
--border:rgba(0,0,0,.10);
|
| 28 |
--shadow: 0 20px 60px rgba(0,0,0,.12);
|
| 29 |
}
|
| 30 |
-
|
| 31 |
.gradio-container{
|
| 32 |
background: radial-gradient(1200px 700px at 20% 10%, rgba(98, 123, 255, .22), transparent 60%),
|
| 33 |
radial-gradient(1000px 700px at 80% 30%, rgba(255, 90, 150, .18), transparent 60%),
|
| 34 |
var(--bg) !important;
|
| 35 |
color: var(--text) !important;
|
| 36 |
}
|
| 37 |
-
|
| 38 |
-
#wrap{
|
| 39 |
-
max-width: 980px;
|
| 40 |
-
margin: 22px auto;
|
| 41 |
-
padding: 0 14px;
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
.header{
|
| 45 |
-
display:flex;
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
padding: 18px 14px;
|
| 49 |
-
border: 1px solid var(--border);
|
| 50 |
-
border-radius: 18px;
|
| 51 |
-
background: var(--panel);
|
| 52 |
-
box-shadow: var(--shadow);
|
| 53 |
}
|
| 54 |
-
|
| 55 |
.block{
|
| 56 |
-
border: 1px solid var(--border);
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
box-shadow: var(--shadow);
|
| 60 |
-
padding: 14px;
|
| 61 |
-
margin-top: 14px;
|
| 62 |
}
|
| 63 |
-
|
| 64 |
.subtle{ color: var(--muted); }
|
| 65 |
-
|
| 66 |
.chat_shell{
|
| 67 |
-
border: 1px solid var(--border);
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
box-shadow: var(--shadow);
|
| 71 |
-
padding: 10px;
|
| 72 |
-
margin-top: 14px;
|
| 73 |
}
|
| 74 |
-
|
| 75 |
-
.composer{
|
| 76 |
-
margin-top: 10px;
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
.primary button, .secondary button{
|
| 80 |
border-radius: 14px !important;
|
| 81 |
border: 1px solid var(--border) !important;
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
.
|
| 85 |
-
background: rgba(98, 123, 255, .85) !important;
|
| 86 |
-
color: white !important;
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
.secondary button{
|
| 90 |
-
background: var(--panel2) !important;
|
| 91 |
-
color: var(--text) !important;
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
footer{ display:none !important; }
|
| 95 |
"""
|
| 96 |
|
|
@@ -167,16 +129,12 @@ def chat_call(headers, model, messages, temperature, max_tokens):
|
|
| 167 |
# ----------------------------
|
| 168 |
# Message utilities
|
| 169 |
# ----------------------------
|
| 170 |
-
MAX_TURNS = 14 #
|
| 171 |
|
| 172 |
def trimmed_messages(msgs):
|
| 173 |
-
|
| 174 |
-
if not msgs:
|
| 175 |
-
return []
|
| 176 |
-
return msgs[-MAX_TURNS * 2 :]
|
| 177 |
|
| 178 |
def build_messages(system_prompt, msgs, user_msg):
|
| 179 |
-
"""Build request payload messages (system + recent turns + new user)."""
|
| 180 |
return (
|
| 181 |
[{"role": "system", "content": system_prompt}]
|
| 182 |
+ trimmed_messages(msgs)
|
|
@@ -184,11 +142,8 @@ def build_messages(system_prompt, msgs, user_msg):
|
|
| 184 |
)
|
| 185 |
|
| 186 |
def ui_messages_from_state(msgs):
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
[{"role":"user"/"assistant", "content":"..."}, ...]
|
| 190 |
-
We hide "system" from the UI.
|
| 191 |
-
"""
|
| 192 |
out = []
|
| 193 |
for m in msgs or []:
|
| 194 |
role = m.get("role")
|
|
@@ -220,7 +175,7 @@ with gr.Blocks(title="Chat") as demo:
|
|
| 220 |
|
| 221 |
model_override = gr.Textbox(
|
| 222 |
label="Model override (optional)",
|
| 223 |
-
placeholder="Paste a chat-capable model id here (takes precedence
|
| 224 |
)
|
| 225 |
|
| 226 |
system_prompt = gr.Textbox(label="System prompt", value="You are a helpful assistant.")
|
|
@@ -229,10 +184,10 @@ with gr.Blocks(title="Chat") as demo:
|
|
| 229 |
max_tokens = gr.Slider(64, 2048, value=800, step=32, label="Max tokens")
|
| 230 |
|
| 231 |
with gr.Column(elem_classes=["chat_shell"]):
|
| 232 |
-
|
|
|
|
| 233 |
|
| 234 |
-
|
| 235 |
-
msg_state = gr.State([])
|
| 236 |
|
| 237 |
with gr.Column(elem_classes=["composer"]):
|
| 238 |
msg = gr.Textbox(placeholder="Message…", show_label=False, lines=1)
|
|
@@ -249,7 +204,6 @@ with gr.Blocks(title="Chat") as demo:
|
|
| 249 |
if err:
|
| 250 |
return f"❌ {err}", gr.update(choices=[], value=None)
|
| 251 |
|
| 252 |
-
# Choose a default if available
|
| 253 |
default = models[0] if models else None
|
| 254 |
return f"✅ Models loaded ({len(models)}).", gr.update(choices=models, value=default)
|
| 255 |
|
|
@@ -260,7 +214,6 @@ with gr.Blocks(title="Chat") as demo:
|
|
| 260 |
user_msg = (user_msg or "").strip()
|
| 261 |
msgs = msgs or []
|
| 262 |
if not user_msg:
|
| 263 |
-
# No-op; keep UI/state unchanged
|
| 264 |
return "", ui_messages_from_state(msgs), msgs
|
| 265 |
|
| 266 |
headers, err = get_headers()
|
|
@@ -306,5 +259,5 @@ with gr.Blocks(title="Chat") as demo:
|
|
| 306 |
)
|
| 307 |
clear.click(do_clear, outputs=[chatbot, msg_state])
|
| 308 |
|
| 309 |
-
#
|
| 310 |
demo.launch(css=CSS)
|
|
|
|
| 4 |
|
| 5 |
BASE_URL = "https://router.huggingface.co/v1"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
CSS = """
|
| 8 |
:root{
|
| 9 |
--bg:#0b0f19;
|
|
|
|
| 14 |
--border:rgba(255,255,255,.10);
|
| 15 |
--shadow: 0 20px 60px rgba(0,0,0,.45);
|
| 16 |
}
|
|
|
|
| 17 |
body.light{
|
| 18 |
--bg:#f6f7fb;
|
| 19 |
--panel:rgba(0,0,0,.04);
|
|
|
|
| 23 |
--border:rgba(0,0,0,.10);
|
| 24 |
--shadow: 0 20px 60px rgba(0,0,0,.12);
|
| 25 |
}
|
|
|
|
| 26 |
.gradio-container{
|
| 27 |
background: radial-gradient(1200px 700px at 20% 10%, rgba(98, 123, 255, .22), transparent 60%),
|
| 28 |
radial-gradient(1000px 700px at 80% 30%, rgba(255, 90, 150, .18), transparent 60%),
|
| 29 |
var(--bg) !important;
|
| 30 |
color: var(--text) !important;
|
| 31 |
}
|
| 32 |
+
#wrap{ max-width: 980px; margin: 22px auto; padding: 0 14px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
.header{
|
| 34 |
+
display:flex; justify-content:space-between; align-items:center;
|
| 35 |
+
padding: 18px 14px; border: 1px solid var(--border);
|
| 36 |
+
border-radius: 18px; background: var(--panel); box-shadow: var(--shadow);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
|
|
|
| 38 |
.block{
|
| 39 |
+
border: 1px solid var(--border); border-radius: 18px;
|
| 40 |
+
background: var(--panel); box-shadow: var(--shadow);
|
| 41 |
+
padding: 14px; margin-top: 14px;
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
|
|
|
| 43 |
.subtle{ color: var(--muted); }
|
|
|
|
| 44 |
.chat_shell{
|
| 45 |
+
border: 1px solid var(--border); border-radius: 18px;
|
| 46 |
+
background: var(--panel); box-shadow: var(--shadow);
|
| 47 |
+
padding: 10px; margin-top: 14px;
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
+
.composer{ margin-top: 10px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
.primary button, .secondary button{
|
| 51 |
border-radius: 14px !important;
|
| 52 |
border: 1px solid var(--border) !important;
|
| 53 |
}
|
| 54 |
+
.primary button{ background: rgba(98, 123, 255, .85) !important; color: white !important; }
|
| 55 |
+
.secondary button{ background: var(--panel2) !important; color: var(--text) !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
footer{ display:none !important; }
|
| 57 |
"""
|
| 58 |
|
|
|
|
| 129 |
# ----------------------------
|
| 130 |
# Message utilities
|
| 131 |
# ----------------------------
|
| 132 |
+
MAX_TURNS = 14 # last N user+assistant pairs
|
| 133 |
|
| 134 |
def trimmed_messages(msgs):
|
| 135 |
+
return (msgs or [])[-MAX_TURNS * 2 :]
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
def build_messages(system_prompt, msgs, user_msg):
|
|
|
|
| 138 |
return (
|
| 139 |
[{"role": "system", "content": system_prompt}]
|
| 140 |
+ trimmed_messages(msgs)
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
def ui_messages_from_state(msgs):
|
| 145 |
+
# Chatbot (messages format) expects list of dicts with role/content.
|
| 146 |
+
# Hide system messages from UI.
|
|
|
|
|
|
|
|
|
|
| 147 |
out = []
|
| 148 |
for m in msgs or []:
|
| 149 |
role = m.get("role")
|
|
|
|
| 175 |
|
| 176 |
model_override = gr.Textbox(
|
| 177 |
label="Model override (optional)",
|
| 178 |
+
placeholder="Paste a chat-capable model id here (takes precedence).",
|
| 179 |
)
|
| 180 |
|
| 181 |
system_prompt = gr.Textbox(label="System prompt", value="You are a helpful assistant.")
|
|
|
|
| 184 |
max_tokens = gr.Slider(64, 2048, value=800, step=32, label="Max tokens")
|
| 185 |
|
| 186 |
with gr.Column(elem_classes=["chat_shell"]):
|
| 187 |
+
# IMPORTANT: no type=... (your Gradio build doesn't support it)
|
| 188 |
+
chatbot = gr.Chatbot(height=560)
|
| 189 |
|
| 190 |
+
msg_state = gr.State([]) # OpenAI-style list[dict]
|
|
|
|
| 191 |
|
| 192 |
with gr.Column(elem_classes=["composer"]):
|
| 193 |
msg = gr.Textbox(placeholder="Message…", show_label=False, lines=1)
|
|
|
|
| 204 |
if err:
|
| 205 |
return f"❌ {err}", gr.update(choices=[], value=None)
|
| 206 |
|
|
|
|
| 207 |
default = models[0] if models else None
|
| 208 |
return f"✅ Models loaded ({len(models)}).", gr.update(choices=models, value=default)
|
| 209 |
|
|
|
|
| 214 |
user_msg = (user_msg or "").strip()
|
| 215 |
msgs = msgs or []
|
| 216 |
if not user_msg:
|
|
|
|
| 217 |
return "", ui_messages_from_state(msgs), msgs
|
| 218 |
|
| 219 |
headers, err = get_headers()
|
|
|
|
| 259 |
)
|
| 260 |
clear.click(do_clear, outputs=[chatbot, msg_state])
|
| 261 |
|
| 262 |
+
# Keep this if your Gradio build supports css= on launch()
|
| 263 |
demo.launch(css=CSS)
|