Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -169,7 +169,9 @@ def respond(message, chat_history, model, system_prompt, temperature, max_tokens
|
|
| 169 |
stream=True,
|
| 170 |
)
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
| 173 |
for chunk in stream:
|
| 174 |
if not chunk.choices:
|
| 175 |
# some providers send trailing chunks with no `choices`
|
|
@@ -223,11 +225,73 @@ def clear_all():
|
|
| 223 |
return [], None, make_preview_html(None)
|
| 224 |
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
# ---------------------------------------------------------------------------
|
| 227 |
# UI
|
| 228 |
# ---------------------------------------------------------------------------
|
| 229 |
|
| 230 |
-
with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
| 231 |
gr.Markdown("## 🤖 Chatterbox — чат + генерация файлов кода")
|
| 232 |
|
| 233 |
if not HF_TOKEN:
|
|
@@ -239,13 +303,13 @@ with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft()) as demo:
|
|
| 239 |
"Providers*), затем перезапустите Space."
|
| 240 |
)
|
| 241 |
|
| 242 |
-
with gr.Row():
|
| 243 |
-
with gr.Column(scale=3):
|
| 244 |
chatbot = gr.Chatbot(
|
| 245 |
type="messages",
|
| 246 |
show_label=False,
|
| 247 |
show_copy_button=True,
|
| 248 |
-
height=
|
| 249 |
)
|
| 250 |
msg = gr.Textbox(
|
| 251 |
placeholder="Опишите, что построить, например: «сделай лендинг с формой подписки»",
|
|
@@ -256,7 +320,7 @@ with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft()) as demo:
|
|
| 256 |
send_btn = gr.Button("Отправить", variant="primary")
|
| 257 |
clear_btn = gr.Button("Очистить")
|
| 258 |
|
| 259 |
-
with gr.Column(scale=2):
|
| 260 |
with gr.Tab("📄 Файлы"):
|
| 261 |
files_out = gr.File(
|
| 262 |
label="Сгенерированные файлы (несколько — упаковываются в .zip)",
|
|
@@ -277,6 +341,12 @@ with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft()) as demo:
|
|
| 277 |
max_tokens = gr.Slider(128, 8192, value=2048, step=64, label="Max new tokens")
|
| 278 |
top_p = gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
inputs = [msg, chatbot, model_dd, system_box, temperature, max_tokens, top_p]
|
| 281 |
outputs = [chatbot, msg, files_out, preview]
|
| 282 |
|
|
|
|
| 169 |
stream=True,
|
| 170 |
)
|
| 171 |
|
| 172 |
+
header = f"_Модель: `{model}`_\n\n"
|
| 173 |
+
partial = header
|
| 174 |
+
chat_history[-1]["content"] = partial
|
| 175 |
for chunk in stream:
|
| 176 |
if not chunk.choices:
|
| 177 |
# some providers send trailing chunks with no `choices`
|
|
|
|
| 225 |
return [], None, make_preview_html(None)
|
| 226 |
|
| 227 |
|
| 228 |
+
DEFAULT_SETTINGS = {
|
| 229 |
+
"model": MODEL_CHOICES[0],
|
| 230 |
+
"system_prompt": DEFAULT_SYSTEM_PROMPT,
|
| 231 |
+
"temperature": 0.7,
|
| 232 |
+
"max_tokens": 2048,
|
| 233 |
+
"top_p": 0.9,
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def load_settings(saved):
|
| 238 |
+
s = {**DEFAULT_SETTINGS, **(saved or {})}
|
| 239 |
+
return (
|
| 240 |
+
gr.update(value=s["model"]),
|
| 241 |
+
gr.update(value=s["system_prompt"]),
|
| 242 |
+
gr.update(value=s["temperature"]),
|
| 243 |
+
gr.update(value=s["max_tokens"]),
|
| 244 |
+
gr.update(value=s["top_p"]),
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def save_settings(model, system_prompt, temperature, max_tokens, top_p):
|
| 249 |
+
return {
|
| 250 |
+
"model": model,
|
| 251 |
+
"system_prompt": system_prompt,
|
| 252 |
+
"temperature": temperature,
|
| 253 |
+
"max_tokens": max_tokens,
|
| 254 |
+
"top_p": top_p,
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
# Thin, rounded, theme-colored scrollbars instead of the bulky default OS
|
| 259 |
+
# ones, plus a layout that fits the viewport instead of forcing page scroll.
|
| 260 |
+
CUSTOM_CSS = """
|
| 261 |
+
:root {
|
| 262 |
+
--sb-thumb: #8a8fb8;
|
| 263 |
+
--sb-track: transparent;
|
| 264 |
+
}
|
| 265 |
+
@media (prefers-color-scheme: dark) {
|
| 266 |
+
:root { --sb-thumb: #55597a; }
|
| 267 |
+
}
|
| 268 |
+
* {
|
| 269 |
+
scrollbar-width: thin;
|
| 270 |
+
scrollbar-color: var(--sb-thumb) var(--sb-track);
|
| 271 |
+
}
|
| 272 |
+
*::-webkit-scrollbar { width: 8px; height: 8px; }
|
| 273 |
+
*::-webkit-scrollbar-track { background: transparent; }
|
| 274 |
+
*::-webkit-scrollbar-thumb {
|
| 275 |
+
background-color: var(--sb-thumb);
|
| 276 |
+
border-radius: 8px;
|
| 277 |
+
border: 2px solid transparent;
|
| 278 |
+
background-clip: content-box;
|
| 279 |
+
}
|
| 280 |
+
*::-webkit-scrollbar-thumb:hover { background-color: #9fa3c7; }
|
| 281 |
+
|
| 282 |
+
.gradio-container { height: 100vh !important; max-height: 100vh !important; }
|
| 283 |
+
#main-row { height: calc(100vh - 130px); }
|
| 284 |
+
#chat-col { height: 100%; }
|
| 285 |
+
#side-col { height: 100%; overflow-y: auto; }
|
| 286 |
+
"""
|
| 287 |
+
|
| 288 |
# ---------------------------------------------------------------------------
|
| 289 |
# UI
|
| 290 |
# ---------------------------------------------------------------------------
|
| 291 |
|
| 292 |
+
with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft(), css=CUSTOM_CSS, fill_height=True) as demo:
|
| 293 |
+
settings_state = gr.BrowserState(DEFAULT_SETTINGS)
|
| 294 |
+
|
| 295 |
gr.Markdown("## 🤖 Chatterbox — чат + генерация файлов кода")
|
| 296 |
|
| 297 |
if not HF_TOKEN:
|
|
|
|
| 303 |
"Providers*), затем перезапустите Space."
|
| 304 |
)
|
| 305 |
|
| 306 |
+
with gr.Row(elem_id="main-row"):
|
| 307 |
+
with gr.Column(scale=3, elem_id="chat-col"):
|
| 308 |
chatbot = gr.Chatbot(
|
| 309 |
type="messages",
|
| 310 |
show_label=False,
|
| 311 |
show_copy_button=True,
|
| 312 |
+
height="calc(100% - 110px)",
|
| 313 |
)
|
| 314 |
msg = gr.Textbox(
|
| 315 |
placeholder="Опишите, что построить, например: «сделай лендинг с формой подписки»",
|
|
|
|
| 320 |
send_btn = gr.Button("Отправить", variant="primary")
|
| 321 |
clear_btn = gr.Button("Очистить")
|
| 322 |
|
| 323 |
+
with gr.Column(scale=2, elem_id="side-col"):
|
| 324 |
with gr.Tab("📄 Файлы"):
|
| 325 |
files_out = gr.File(
|
| 326 |
label="Сгенерированные файлы (несколько — упаковываются в .zip)",
|
|
|
|
| 341 |
max_tokens = gr.Slider(128, 8192, value=2048, step=64, label="Max new tokens")
|
| 342 |
top_p = gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 343 |
|
| 344 |
+
settings_widgets = [model_dd, system_box, temperature, max_tokens, top_p]
|
| 345 |
+
|
| 346 |
+
demo.load(load_settings, inputs=[settings_state], outputs=settings_widgets)
|
| 347 |
+
for widget in settings_widgets:
|
| 348 |
+
widget.change(save_settings, inputs=settings_widgets, outputs=[settings_state])
|
| 349 |
+
|
| 350 |
inputs = [msg, chatbot, model_dd, system_box, temperature, max_tokens, top_p]
|
| 351 |
outputs = [chatbot, msg, files_out, preview]
|
| 352 |
|