seawolf2357 commited on
Commit
92e3478
ยท
verified ยท
1 Parent(s): e27811d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -402,9 +402,8 @@ def update_session_list():
402
  def load_session_history(selected_session):
403
  try:
404
  if not selected_session:
405
- return gr.update(), gr.update()
406
 
407
- # ์„ธ์…˜ ID ์ถ”์ถœ
408
  session_id = selected_session.split(" (")[0] if " (" in selected_session else selected_session
409
 
410
  conn = sqlite3.connect('chat_history.db')
@@ -418,7 +417,6 @@ def load_session_history(selected_session):
418
  history = c.fetchall()
419
  conn.close()
420
 
421
- # ํ”„๋กฌํ”„ํŠธ ์นด๋“œ์šฉ HTML ์ƒ์„ฑ
422
  cards_html = """
423
  <style>
424
  .prompt-grid {
@@ -445,43 +443,55 @@ def load_session_history(selected_session):
445
  color: #666;
446
  margin-top: 10px;
447
  }
 
 
 
448
  </style>
449
  <div class="prompt-grid">
450
  """
451
 
 
 
 
 
452
  for i, (prompt, response, timestamp) in enumerate(history):
453
- # ํ”„๋กฌํ”„ํŠธ ํ…์ŠคํŠธ๋ฅผ ์ ์ ˆํ•œ ๊ธธ์ด๋กœ ์ž๋ฅด๊ธฐ
454
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
455
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
456
 
 
 
 
457
  cards_html += f"""
458
- <div class="prompt-card" onclick="showResponse({i})">
459
  <div>{short_prompt}</div>
460
  <div class="timestamp">{formatted_time}</div>
461
  </div>
462
  """
463
-
464
  cards_html += "</div>"
465
 
466
- # JavaScript ํ•จ์ˆ˜ ์ถ”๊ฐ€
467
- cards_html += """
468
  <script>
469
- function showResponse(index) {
470
- const responses = %s;
471
- document.getElementById('code-display').innerHTML = `<pre><code>${responses[index]}</code></pre>`;
472
- }
473
- </script>
474
- """ % json.dumps([response for _, response, _ in history])
475
 
476
- # ์ฝ”๋“œ ํ‘œ์‹œ ์˜์—ญ ์ถ”๊ฐ€
477
- cards_html += """
478
- <div id="code-display" style="
479
- margin-top: 20px;
480
- padding: 20px;
481
- background: #f5f5f5;
482
- border-radius: 8px;
483
- display: none;
484
- "></div>
 
 
 
 
 
 
 
485
  """
486
 
487
  return gr.HTML(value=cards_html)
 
402
  def load_session_history(selected_session):
403
  try:
404
  if not selected_session:
405
+ return gr.HTML("")
406
 
 
407
  session_id = selected_session.split(" (")[0] if " (" in selected_session else selected_session
408
 
409
  conn = sqlite3.connect('chat_history.db')
 
417
  history = c.fetchall()
418
  conn.close()
419
 
 
420
  cards_html = """
421
  <style>
422
  .prompt-grid {
 
443
  color: #666;
444
  margin-top: 10px;
445
  }
446
+ #code-display {
447
+ display: none;
448
+ }
449
  </style>
450
  <div class="prompt-grid">
451
  """
452
 
453
+ # ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ JavaScript ๋ฐฐ์—ด๋กœ ์ค€๋น„
454
+ responses_data = []
455
+ prompts_data = []
456
+
457
  for i, (prompt, response, timestamp) in enumerate(history):
 
458
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
459
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
460
 
461
+ responses_data.append(response)
462
+ prompts_data.append(prompt)
463
+
464
  cards_html += f"""
465
+ <div class="prompt-card" onclick="executeHistoryItem({i})">
466
  <div>{short_prompt}</div>
467
  <div class="timestamp">{formatted_time}</div>
468
  </div>
469
  """
470
+
471
  cards_html += "</div>"
472
 
473
+ # JavaScript ํ•จ์ˆ˜ ์ˆ˜์ •
474
+ cards_html += f"""
475
  <script>
476
+ const responses = {json.dumps(responses_data)};
477
+ const prompts = {json.dumps(prompts_data)};
 
 
 
 
478
 
479
+ function executeHistoryItem(index) {{
480
+ // ์ž…๋ ฅ ํ•„๋“œ์— ํ”„๋กฌํ”„ํŠธ ์„ค์ •
481
+ document.querySelector('textarea').value = prompts[index];
482
+
483
+ // ์ฝ”๋“œ ์‹คํ–‰
484
+ const code = responses[index];
485
+ const encodedHtml = btoa(code);
486
+ const dataUri = `data:text/html;charset=utf-8;base64,${{encodedHtml}}`;
487
+
488
+ // iframe ์—…๋ฐ์ดํŠธ
489
+ document.querySelector('.html_content iframe').src = dataUri;
490
+
491
+ // ์„ธ์…˜ ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ
492
+ document.querySelector('.session-drawer').style.display = 'none';
493
+ }}
494
+ </script>
495
  """
496
 
497
  return gr.HTML(value=cards_html)