saad-sust commited on
Commit
d8e4eb9
Β·
verified Β·
1 Parent(s): 8876724

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -7
app.py CHANGED
@@ -2405,12 +2405,35 @@ for i, msg in enumerate(st.session_state.messages):
2405
  avatar = "πŸ§‘β€πŸŽ“" if msg["role"] == "user" else "πŸ“"
2406
  with st.chat_message(msg["role"], avatar=avatar):
2407
  st.markdown(msg["content"])
2408
- # Copy button β€” reliable st.code approach
2409
  if msg["role"] == "assistant":
2410
- if st.button("πŸ“‹ Copy", key=f"copy_{i}", help="Click to see copyable text"):
2411
- st.session_state[f"show_copy_{i}"] = not st.session_state.get(f"show_copy_{i}", False)
2412
- if st.session_state.get(f"show_copy_{i}"):
2413
- st.code(msg["content"], language=None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2414
 
2415
  # ════════════════════════════════════════════════════════════════════
2416
  # INPUT β€” use st.chat_input (cleaner than text_input + button)
@@ -2486,10 +2509,39 @@ if problem and problem != st.session_state.last_submitted:
2486
  answer = ask_ai_streaming(problem, sympy_result, st.session_state.messages)
2487
  plot_graph(problem, sympy_result)
2488
 
2489
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2490
  # Save to history
2491
  if not st.session_state.current_chat_id:
2492
- new_chat() # create chat ID BEFORE appending messages
2493
  st.session_state.messages.append({"role": "user", "content": problem})
2494
  st.session_state.messages.append({"role": "assistant", "content": answer})
2495
  save_current_chat()
 
2405
  avatar = "πŸ§‘β€πŸŽ“" if msg["role"] == "user" else "πŸ“"
2406
  with st.chat_message(msg["role"], avatar=avatar):
2407
  st.markdown(msg["content"])
 
2408
  if msg["role"] == "assistant":
2409
+ escaped = (msg["content"]
2410
+ .replace("\\", "\\\\")
2411
+ .replace("`", "\\`")
2412
+ .replace("$", "\\$")
2413
+ .replace("\n", "\\n")
2414
+ .replace("\r", "")
2415
+ .replace('"', '\\"'))
2416
+ st.markdown(f"""
2417
+ <button onclick="
2418
+ var txt=`{escaped}`;
2419
+ if(navigator.clipboard&&window.isSecureContext){{
2420
+ navigator.clipboard.writeText(txt).then(()=>{{
2421
+ this.innerText='βœ… Copied';
2422
+ setTimeout(()=>this.innerText='πŸ“‹ Copy',1500);
2423
+ }});
2424
+ }}else{{
2425
+ var ta=document.createElement('textarea');
2426
+ ta.value=txt.replace(/\\\\n/g,'\\n');
2427
+ ta.style.position='fixed';ta.style.opacity='0';
2428
+ document.body.appendChild(ta);ta.focus();ta.select();
2429
+ document.execCommand('copy');document.body.removeChild(ta);
2430
+ this.innerText='βœ… Copied';
2431
+ setTimeout(()=>this.innerText='πŸ“‹ Copy',1500);
2432
+ }}
2433
+ " style="background:transparent;border:1px solid #1f2937;color:#4b5563;border-radius:6px;font-size:0.72rem;padding:2px 8px;cursor:pointer;margin-top:4px;transition:all 0.15s;"
2434
+ onmouseover="this.style.borderColor='#3b82f6';this.style.color='#ececec'"
2435
+ onmouseout="this.style.borderColor='#1f2937';this.style.color='#4b5563'">πŸ“‹ Copy</button>
2436
+ """, unsafe_allow_html=True)
2437
 
2438
  # ════════════════════════════════════════════════════════════════════
2439
  # INPUT β€” use st.chat_input (cleaner than text_input + button)
 
2509
  answer = ask_ai_streaming(problem, sympy_result, st.session_state.messages)
2510
  plot_graph(problem, sympy_result)
2511
 
2512
+ # Copy button on live response β€” available immediately
2513
+ escaped = (answer
2514
+ .replace("\\", "\\\\")
2515
+ .replace("`", "\\`")
2516
+ .replace("$", "\\$")
2517
+ .replace("\n", "\\n")
2518
+ .replace("\r", "")
2519
+ .replace('"', '\\"'))
2520
+ st.markdown(f"""
2521
+ <button onclick="
2522
+ var txt=`{escaped}`;
2523
+ if(navigator.clipboard&&window.isSecureContext){{
2524
+ navigator.clipboard.writeText(txt).then(()=>{{
2525
+ this.innerText='βœ… Copied';
2526
+ setTimeout(()=>this.innerText='πŸ“‹ Copy',1500);
2527
+ }});
2528
+ }}else{{
2529
+ var ta=document.createElement('textarea');
2530
+ ta.value=txt.replace(/\\\\n/g,'\\n');
2531
+ ta.style.position='fixed';ta.style.opacity='0';
2532
+ document.body.appendChild(ta);ta.focus();ta.select();
2533
+ document.execCommand('copy');document.body.removeChild(ta);
2534
+ this.innerText='βœ… Copied';
2535
+ setTimeout(()=>this.innerText='πŸ“‹ Copy',1500);
2536
+ }}
2537
+ " style="background:transparent;border:1px solid #1f2937;color:#4b5563;border-radius:6px;font-size:0.72rem;padding:2px 8px;cursor:pointer;margin-top:4px;transition:all 0.15s;"
2538
+ onmouseover="this.style.borderColor='#3b82f6';this.style.color='#ececec'"
2539
+ onmouseout="this.style.borderColor='#1f2937';this.style.color='#4b5563'">πŸ“‹ Copy</button>
2540
+ """, unsafe_allow_html=True)
2541
+
2542
  # Save to history
2543
  if not st.session_state.current_chat_id:
2544
+ new_chat() # create chat ID BEFORE appending β€” avoids wiping messages
2545
  st.session_state.messages.append({"role": "user", "content": problem})
2546
  st.session_state.messages.append({"role": "assistant", "content": answer})
2547
  save_current_chat()