Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,13 +18,12 @@ DATASETS = ["The Stack", "CodeXGLUE"] # Dropdown for dataset selection
|
|
| 18 |
HF_TOKEN = os.getenv("HF_TOKEN") # Set in your Space's Secrets
|
| 19 |
DB_PATH = "history.db"
|
| 20 |
|
|
|
|
| 21 |
SYSTEM_DEFAULT = (
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"Provide
|
| 25 |
-
"
|
| 26 |
-
"(e.g., Laravel Blade, Django templates). "
|
| 27 |
-
"If user asks for too much frontend, politely say: 'I'm a backend-focused assistant and cannot provide excessive frontend code.'"
|
| 28 |
)
|
| 29 |
|
| 30 |
# ---------------------------
|
|
@@ -184,6 +183,7 @@ FRONTEND_KEYWORDS = [
|
|
| 184 |
def is_frontend_request(user_text: str) -> bool:
|
| 185 |
text_lower = user_text.lower()
|
| 186 |
return any(kw in text_lower for kw in FRONTEND_KEYWORDS)
|
|
|
|
| 187 |
# --- Fixed send_cb to show user message ---
|
| 188 |
def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens, temperature, top_p, model_choice, dataset_choice, *args):
|
| 189 |
sid = label_to_id(selected_label)
|
|
@@ -314,11 +314,48 @@ if not labels:
|
|
| 314 |
default_selected = labels[0] if labels else None
|
| 315 |
|
| 316 |
with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 317 |
gr.HTML("""
|
| 318 |
<style>
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
</style>
|
| 323 |
""")
|
| 324 |
|
|
@@ -327,58 +364,67 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 327 |
with gr.Row():
|
| 328 |
with gr.Column(scale=1, min_width=260):
|
| 329 |
gr.Markdown("### 📁 Sessions")
|
| 330 |
-
|
|
|
|
| 331 |
choices=labels,
|
| 332 |
value=default_selected,
|
| 333 |
-
label="
|
| 334 |
interactive=True
|
| 335 |
)
|
| 336 |
-
|
| 337 |
-
#
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
def rename_session_cb(new_title, selected_label):
|
| 343 |
sid = label_to_id(selected_label)
|
| 344 |
-
if sid and new_title.strip():
|
| 345 |
conn = db()
|
| 346 |
cur = conn.cursor()
|
| 347 |
cur.execute("UPDATE sessions SET title=? WHERE id=?", (new_title.strip(), sid))
|
| 348 |
conn.commit()
|
| 349 |
conn.close()
|
| 350 |
-
|
| 351 |
labels, _ = list_sessions()
|
| 352 |
new_selected = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
|
| 353 |
return gr.update(choices=labels, value=new_selected)
|
| 354 |
-
|
|
|
|
| 355 |
rename_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=session_list)
|
| 356 |
-
with gr.Row():
|
| 357 |
-
new_btn = gr.Button("➕ New Chat", variant="primary")
|
| 358 |
-
del_btn = gr.Button("🗑️ Delete", variant="stop")
|
| 359 |
-
refresh_btn = gr.Button("🔄 Refresh", variant="secondary")
|
| 360 |
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
model_choice = gr.Dropdown(
|
| 363 |
choices=list(MODELS.keys()),
|
| 364 |
value=list(MODELS.keys())[0],
|
| 365 |
-
label="
|
| 366 |
interactive=True
|
| 367 |
)
|
| 368 |
|
| 369 |
-
gr.Markdown("### 📚 Dataset
|
| 370 |
dataset_choice = gr.Dropdown(
|
| 371 |
choices=DATASETS,
|
| 372 |
value=DATASETS[0],
|
| 373 |
-
label="
|
| 374 |
interactive=True
|
| 375 |
)
|
| 376 |
|
| 377 |
-
gr.Markdown("### ⚙️
|
|
|
|
| 378 |
system_box = gr.Textbox(
|
| 379 |
value=SYSTEM_DEFAULT,
|
| 380 |
label="System message",
|
| 381 |
-
lines=
|
| 382 |
)
|
| 383 |
max_tokens = gr.Slider(256, 4096, value=1200, step=16, label="Max tokens")
|
| 384 |
temperature = gr.Slider(0.0, 2.0, value=0.25, step=0.05, label="Temperature")
|
|
@@ -392,6 +438,7 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 392 |
send_btn = gr.Button("Send ▶️", variant="primary")
|
| 393 |
regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
|
| 394 |
|
|
|
|
| 395 |
refresh_btn.click(refresh_sessions_cb, outputs=session_list)
|
| 396 |
new_btn.click(new_chat_cb, outputs=[session_list, chatbot, user_box])
|
| 397 |
del_btn.click(delete_chat_cb, inputs=session_list, outputs=[session_list, chatbot])
|
|
|
|
| 18 |
HF_TOKEN = os.getenv("HF_TOKEN") # Set in your Space's Secrets
|
| 19 |
DB_PATH = "history.db"
|
| 20 |
|
| 21 |
+
# Minimal system prompt (short)
|
| 22 |
SYSTEM_DEFAULT = (
|
| 23 |
+
"Backend-focused assistant.\n"
|
| 24 |
+
"- Focus: DB, APIs, CRUD, auth, migrations.\n"
|
| 25 |
+
"- Provide backend scaffolds with paths & commands.\n"
|
| 26 |
+
"- Frontend only if essential for integration."
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
# ---------------------------
|
|
|
|
| 183 |
def is_frontend_request(user_text: str) -> bool:
|
| 184 |
text_lower = user_text.lower()
|
| 185 |
return any(kw in text_lower for kw in FRONTEND_KEYWORDS)
|
| 186 |
+
|
| 187 |
# --- Fixed send_cb to show user message ---
|
| 188 |
def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens, temperature, top_p, model_choice, dataset_choice, *args):
|
| 189 |
sid = label_to_id(selected_label)
|
|
|
|
| 314 |
default_selected = labels[0] if labels else None
|
| 315 |
|
| 316 |
with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.themes.Soft()) as demo:
|
| 317 |
+
# Prevent page-level scrolling and tighten UI spacing.
|
| 318 |
gr.HTML("""
|
| 319 |
<style>
|
| 320 |
+
html, body { overflow: hidden; height: 100%; } /* Lock scrolling on page */
|
| 321 |
+
.gradio-container { height: 100vh; width: 100vw; } /* Fill window */
|
| 322 |
+
|
| 323 |
+
/* Button styling (smaller icons) */
|
| 324 |
+
.gr-button, button {
|
| 325 |
+
padding: 6px 8px !important;
|
| 326 |
+
font-size: 14px !important;
|
| 327 |
+
min-height: 28px !important;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
/* Make sidebar compact and scrollable */
|
| 331 |
+
.gr-block.gr-column:nth-of-type(1) {
|
| 332 |
+
max-width: 340px;
|
| 333 |
+
overflow-y: auto;
|
| 334 |
+
padding-right: 8px;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
/* Compact dropdown / radio list for sessions */
|
| 338 |
+
.gr-dropdown, .gr-radio {
|
| 339 |
+
max-height: 420px !important;
|
| 340 |
+
overflow-y: auto !important;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
/* Reduce space used by sliders & textareas */
|
| 344 |
+
.gr-slider, .gr-textbox, .gr-dropdown, .gr-radio {
|
| 345 |
+
margin-bottom: 6px !important;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
/* Make the chat area fill more vertical space */
|
| 349 |
+
.gr-chat { height: 720px !important; }
|
| 350 |
+
|
| 351 |
+
/* Reduce overall margins */
|
| 352 |
+
.container, .gr-box { margin: 0 !important; padding: 4px !important; }
|
| 353 |
+
|
| 354 |
+
/* Make small icon buttons visually compact */
|
| 355 |
+
.icon-btn { padding: 4px 6px !important; min-width: 34px !important; }
|
| 356 |
+
|
| 357 |
+
/* Keep the second column scrollable if content overflows */
|
| 358 |
+
.gr-block.gr-column:nth-of-type(2) { max-height: calc(100vh - 40px); overflow-y: auto; }
|
| 359 |
</style>
|
| 360 |
""")
|
| 361 |
|
|
|
|
| 364 |
with gr.Row():
|
| 365 |
with gr.Column(scale=1, min_width=260):
|
| 366 |
gr.Markdown("### 📁 Sessions")
|
| 367 |
+
# compact session selector
|
| 368 |
+
session_list = gr.Dropdown(
|
| 369 |
choices=labels,
|
| 370 |
value=default_selected,
|
| 371 |
+
label="Chats",
|
| 372 |
interactive=True
|
| 373 |
)
|
| 374 |
+
|
| 375 |
+
# small inline controls (icons)
|
| 376 |
+
with gr.Row():
|
| 377 |
+
new_btn = gr.Button("➕", elem_id="new-chat-btn")
|
| 378 |
+
rename_btn = gr.Button("✏️", elem_id="rename-chat-btn")
|
| 379 |
+
del_btn = gr.Button("🗑️", elem_id="delete-chat-btn")
|
| 380 |
+
refresh_btn = gr.Button("🔄", elem_id="refresh-chat-btn")
|
| 381 |
+
|
| 382 |
+
# editable title input (kept but small)
|
| 383 |
+
edit_title_box = gr.Textbox(label="Rename selected chat", placeholder="Type new title...", lines=1)
|
| 384 |
+
|
| 385 |
+
# rename callback
|
| 386 |
def rename_session_cb(new_title, selected_label):
|
| 387 |
sid = label_to_id(selected_label)
|
| 388 |
+
if sid and new_title and new_title.strip():
|
| 389 |
conn = db()
|
| 390 |
cur = conn.cursor()
|
| 391 |
cur.execute("UPDATE sessions SET title=? WHERE id=?", (new_title.strip(), sid))
|
| 392 |
conn.commit()
|
| 393 |
conn.close()
|
|
|
|
| 394 |
labels, _ = list_sessions()
|
| 395 |
new_selected = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
|
| 396 |
return gr.update(choices=labels, value=new_selected)
|
| 397 |
+
|
| 398 |
+
# wire rename button
|
| 399 |
rename_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=session_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
+
# wire other small buttons
|
| 402 |
+
new_btn.click(new_chat_cb, outputs=[session_list, gr.update(value=[]), gr.update(value="")])
|
| 403 |
+
del_btn.click(delete_chat_cb, inputs=session_list, outputs=[session_list, gr.update(value=[])])
|
| 404 |
+
refresh_btn.click(refresh_sessions_cb, outputs=session_list)
|
| 405 |
+
|
| 406 |
+
gr.Markdown("### 🤖 Model")
|
| 407 |
model_choice = gr.Dropdown(
|
| 408 |
choices=list(MODELS.keys()),
|
| 409 |
value=list(MODELS.keys())[0],
|
| 410 |
+
label="Model",
|
| 411 |
interactive=True
|
| 412 |
)
|
| 413 |
|
| 414 |
+
gr.Markdown("### 📚 Dataset")
|
| 415 |
dataset_choice = gr.Dropdown(
|
| 416 |
choices=DATASETS,
|
| 417 |
value=DATASETS[0],
|
| 418 |
+
label="Dataset",
|
| 419 |
interactive=True
|
| 420 |
)
|
| 421 |
|
| 422 |
+
gr.Markdown("### ⚙️ Settings")
|
| 423 |
+
# make system box short (min lines)
|
| 424 |
system_box = gr.Textbox(
|
| 425 |
value=SYSTEM_DEFAULT,
|
| 426 |
label="System message",
|
| 427 |
+
lines=3
|
| 428 |
)
|
| 429 |
max_tokens = gr.Slider(256, 4096, value=1200, step=16, label="Max tokens")
|
| 430 |
temperature = gr.Slider(0.0, 2.0, value=0.25, step=0.05, label="Temperature")
|
|
|
|
| 438 |
send_btn = gr.Button("Send ▶️", variant="primary")
|
| 439 |
regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
|
| 440 |
|
| 441 |
+
# Hook up callbacks (preserve your existing wires)
|
| 442 |
refresh_btn.click(refresh_sessions_cb, outputs=session_list)
|
| 443 |
new_btn.click(new_chat_cb, outputs=[session_list, chatbot, user_box])
|
| 444 |
del_btn.click(delete_chat_cb, inputs=session_list, outputs=[session_list, chatbot])
|