seawolf2357 commited on
Commit
f735461
ยท
verified ยท
1 Parent(s): e344b68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -16
app.py CHANGED
@@ -61,6 +61,24 @@ class Role:
61
  History = List[Tuple[str, str]]
62
  Messages = List[Dict[str, str]]
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def history_to_messages(history: History, system: str) -> Messages:
65
  messages = [{'role': Role.SYSTEM, 'content': system}]
66
  for h in history:
@@ -234,10 +252,7 @@ def send_to_sandbox(code):
234
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
235
  return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
236
 
237
- def get_image_base64(image_path):
238
- with open(image_path, "rb") as image_file:
239
- encoded_string = base64.b64encode(image_file.read()).decode()
240
- return encoded_string
241
 
242
  theme = gr.themes.Soft()
243
 
@@ -431,31 +446,40 @@ def load_json_data():
431
  }
432
  ]
433
 
 
 
 
434
  def load_session_history(selected_session=None):
 
 
435
  try:
 
 
 
 
436
  json_data = load_json_data()
437
 
438
  html_content = """
439
  <style>
440
  .prompt-grid {
441
  display: grid;
442
- grid-template-columns: repeat(3, 1fr);
443
  gap: 20px;
444
  padding: 20px;
 
445
  }
446
  .prompt-card {
447
  background: white;
448
  border: 1px solid #eee;
449
  border-radius: 8px;
450
  padding: 15px;
451
- transition: all 0.3s ease;
452
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
453
- min-height: 300px;
454
  cursor: pointer;
 
 
 
455
  }
456
  .prompt-card:hover {
457
  transform: translateY(-2px);
458
- box-shadow: 0 4px 10px rgba(0,0,0,0.15);
459
  }
460
  .card-image {
461
  width: 100%;
@@ -463,6 +487,9 @@ def load_session_history(selected_session=None):
463
  object-fit: cover;
464
  border-radius: 4px;
465
  margin-bottom: 10px;
 
 
 
466
  }
467
  .card-name {
468
  font-weight: bold;
@@ -483,7 +510,10 @@ def load_session_history(selected_session=None):
483
  background-color: #f8f9fa;
484
  padding: 8px;
485
  border-radius: 4px;
486
- border: 1px solid #eee;
 
 
 
487
  }
488
  </style>
489
  <div class="prompt-grid">
@@ -491,17 +521,30 @@ def load_session_history(selected_session=None):
491
 
492
  for item in json_data:
493
  html_content += f"""
494
- <div class="prompt-card">
495
- <img src="{item.get('image_url', '')}" class="card-image" alt="{html.escape(item.get('name', ''))}">
496
  <div class="card-name">{html.escape(item.get('name', ''))}</div>
497
  <div class="card-prompt">{html.escape(item.get('prompt', ''))}</div>
498
  </div>
499
  """
500
 
501
  html_content += """
 
 
 
 
 
 
 
 
 
 
502
  </div>
503
  """
504
 
 
 
 
505
  return gr.HTML(value=html_content)
506
 
507
  except Exception as e:
@@ -512,8 +555,6 @@ def load_session_history(selected_session=None):
512
  demo_instance = Demo()
513
 
514
 
515
-
516
-
517
  with gr.Blocks(css_paths="app.css",theme=theme) as demo:
518
  history = gr.State([])
519
  setting = gr.State({
@@ -661,12 +702,24 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
661
 
662
 
663
  # ์„ธ์…˜ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ˆ˜์ •
664
-
665
  sessionBtn.click(
666
  fn=lambda: (gr.update(open=True), load_session_history()),
667
  inputs=[],
668
- outputs=[session_drawer, session_history]
 
 
 
 
 
 
 
 
 
 
 
669
  )
 
 
670
  # ์„ธ์…˜ ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ ์ด๋ฒคํŠธ ์ˆ˜์ •
671
  session_drawer.close(
672
  lambda: (gr.update(open=False), gr.HTML("")),
 
61
  History = List[Tuple[str, str]]
62
  Messages = List[Dict[str, str]]
63
 
64
+ def get_image_base64(image_path):
65
+ try:
66
+ # ์ด๋ฏธ์ง€ ์บ์‹ฑ์„ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ
67
+ if not hasattr(get_image_base64, 'cache'):
68
+ get_image_base64.cache = {}
69
+
70
+ # ์บ์‹œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์œผ๋ฉด ๋ฐ˜ํ™˜
71
+ if image_path in get_image_base64.cache:
72
+ return get_image_base64.cache[image_path]
73
+
74
+ with open(image_path, "rb") as image_file:
75
+ encoded_string = base64.b64encode(image_file.read()).decode()
76
+ get_image_base64.cache[image_path] = encoded_string
77
+ return encoded_string
78
+ except:
79
+ # ์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹คํŒจ์‹œ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ๋ฐ˜ํ™˜
80
+ return get_image_base64('default.png')
81
+
82
  def history_to_messages(history: History, system: str) -> Messages:
83
  messages = [{'role': Role.SYSTEM, 'content': system}]
84
  for h in history:
 
252
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
253
  return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
254
 
255
+
 
 
 
256
 
257
  theme = gr.themes.Soft()
258
 
 
446
  }
447
  ]
448
 
449
+ # ์ „์—ญ ๋ณ€์ˆ˜๋กœ ํ…œํ”Œ๋ฆฟ HTML ์บ์‹œ ์ €์žฅ
450
+ template_html_cache = None
451
+
452
  def load_session_history(selected_session=None):
453
+ global template_html_cache
454
+
455
  try:
456
+ # ์บ์‹œ๋œ HTML์ด ์žˆ์œผ๋ฉด ๋ฐ”๋กœ ๋ฐ˜ํ™˜
457
+ if template_html_cache:
458
+ return gr.HTML(value=template_html_cache)
459
+
460
  json_data = load_json_data()
461
 
462
  html_content = """
463
  <style>
464
  .prompt-grid {
465
  display: grid;
466
+ grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
467
  gap: 20px;
468
  padding: 20px;
469
+ contain: content;
470
  }
471
  .prompt-card {
472
  background: white;
473
  border: 1px solid #eee;
474
  border-radius: 8px;
475
  padding: 15px;
 
 
 
476
  cursor: pointer;
477
+ transition: transform 0.2s ease;
478
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
479
+ will-change: transform;
480
  }
481
  .prompt-card:hover {
482
  transform: translateY(-2px);
 
483
  }
484
  .card-image {
485
  width: 100%;
 
487
  object-fit: cover;
488
  border-radius: 4px;
489
  margin-bottom: 10px;
490
+ background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
491
+ background-size: 200% 100%;
492
+ animation: loading 1.5s infinite;
493
  }
494
  .card-name {
495
  font-weight: bold;
 
510
  background-color: #f8f9fa;
511
  padding: 8px;
512
  border-radius: 4px;
513
+ }
514
+ @keyframes loading {
515
+ 0% { background-position: 200% 0; }
516
+ 100% { background-position: -200% 0; }
517
  }
518
  </style>
519
  <div class="prompt-grid">
 
521
 
522
  for item in json_data:
523
  html_content += f"""
524
+ <div class="prompt-card" onclick="copyPrompt(this)" data-prompt="{html.escape(item.get('prompt', ''))}">
525
+ <img src="{item.get('image_url', '')}" class="card-image" loading="lazy" alt="{html.escape(item.get('name', ''))}">
526
  <div class="card-name">{html.escape(item.get('name', ''))}</div>
527
  <div class="card-prompt">{html.escape(item.get('prompt', ''))}</div>
528
  </div>
529
  """
530
 
531
  html_content += """
532
+ <script>
533
+ function copyPrompt(card) {
534
+ const prompt = card.getAttribute('data-prompt');
535
+ const textarea = document.querySelector('.ant-input-textarea-large textarea');
536
+ if (textarea) {
537
+ textarea.value = prompt;
538
+ textarea.dispatchEvent(new Event('input', { bubbles: true }));
539
+ }
540
+ }
541
+ </script>
542
  </div>
543
  """
544
 
545
+ # HTML ์บ์‹œ ์ €์žฅ
546
+ template_html_cache = html_content
547
+
548
  return gr.HTML(value=html_content)
549
 
550
  except Exception as e:
 
555
  demo_instance = Demo()
556
 
557
 
 
 
558
  with gr.Blocks(css_paths="app.css",theme=theme) as demo:
559
  history = gr.State([])
560
  setting = gr.State({
 
702
 
703
 
704
  # ์„ธ์…˜ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ˆ˜์ •
 
705
  sessionBtn.click(
706
  fn=lambda: (gr.update(open=True), load_session_history()),
707
  inputs=[],
708
+ outputs=[session_drawer, session_history],
709
+ _js="""
710
+ () => {
711
+ document.body.style.cursor = 'wait';
712
+ const drawer = document.querySelector('.session-drawer');
713
+ if (drawer) drawer.style.opacity = '0';
714
+ setTimeout(() => {
715
+ if (drawer) drawer.style.opacity = '1';
716
+ document.body.style.cursor = 'default';
717
+ }, 100);
718
+ }
719
+ """
720
  )
721
+
722
+
723
  # ์„ธ์…˜ ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ ์ด๋ฒคํŠธ ์ˆ˜์ •
724
  session_drawer.close(
725
  lambda: (gr.update(open=False), gr.HTML("")),