Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,23 +61,19 @@ class Role:
|
|
| 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 |
-
|
| 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}]
|
|
@@ -446,18 +442,18 @@ def load_json_data():
|
|
| 446 |
}
|
| 447 |
]
|
| 448 |
|
| 449 |
-
# ํ
ํ๋ฆฟ
|
| 450 |
-
|
| 451 |
|
| 452 |
def load_session_history(selected_session=None):
|
| 453 |
-
global
|
| 454 |
|
| 455 |
try:
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
json_data = load_json_data()
|
| 461 |
|
| 462 |
html_content = """
|
| 463 |
<style>
|
|
@@ -466,7 +462,6 @@ def load_session_history(selected_session=None):
|
|
| 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;
|
|
@@ -474,12 +469,11 @@ def load_session_history(selected_session=None):
|
|
| 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,9 +481,6 @@ def load_session_history(selected_session=None):
|
|
| 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;
|
|
@@ -505,23 +496,18 @@ def load_session_history(selected_session=None):
|
|
| 505 |
-webkit-line-clamp: 6;
|
| 506 |
-webkit-box-orient: vertical;
|
| 507 |
overflow: hidden;
|
| 508 |
-
text-overflow: ellipsis;
|
| 509 |
height: 90px;
|
| 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">
|
| 520 |
"""
|
| 521 |
|
| 522 |
for item in json_data:
|
| 523 |
html_content += f"""
|
| 524 |
-
<div class="prompt-card" onclick="copyToInput(
|
| 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>
|
|
@@ -530,31 +516,27 @@ def load_session_history(selected_session=None):
|
|
| 530 |
|
| 531 |
html_content += """
|
| 532 |
<script>
|
| 533 |
-
function copyToInput(
|
|
|
|
| 534 |
const textarea = document.querySelector('.ant-input-textarea-large textarea');
|
| 535 |
if (textarea) {
|
| 536 |
textarea.value = prompt;
|
| 537 |
textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
| 538 |
-
|
| 539 |
-
const closeBtn = document.querySelector('.session-drawer .close-btn');
|
| 540 |
-
if (closeBtn) closeBtn.click();
|
| 541 |
}
|
| 542 |
}
|
| 543 |
</script>
|
| 544 |
</div>
|
| 545 |
"""
|
| 546 |
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
return gr.HTML(value=html_content)
|
| 551 |
|
| 552 |
except Exception as e:
|
| 553 |
print(f"Error in load_session_history: {str(e)}")
|
| 554 |
return gr.HTML("Error loading templates")
|
| 555 |
|
| 556 |
|
| 557 |
-
|
| 558 |
# Demo ์ธ์คํด์ค ์์ฑ
|
| 559 |
demo_instance = Demo()
|
| 560 |
|
|
@@ -706,13 +688,14 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
-
|
| 710 |
# ์ธ์
๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์์
|
| 711 |
sessionBtn.click(
|
| 712 |
fn=lambda: (gr.update(open=True), load_session_history()),
|
| 713 |
inputs=[],
|
| 714 |
-
outputs=[session_drawer, session_history]
|
|
|
|
| 715 |
)
|
|
|
|
| 716 |
# ์ธ์
๋๋ก์ด ๋ซ๊ธฐ ์ด๋ฒคํธ ์์
|
| 717 |
session_drawer.close(
|
| 718 |
lambda: (gr.update(open=False), gr.HTML("")),
|
|
|
|
| 61 |
History = List[Tuple[str, str]]
|
| 62 |
Messages = List[Dict[str, str]]
|
| 63 |
|
| 64 |
+
# ์ด๋ฏธ์ง ์บ์๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
|
| 65 |
+
IMAGE_CACHE = {}
|
| 66 |
+
|
| 67 |
def get_image_base64(image_path):
|
| 68 |
+
if image_path in IMAGE_CACHE:
|
| 69 |
+
return IMAGE_CACHE[image_path]
|
| 70 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
with open(image_path, "rb") as image_file:
|
| 72 |
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 73 |
+
IMAGE_CACHE[image_path] = encoded_string
|
| 74 |
return encoded_string
|
| 75 |
except:
|
| 76 |
+
return IMAGE_CACHE.get('default.png', '')
|
|
|
|
| 77 |
|
| 78 |
def history_to_messages(history: History, system: str) -> Messages:
|
| 79 |
messages = [{'role': Role.SYSTEM, 'content': system}]
|
|
|
|
| 442 |
}
|
| 443 |
]
|
| 444 |
|
| 445 |
+
# ์ ์ญ ๋ณ์๋ก ํ
ํ๋ฆฟ ๋ฐ์ดํฐ ์บ์
|
| 446 |
+
TEMPLATE_CACHE = None
|
| 447 |
|
| 448 |
def load_session_history(selected_session=None):
|
| 449 |
+
global TEMPLATE_CACHE
|
| 450 |
|
| 451 |
try:
|
| 452 |
+
if TEMPLATE_CACHE:
|
| 453 |
+
return TEMPLATE_CACHE
|
| 454 |
+
|
| 455 |
+
# ์ต์ํ์ ์นด๋๋ง ๋จผ์ ๋ก๋ (์ฒซ 12๊ฐ)
|
| 456 |
+
json_data = load_json_data()[:12]
|
| 457 |
|
| 458 |
html_content = """
|
| 459 |
<style>
|
|
|
|
| 462 |
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 463 |
gap: 20px;
|
| 464 |
padding: 20px;
|
|
|
|
| 465 |
}
|
| 466 |
.prompt-card {
|
| 467 |
background: white;
|
|
|
|
| 469 |
border-radius: 8px;
|
| 470 |
padding: 15px;
|
| 471 |
cursor: pointer;
|
|
|
|
| 472 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
|
|
|
| 473 |
}
|
| 474 |
.prompt-card:hover {
|
| 475 |
transform: translateY(-2px);
|
| 476 |
+
transition: transform 0.2s;
|
| 477 |
}
|
| 478 |
.card-image {
|
| 479 |
width: 100%;
|
|
|
|
| 481 |
object-fit: cover;
|
| 482 |
border-radius: 4px;
|
| 483 |
margin-bottom: 10px;
|
|
|
|
|
|
|
|
|
|
| 484 |
}
|
| 485 |
.card-name {
|
| 486 |
font-weight: bold;
|
|
|
|
| 496 |
-webkit-line-clamp: 6;
|
| 497 |
-webkit-box-orient: vertical;
|
| 498 |
overflow: hidden;
|
|
|
|
| 499 |
height: 90px;
|
| 500 |
background-color: #f8f9fa;
|
| 501 |
padding: 8px;
|
| 502 |
border-radius: 4px;
|
| 503 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
</style>
|
| 505 |
+
<div class="prompt-grid" id="promptGrid">
|
| 506 |
"""
|
| 507 |
|
| 508 |
for item in json_data:
|
| 509 |
html_content += f"""
|
| 510 |
+
<div class="prompt-card" onclick="copyToInput(this)" data-prompt="{html.escape(item.get('prompt', ''))}">
|
| 511 |
<img src="{item.get('image_url', '')}" class="card-image" loading="lazy" alt="{html.escape(item.get('name', ''))}">
|
| 512 |
<div class="card-name">{html.escape(item.get('name', ''))}</div>
|
| 513 |
<div class="card-prompt">{html.escape(item.get('prompt', ''))}</div>
|
|
|
|
| 516 |
|
| 517 |
html_content += """
|
| 518 |
<script>
|
| 519 |
+
function copyToInput(card) {
|
| 520 |
+
const prompt = card.dataset.prompt;
|
| 521 |
const textarea = document.querySelector('.ant-input-textarea-large textarea');
|
| 522 |
if (textarea) {
|
| 523 |
textarea.value = prompt;
|
| 524 |
textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
| 525 |
+
document.querySelector('.session-drawer .close-btn').click();
|
|
|
|
|
|
|
| 526 |
}
|
| 527 |
}
|
| 528 |
</script>
|
| 529 |
</div>
|
| 530 |
"""
|
| 531 |
|
| 532 |
+
TEMPLATE_CACHE = gr.HTML(value=html_content)
|
| 533 |
+
return TEMPLATE_CACHE
|
|
|
|
|
|
|
| 534 |
|
| 535 |
except Exception as e:
|
| 536 |
print(f"Error in load_session_history: {str(e)}")
|
| 537 |
return gr.HTML("Error loading templates")
|
| 538 |
|
| 539 |
|
|
|
|
| 540 |
# Demo ์ธ์คํด์ค ์์ฑ
|
| 541 |
demo_instance = Demo()
|
| 542 |
|
|
|
|
| 688 |
|
| 689 |
|
| 690 |
|
|
|
|
| 691 |
# ์ธ์
๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์์
|
| 692 |
sessionBtn.click(
|
| 693 |
fn=lambda: (gr.update(open=True), load_session_history()),
|
| 694 |
inputs=[],
|
| 695 |
+
outputs=[session_drawer, session_history],
|
| 696 |
+
queue=False # ํ ๋นํ์ฑํ๋ก ์ฆ์ ์คํ
|
| 697 |
)
|
| 698 |
+
|
| 699 |
# ์ธ์
๋๋ก์ด ๋ซ๊ธฐ ์ด๋ฒคํธ ์์
|
| 700 |
session_drawer.close(
|
| 701 |
lambda: (gr.update(open=False), gr.HTML("")),
|