arubaDev commited on
Commit
90302f8
·
verified ·
1 Parent(s): 57bff71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -136
app.py CHANGED
@@ -12,18 +12,17 @@ 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
-
16
- DATASETS = ["The Stack", "CodeXGLUE"] # Dropdown for dataset selection
17
-
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
  # ---------------------------
@@ -91,8 +90,7 @@ def get_messages(session_id: int):
91
  cur = conn.cursor()
92
  cur.execute("""
93
  SELECT role, content FROM messages
94
- WHERE session_id = ?
95
- ORDER BY id ASC
96
  """, (session_id,))
97
  rows = cur.fetchall()
98
  conn.close()
@@ -118,7 +116,7 @@ def update_session_title_if_needed(session_id: int, first_user_text: str):
118
  title = first_user_text.strip().split("\n")[0]
119
  title = (title[:50] + "…") if len(title) > 50 else title
120
  cur.execute("UPDATE sessions SET title=? WHERE id=?", (title or "New chat", session_id))
121
- conn.commit()
122
  conn.close()
123
 
124
  # ---------------------------
@@ -177,14 +175,13 @@ def delete_chat_cb(selected_label):
177
  return gr.update(choices=labels, value=selected), []
178
 
179
  FRONTEND_KEYWORDS = [
180
- "react", "vue", "angular", "html", "css", "javascript", "tailwind", "recharts", "typescript"
181
  ]
182
 
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)
190
  if sid is None:
@@ -192,14 +189,12 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
192
  labels, _ = list_sessions()
193
  selected_label = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
194
 
195
- # Save user message
196
  add_message(sid, "user", user_text)
197
  update_session_title_if_needed(sid, user_text)
198
 
199
  display_msgs = chatbot_msgs[:]
200
  display_msgs.append({"role": "user", "content": user_text})
201
 
202
- # Check for frontend-heavy request
203
  if is_frontend_request(user_text):
204
  apology = "⚠️ I'm a backend-focused assistant and cannot provide frontend code."
205
  display_msgs.append({"role": "assistant", "content": apology})
@@ -207,7 +202,6 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
207
  yield (display_msgs, "", selected_label)
208
  return
209
 
210
- # Normal backend response
211
  display_msgs.append({"role": "assistant", "content": "…"})
212
  yield (display_msgs, "", selected_label)
213
 
@@ -222,10 +216,8 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
222
  top_p=float(top_p),
223
  stream=True,
224
  ):
225
- # --- FIX: handle models that send empty chunks or use message instead of delta ---
226
  if not hasattr(chunk, "choices") or not chunk.choices:
227
  continue
228
-
229
  choice = chunk.choices[0]
230
  delta = ""
231
  if hasattr(choice, "delta") and choice.delta and getattr(choice.delta, "content", None) is not None:
@@ -243,7 +235,6 @@ def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens,
243
  display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
244
  yield (display_msgs, "", selected_label)
245
 
246
-
247
  def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p, model_choice, dataset_choice):
248
  sid = label_to_id(selected_label)
249
  if sid is None:
@@ -253,13 +244,11 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
253
  if not msgs:
254
  return [], ""
255
 
256
- # Remove the last assistant message if it exists (to regenerate it)
257
  if msgs and msgs[-1]["role"] == "assistant":
258
  conn = db()
259
  cur = conn.cursor()
260
  cur.execute("""
261
- DELETE FROM messages
262
- WHERE id = (
263
  SELECT id FROM messages WHERE session_id=? ORDER BY id DESC LIMIT 1
264
  )
265
  """, (sid,))
@@ -281,10 +270,8 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
281
  top_p=float(top_p),
282
  stream=True,
283
  ):
284
- # --- FIX: handle models that send empty chunks or use message instead of delta ---
285
  if not hasattr(chunk, "choices") or not chunk.choices:
286
  continue
287
-
288
  choice = chunk.choices[0]
289
  delta = ""
290
  if hasattr(choice, "delta") and choice.delta and getattr(choice.delta, "content", None) is not None:
@@ -302,7 +289,6 @@ def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p
302
  display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
303
  yield display_msgs
304
 
305
-
306
  # ---------------------------
307
  # App UI
308
  # ---------------------------
@@ -311,113 +297,23 @@ labels, _ = list_sessions()
311
  if not labels:
312
  first_sid = create_session("New chat")
313
  labels, _ = list_sessions()
 
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
-
362
  gr.Markdown("## 🗄️ LLaMA & Mistral Backend-Focused CRUD Automation — with Persistent History")
363
 
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 (kept unchanged)
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
- gr.Markdown("### 🤖 Model")
399
- model_choice = gr.Dropdown(
400
- choices=list(MODELS.keys()),
401
- value=list(MODELS.keys())[0],
402
- label="Model",
403
- interactive=True
404
- )
405
 
406
- gr.Markdown("### 📚 Dataset")
407
- dataset_choice = gr.Dropdown(
408
- choices=DATASETS,
409
- value=DATASETS[0],
410
- label="Dataset",
411
- interactive=True
412
- )
413
-
414
- gr.Markdown("### ⚙️ Settings")
415
- # make system box short (min lines)
416
- system_box = gr.Textbox(
417
- value=SYSTEM_DEFAULT,
418
- label="System message",
419
- lines=3
420
- )
421
  max_tokens = gr.Slider(256, 4096, value=1200, step=16, label="Max tokens")
422
  temperature = gr.Slider(0.0, 2.0, value=0.25, step=0.05, label="Temperature")
423
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
@@ -430,19 +326,11 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
430
  send_btn = gr.Button("Send ▶️", variant="primary")
431
  regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
432
 
433
- # Hook up callbacks (moved here so chatbot & user_box exist)
434
- # wire rename button (rename callback returns gr.update)
435
- rename_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=[session_list])
436
-
437
- # wire other small buttons - use components in outputs (not gr.update)
438
- refresh_btn.click(refresh_sessions_cb, outputs=[session_list])
439
  new_btn.click(new_chat_cb, outputs=[session_list, chatbot, user_box])
440
- del_btn.click(delete_chat_cb, inputs=[session_list], outputs=[session_list, chatbot])
441
-
442
- # session load/change
443
- session_list.change(load_session_cb, inputs=[session_list], outputs=[chatbot])
444
 
445
- # main conversation wiring
446
  send_btn.click(
447
  send_cb,
448
  inputs=[user_box, session_list, chatbot, system_box, max_tokens, temperature, top_p, model_choice, dataset_choice],
@@ -458,7 +346,7 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
458
  regen_btn.click(
459
  regenerate_cb,
460
  inputs=[session_list, system_box, max_tokens, temperature, top_p, model_choice, dataset_choice],
461
- outputs=[chatbot]
462
  )
463
 
464
  if __name__ == "__main__":
 
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"
18
 
 
19
  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
  # ---------------------------
 
90
  cur = conn.cursor()
91
  cur.execute("""
92
  SELECT role, content FROM messages
93
+ WHERE session_id = ? ORDER BY id ASC
 
94
  """, (session_id,))
95
  rows = cur.fetchall()
96
  conn.close()
 
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
+ conn.commit()
120
  conn.close()
121
 
122
  # ---------------------------
 
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:
182
  text_lower = user_text.lower()
183
  return any(kw in text_lower for kw in FRONTEND_KEYWORDS)
184
 
 
185
  def send_cb(user_text, selected_label, chatbot_msgs, system_message, max_tokens, temperature, top_p, model_choice, dataset_choice, *args):
186
  sid = label_to_id(selected_label)
187
  if sid is None:
 
189
  labels, _ = list_sessions()
190
  selected_label = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
191
 
 
192
  add_message(sid, "user", user_text)
193
  update_session_title_if_needed(sid, user_text)
194
 
195
  display_msgs = chatbot_msgs[:]
196
  display_msgs.append({"role": "user", "content": user_text})
197
 
 
198
  if is_frontend_request(user_text):
199
  apology = "⚠️ I'm a backend-focused assistant and cannot provide frontend code."
200
  display_msgs.append({"role": "assistant", "content": apology})
 
202
  yield (display_msgs, "", selected_label)
203
  return
204
 
 
205
  display_msgs.append({"role": "assistant", "content": "…"})
206
  yield (display_msgs, "", selected_label)
207
 
 
216
  top_p=float(top_p),
217
  stream=True,
218
  ):
 
219
  if not hasattr(chunk, "choices") or not chunk.choices:
220
  continue
 
221
  choice = chunk.choices[0]
222
  delta = ""
223
  if hasattr(choice, "delta") and choice.delta and getattr(choice.delta, "content", None) is not None:
 
235
  display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
236
  yield (display_msgs, "", selected_label)
237
 
 
238
  def regenerate_cb(selected_label, system_message, max_tokens, temperature, top_p, model_choice, dataset_choice):
239
  sid = label_to_id(selected_label)
240
  if sid is None:
 
244
  if not msgs:
245
  return [], ""
246
 
 
247
  if msgs and msgs[-1]["role"] == "assistant":
248
  conn = db()
249
  cur = conn.cursor()
250
  cur.execute("""
251
+ DELETE FROM messages WHERE id = (
 
252
  SELECT id FROM messages WHERE session_id=? ORDER BY id DESC LIMIT 1
253
  )
254
  """, (sid,))
 
270
  top_p=float(top_p),
271
  stream=True,
272
  ):
 
273
  if not hasattr(chunk, "choices") or not chunk.choices:
274
  continue
 
275
  choice = chunk.choices[0]
276
  delta = ""
277
  if hasattr(choice, "delta") and choice.delta and getattr(choice.delta, "content", None) is not None:
 
289
  display_msgs[-1]["content"] = f"⚠️ Error: {str(e)}"
290
  yield display_msgs
291
 
 
292
  # ---------------------------
293
  # App UI
294
  # ---------------------------
 
297
  if not labels:
298
  first_sid = create_session("New chat")
299
  labels, _ = list_sessions()
300
+
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.Markdown("## 🗄️ LLaMA & Mistral Backend-Focused CRUD Automation — with Persistent History")
305
 
306
  with gr.Row():
307
  with gr.Column(scale=1, min_width=260):
308
+ session_list = gr.Radio(choices=labels, value=default_selected, label="Your chats", interactive=True)
309
+ new_btn = gr.Button("➕ New Chat", variant="primary")
310
+ del_btn = gr.Button("🗑️ Delete", variant="stop")
311
+ refresh_btn = gr.Button("🔄 Refresh", variant="secondary")
 
 
 
 
312
 
313
+ model_choice = gr.Dropdown(choices=list(MODELS.keys()), value=list(MODELS.keys())[0], label="Choose a model", interactive=True)
314
+ dataset_choice = gr.Dropdown(choices=DATASETS, value=DATASETS[0], label="Select a dataset", interactive=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
 
316
+ system_box = gr.Textbox(value=SYSTEM_DEFAULT, label="System message", lines=5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  max_tokens = gr.Slider(256, 4096, value=1200, step=16, label="Max tokens")
318
  temperature = gr.Slider(0.0, 2.0, value=0.25, step=0.05, label="Temperature")
319
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
 
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])
332
+ session_list.change(load_session_cb, inputs=session_list, outputs=chatbot)
 
 
333
 
 
334
  send_btn.click(
335
  send_cb,
336
  inputs=[user_box, session_list, chatbot, system_box, max_tokens, temperature, top_p, model_choice, dataset_choice],
 
346
  regen_btn.click(
347
  regenerate_cb,
348
  inputs=[session_list, system_box, max_tokens, temperature, top_p, model_choice, dataset_choice],
349
+ outputs=chatbot
350
  )
351
 
352
  if __name__ == "__main__":