Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ MODELS = {
|
|
| 12 |
"Meta LLaMA 3.1 (8B Instruct)": "meta-llama/Llama-3.1-8B-Instruct",
|
| 13 |
"Mistral 7B Instruct": "mistralai/Mistral-7B-Instruct-v0.3",
|
| 14 |
}
|
|
|
|
| 15 |
DATASETS = ["The Stack", "CodeXGLUE"]
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
DB_PATH = "history.db"
|
|
@@ -20,13 +21,11 @@ SYSTEM_DEFAULT = (
|
|
| 20 |
"You are a backend-focused coding assistant. "
|
| 21 |
"Always prioritize database, API, authentication, routing, migrations, and CRUD logic. "
|
| 22 |
"Provide full backend code scaffolds with files, paths, and commands. "
|
| 23 |
-
"Only include frontend if required for framework integration
|
| 24 |
-
"(e.g., Laravel Blade, Django templates). "
|
| 25 |
-
"If user asks for too much frontend, politely say: 'I'm a backend-focused assistant and cannot provide excessive frontend code.'"
|
| 26 |
)
|
| 27 |
|
| 28 |
# ---------------------------
|
| 29 |
-
# DB Setup
|
| 30 |
# ---------------------------
|
| 31 |
def db():
|
| 32 |
conn = sqlite3.connect(DB_PATH)
|
|
@@ -89,8 +88,10 @@ def get_messages(session_id: int):
|
|
| 89 |
conn = db()
|
| 90 |
cur = conn.cursor()
|
| 91 |
cur.execute("""
|
| 92 |
-
SELECT role, content
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
""", (session_id,))
|
| 95 |
rows = cur.fetchall()
|
| 96 |
conn.close()
|
|
@@ -116,11 +117,11 @@ def update_session_title_if_needed(session_id: int, first_user_text: str):
|
|
| 116 |
title = first_user_text.strip().split("\n")[0]
|
| 117 |
title = (title[:50] + "…") if len(title) > 50 else title
|
| 118 |
cur.execute("UPDATE sessions SET title=? WHERE id=?", (title or "New chat", session_id))
|
| 119 |
-
|
| 120 |
conn.close()
|
| 121 |
|
| 122 |
# ---------------------------
|
| 123 |
-
# Helpers
|
| 124 |
# ---------------------------
|
| 125 |
def label_to_id(label: str | None) -> int | None:
|
| 126 |
if not label:
|
|
@@ -147,7 +148,7 @@ def load_dataset_by_name(name: str):
|
|
| 147 |
return None
|
| 148 |
|
| 149 |
# ---------------------------
|
| 150 |
-
# Gradio Callbacks
|
| 151 |
# ---------------------------
|
| 152 |
def refresh_sessions_cb():
|
| 153 |
labels, _ = list_sessions()
|
|
@@ -175,7 +176,7 @@ def delete_chat_cb(selected_label):
|
|
| 175 |
return gr.update(choices=labels, value=selected), []
|
| 176 |
|
| 177 |
FRONTEND_KEYWORDS = [
|
| 178 |
-
"react","vue","angular","html","css","javascript","tailwind","recharts","typescript"
|
| 179 |
]
|
| 180 |
|
| 181 |
def is_frontend_request(user_text: str) -> bool:
|
|
@@ -208,6 +209,7 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
|
|
| 208 |
client = get_client(model_choice)
|
| 209 |
api_messages = build_api_messages(sid, system_message)
|
| 210 |
partial = ""
|
|
|
|
| 211 |
try:
|
| 212 |
for chunk in client.chat_completion(
|
| 213 |
messages=api_messages,
|
|
@@ -231,6 +233,7 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
|
|
| 231 |
yield (display_msgs, "", selected_label)
|
| 232 |
|
| 233 |
add_message(sid, "assistant", partial)
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
|
| 236 |
yield (display_msgs, "", selected_label)
|
|
@@ -248,8 +251,11 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
|
|
| 248 |
conn = db()
|
| 249 |
cur = conn.cursor()
|
| 250 |
cur.execute("""
|
| 251 |
-
DELETE FROM messages
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
)
|
| 254 |
""", (sid,))
|
| 255 |
conn.commit()
|
|
@@ -262,6 +268,7 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
|
|
| 262 |
|
| 263 |
client = get_client(model_choice)
|
| 264 |
partial = ""
|
|
|
|
| 265 |
try:
|
| 266 |
for chunk in client.chat_completion(
|
| 267 |
messages=api_messages,
|
|
@@ -285,12 +292,13 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
|
|
| 285 |
yield display_msgs
|
| 286 |
|
| 287 |
add_message(sid, "assistant", partial)
|
|
|
|
| 288 |
except Exception as e:
|
| 289 |
display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
|
| 290 |
yield display_msgs
|
| 291 |
|
| 292 |
# ---------------------------
|
| 293 |
-
# App UI
|
| 294 |
# ---------------------------
|
| 295 |
init_db()
|
| 296 |
labels, _ = list_sessions()
|
|
@@ -301,31 +309,152 @@ if not labels:
|
|
| 301 |
default_selected = labels[0] if labels else None
|
| 302 |
|
| 303 |
with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.themes.Soft()) as demo:
|
| 304 |
-
gr.
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
-
|
| 314 |
-
dataset_choice = gr.Dropdown(choices=DATASETS, value=DATASETS[0], label="Select a dataset", interactive=True)
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
|
|
|
| 321 |
with gr.Column(scale=3):
|
| 322 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
with gr.Row():
|
| 324 |
-
user_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
with gr.Row():
|
| 326 |
-
send_btn = gr.Button("Send ▶️", variant="primary")
|
| 327 |
-
regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
|
| 328 |
|
|
|
|
| 329 |
refresh_btn.click(refresh_sessions_cb, outputs=session_list)
|
| 330 |
new_btn.click(new_chat_cb, outputs=[session_list, chatbot, user_box])
|
| 331 |
del_btn.click(delete_chat_cb, inputs=session_list, outputs=[session_list, chatbot])
|
|
@@ -350,4 +479,4 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 350 |
)
|
| 351 |
|
| 352 |
if __name__ == "__main__":
|
| 353 |
-
demo.launch()
|
|
|
|
| 12 |
"Meta LLaMA 3.1 (8B Instruct)": "meta-llama/Llama-3.1-8B-Instruct",
|
| 13 |
"Mistral 7B Instruct": "mistralai/Mistral-7B-Instruct-v0.3",
|
| 14 |
}
|
| 15 |
+
|
| 16 |
DATASETS = ["The Stack", "CodeXGLUE"]
|
| 17 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
DB_PATH = "history.db"
|
|
|
|
| 21 |
"You are a backend-focused coding assistant. "
|
| 22 |
"Always prioritize database, API, authentication, routing, migrations, and CRUD logic. "
|
| 23 |
"Provide full backend code scaffolds with files, paths, and commands. "
|
| 24 |
+
"Only include frontend if required for framework integration."
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
# ---------------------------
|
| 28 |
+
# DB Setup (unchanged)
|
| 29 |
# ---------------------------
|
| 30 |
def db():
|
| 31 |
conn = sqlite3.connect(DB_PATH)
|
|
|
|
| 88 |
conn = db()
|
| 89 |
cur = conn.cursor()
|
| 90 |
cur.execute("""
|
| 91 |
+
SELECT role, content
|
| 92 |
+
FROM messages
|
| 93 |
+
WHERE session_id = ?
|
| 94 |
+
ORDER BY id ASC
|
| 95 |
""", (session_id,))
|
| 96 |
rows = cur.fetchall()
|
| 97 |
conn.close()
|
|
|
|
| 117 |
title = first_user_text.strip().split("\n")[0]
|
| 118 |
title = (title[:50] + "…") if len(title) > 50 else title
|
| 119 |
cur.execute("UPDATE sessions SET title=? WHERE id=?", (title or "New chat", session_id))
|
| 120 |
+
conn.commit()
|
| 121 |
conn.close()
|
| 122 |
|
| 123 |
# ---------------------------
|
| 124 |
+
# Helpers (unchanged)
|
| 125 |
# ---------------------------
|
| 126 |
def label_to_id(label: str | None) -> int | None:
|
| 127 |
if not label:
|
|
|
|
| 148 |
return None
|
| 149 |
|
| 150 |
# ---------------------------
|
| 151 |
+
# Gradio Callbacks (unchanged)
|
| 152 |
# ---------------------------
|
| 153 |
def refresh_sessions_cb():
|
| 154 |
labels, _ = list_sessions()
|
|
|
|
| 176 |
return gr.update(choices=labels, value=selected), []
|
| 177 |
|
| 178 |
FRONTEND_KEYWORDS = [
|
| 179 |
+
"react", "vue", "angular", "html", "css", "javascript", "tailwind", "recharts", "typescript"
|
| 180 |
]
|
| 181 |
|
| 182 |
def is_frontend_request(user_text: str) -> bool:
|
|
|
|
| 209 |
client = get_client(model_choice)
|
| 210 |
api_messages = build_api_messages(sid, system_message)
|
| 211 |
partial = ""
|
| 212 |
+
|
| 213 |
try:
|
| 214 |
for chunk in client.chat_completion(
|
| 215 |
messages=api_messages,
|
|
|
|
| 233 |
yield (display_msgs, "", selected_label)
|
| 234 |
|
| 235 |
add_message(sid, "assistant", partial)
|
| 236 |
+
|
| 237 |
except Exception as e:
|
| 238 |
display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
|
| 239 |
yield (display_msgs, "", selected_label)
|
|
|
|
| 251 |
conn = db()
|
| 252 |
cur = conn.cursor()
|
| 253 |
cur.execute("""
|
| 254 |
+
DELETE FROM messages
|
| 255 |
+
WHERE id = (
|
| 256 |
+
SELECT id FROM messages
|
| 257 |
+
WHERE session_id=?
|
| 258 |
+
ORDER BY id DESC LIMIT 1
|
| 259 |
)
|
| 260 |
""", (sid,))
|
| 261 |
conn.commit()
|
|
|
|
| 268 |
|
| 269 |
client = get_client(model_choice)
|
| 270 |
partial = ""
|
| 271 |
+
|
| 272 |
try:
|
| 273 |
for chunk in client.chat_completion(
|
| 274 |
messages=api_messages,
|
|
|
|
| 292 |
yield display_msgs
|
| 293 |
|
| 294 |
add_message(sid, "assistant", partial)
|
| 295 |
+
|
| 296 |
except Exception as e:
|
| 297 |
display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
|
| 298 |
yield display_msgs
|
| 299 |
|
| 300 |
# ---------------------------
|
| 301 |
+
# App UI - OPTIMIZED LAYOUT
|
| 302 |
# ---------------------------
|
| 303 |
init_db()
|
| 304 |
labels, _ = list_sessions()
|
|
|
|
| 309 |
default_selected = labels[0] if labels else None
|
| 310 |
|
| 311 |
with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.themes.Soft()) as demo:
|
| 312 |
+
gr.HTML("""
|
| 313 |
+
<style>
|
| 314 |
+
.compact-row {
|
| 315 |
+
margin-bottom: 0.5rem !important;
|
| 316 |
+
}
|
| 317 |
+
.compact-slider {
|
| 318 |
+
margin-bottom: 0.3rem !important;
|
| 319 |
+
}
|
| 320 |
+
.small-textbox {
|
| 321 |
+
margin-bottom: 0.5rem !important;
|
| 322 |
+
}
|
| 323 |
+
.section-header {
|
| 324 |
+
margin-bottom: 0.5rem !important;
|
| 325 |
+
font-weight: 600;
|
| 326 |
+
}
|
| 327 |
+
button {
|
| 328 |
+
background-color: #22c55e !important;
|
| 329 |
+
color: #ffffff !important;
|
| 330 |
+
border: none !important;
|
| 331 |
+
}
|
| 332 |
+
button:hover {
|
| 333 |
+
background-color: #16a34a !important;
|
| 334 |
+
}
|
| 335 |
+
button:focus {
|
| 336 |
+
outline: 2px solid #166534 !important;
|
| 337 |
+
outline-offset: 2px;
|
| 338 |
+
}
|
| 339 |
+
</style>
|
| 340 |
+
""")
|
| 341 |
|
| 342 |
+
gr.Markdown("## 🗄️ Backend CRUD Assistant")
|
|
|
|
| 343 |
|
| 344 |
+
with gr.Row():
|
| 345 |
+
# Left sidebar - compact layout
|
| 346 |
+
with gr.Column(scale=1, min_width=280):
|
| 347 |
+
# Sessions section
|
| 348 |
+
gr.Markdown("### 📁 Sessions", elem_classes=["section-header"])
|
| 349 |
+
session_list = gr.Radio(
|
| 350 |
+
choices=labels,
|
| 351 |
+
value=default_selected,
|
| 352 |
+
label="Your chats",
|
| 353 |
+
interactive=True,
|
| 354 |
+
elem_classes=["compact-row"]
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
# Session management buttons in a tight grid
|
| 358 |
+
with gr.Row(elem_classes=["compact-row"]):
|
| 359 |
+
new_btn = gr.Button("➕ New", size="sm", variant="primary")
|
| 360 |
+
del_btn = gr.Button("🗑️ Delete", size="sm", variant="stop")
|
| 361 |
+
refresh_btn = gr.Button("🔄", size="sm", variant="secondary")
|
| 362 |
+
|
| 363 |
+
# Rename section - more compact
|
| 364 |
+
with gr.Row(elem_classes=["compact-row"]):
|
| 365 |
+
edit_title_box = gr.Textbox(
|
| 366 |
+
label="✏️ Rename",
|
| 367 |
+
placeholder="New title...",
|
| 368 |
+
scale=3,
|
| 369 |
+
elem_classes=["small-textbox"]
|
| 370 |
+
)
|
| 371 |
+
rename_btn = gr.Button("💾", size="sm", scale=1)
|
| 372 |
+
|
| 373 |
+
def rename_session_cb(new_title, selected_label):
|
| 374 |
+
sid = label_to_id(selected_label)
|
| 375 |
+
if sid and new_title.strip():
|
| 376 |
+
conn = db()
|
| 377 |
+
cur = conn.cursor()
|
| 378 |
+
cur.execute("UPDATE sessions SET title=? WHERE id=?", (new_title.strip(), sid))
|
| 379 |
+
conn.commit()
|
| 380 |
+
conn.close()
|
| 381 |
+
labels, _ = list_sessions()
|
| 382 |
+
new_selected = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
|
| 383 |
+
return gr.update(choices=labels, value=new_selected)
|
| 384 |
+
|
| 385 |
+
rename_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=session_list)
|
| 386 |
+
|
| 387 |
+
# Model and Dataset in a compact row
|
| 388 |
+
with gr.Row(elem_classes=["compact-row"]):
|
| 389 |
+
with gr.Column(scale=1):
|
| 390 |
+
gr.Markdown("**🤖 Model**")
|
| 391 |
+
model_choice = gr.Dropdown(
|
| 392 |
+
choices=list(MODELS.keys()),
|
| 393 |
+
value=list(MODELS.keys())[0],
|
| 394 |
+
label="",
|
| 395 |
+
interactive=True,
|
| 396 |
+
container=False
|
| 397 |
+
)
|
| 398 |
+
with gr.Column(scale=1):
|
| 399 |
+
gr.Markdown("**📚 Dataset**")
|
| 400 |
+
dataset_choice = gr.Dropdown(
|
| 401 |
+
choices=DATASETS,
|
| 402 |
+
value=DATASETS[0],
|
| 403 |
+
label="",
|
| 404 |
+
interactive=True,
|
| 405 |
+
container=False
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Generation Settings - more compact
|
| 409 |
+
gr.Markdown("### ⚙️ Settings", elem_classes=["section-header"])
|
| 410 |
+
|
| 411 |
+
# Compact system message
|
| 412 |
+
system_box = gr.Textbox(
|
| 413 |
+
value=SYSTEM_DEFAULT,
|
| 414 |
+
label="System prompt",
|
| 415 |
+
lines=2,
|
| 416 |
+
max_lines=2,
|
| 417 |
+
elem_classes=["small-textbox"]
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
# Sliders in compact layout
|
| 421 |
+
max_tokens = gr.Slider(
|
| 422 |
+
256, 2048, value=1200, step=16,
|
| 423 |
+
label="Max tokens",
|
| 424 |
+
elem_classes=["compact-slider"]
|
| 425 |
+
)
|
| 426 |
+
temperature = gr.Slider(
|
| 427 |
+
0.0, 2.0, value=0.25, step=0.05,
|
| 428 |
+
label="Temperature",
|
| 429 |
+
elem_classes=["compact-slider"]
|
| 430 |
+
)
|
| 431 |
+
top_p = gr.Slider(
|
| 432 |
+
0.1, 1.0, value=0.9, step=0.05,
|
| 433 |
+
label="Top-p",
|
| 434 |
+
elem_classes=["compact-slider"]
|
| 435 |
+
)
|
| 436 |
|
| 437 |
+
# Main chat area
|
| 438 |
with gr.Column(scale=3):
|
| 439 |
+
chatbot = gr.Chatbot(
|
| 440 |
+
label="Assistant",
|
| 441 |
+
height=520,
|
| 442 |
+
type="messages",
|
| 443 |
+
show_copy_button=True
|
| 444 |
+
)
|
| 445 |
with gr.Row():
|
| 446 |
+
user_box = gr.Textbox(
|
| 447 |
+
placeholder="Describe your CRUD/backend task…",
|
| 448 |
+
lines=2,
|
| 449 |
+
max_lines=3,
|
| 450 |
+
scale=5,
|
| 451 |
+
container=False
|
| 452 |
+
)
|
| 453 |
with gr.Row():
|
| 454 |
+
send_btn = gr.Button("Send ▶️", variant="primary", size="sm")
|
| 455 |
+
regen_btn = gr.Button("Regenerate 🔁", variant="secondary", size="sm")
|
| 456 |
|
| 457 |
+
# Event handlers (unchanged)
|
| 458 |
refresh_btn.click(refresh_sessions_cb, outputs=session_list)
|
| 459 |
new_btn.click(new_chat_cb, outputs=[session_list, chatbot, user_box])
|
| 460 |
del_btn.click(delete_chat_cb, inputs=session_list, outputs=[session_list, chatbot])
|
|
|
|
| 479 |
)
|
| 480 |
|
| 481 |
if __name__ == "__main__":
|
| 482 |
+
demo.launch()
|