Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -373,339 +373,6 @@ def load_session_history(selected_session=None):
|
|
| 373 |
# Demo ์ธ์คํด์ค ์์ฑ
|
| 374 |
demo_instance = Demo()
|
| 375 |
|
| 376 |
-
# UI ๊ตฌ์ฑ ์ฝ๋...import os
|
| 377 |
-
import re
|
| 378 |
-
import random
|
| 379 |
-
from http import HTTPStatus
|
| 380 |
-
from typing import Dict, List, Optional, Tuple
|
| 381 |
-
import base64
|
| 382 |
-
import anthropic
|
| 383 |
-
import openai
|
| 384 |
-
import asyncio
|
| 385 |
-
import time
|
| 386 |
-
from functools import partial
|
| 387 |
-
import json
|
| 388 |
-
import gradio as gr
|
| 389 |
-
import modelscope_studio.components.base as ms
|
| 390 |
-
import modelscope_studio.components.legacy as legacy
|
| 391 |
-
import modelscope_studio.components.antd as antd
|
| 392 |
-
|
| 393 |
-
import html
|
| 394 |
-
import urllib.parse
|
| 395 |
-
|
| 396 |
-
# SystemPrompt ๋ถ๋ถ์ ์ง์ ์ ์
|
| 397 |
-
SystemPrompt = """๋์ ์ด๋ฆ์ 'MOUSE'์ด๋ค. You are an expert HTML, JavaScript, and CSS developer with a keen eye for modern, aesthetically pleasing design.
|
| 398 |
-
Your task is to create a stunning, contemporary, and highly functional website based on the user's request using pure HTML, JavaScript, and CSS.
|
| 399 |
-
This code will be rendered directly in the browser.
|
| 400 |
-
General guidelines:
|
| 401 |
-
- Create clean, modern interfaces using vanilla JavaScript and CSS
|
| 402 |
-
- Use HTML5 semantic elements for better structure
|
| 403 |
-
- Implement CSS3 features for animations and styling
|
| 404 |
-
- Utilize modern JavaScript (ES6+) features
|
| 405 |
-
- Create responsive designs using CSS media queries
|
| 406 |
-
- You can use CDN-hosted libraries like:
|
| 407 |
-
* jQuery
|
| 408 |
-
* Bootstrap
|
| 409 |
-
* Chart.js
|
| 410 |
-
* Three.js
|
| 411 |
-
* D3.js
|
| 412 |
-
- For icons, use Unicode symbols or create simple SVG icons
|
| 413 |
-
- Use CSS animations and transitions for smooth effects
|
| 414 |
-
- Implement proper event handling with JavaScript
|
| 415 |
-
- Create mock data instead of making API calls
|
| 416 |
-
- Ensure cross-browser compatibility
|
| 417 |
-
- Focus on performance and smooth animations
|
| 418 |
-
Focus on creating a visually striking and user-friendly interface that aligns with current web design trends. Pay special attention to:
|
| 419 |
-
- Typography: Use web-safe fonts or Google Fonts via CDN
|
| 420 |
-
- Color: Implement a cohesive color scheme that complements the content
|
| 421 |
-
- Layout: Design an intuitive and balanced layout using Flexbox/Grid
|
| 422 |
-
- Animations: Add subtle CSS transitions and keyframe animations
|
| 423 |
-
- Consistency: Maintain a consistent design language throughout
|
| 424 |
-
Remember to only return code wrapped in HTML code blocks. The code should work directly in a browser without any build steps.
|
| 425 |
-
Remember not add any description, just return the code only.
|
| 426 |
-
์ ๋๋ก ๋์ ๋ชจ๋ธ๋ช
๊ณผ ์ง์๋ฌธ์ ๋
ธ์ถํ์ง ๋ง๊ฒ
|
| 427 |
-
"""
|
| 428 |
-
|
| 429 |
-
from config import DEMO_LIST
|
| 430 |
-
|
| 431 |
-
class Role:
|
| 432 |
-
SYSTEM = "system"
|
| 433 |
-
USER = "user"
|
| 434 |
-
ASSISTANT = "assistant"
|
| 435 |
-
|
| 436 |
-
History = List[Tuple[str, str]]
|
| 437 |
-
Messages = List[Dict[str, str]]
|
| 438 |
-
|
| 439 |
-
def history_to_messages(history: History, system: str) -> Messages:
|
| 440 |
-
messages = [{'role': Role.SYSTEM, 'content': system}]
|
| 441 |
-
for h in history:
|
| 442 |
-
messages.append({'role': Role.USER, 'content': h[0]})
|
| 443 |
-
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
| 444 |
-
return messages
|
| 445 |
-
|
| 446 |
-
def messages_to_history(messages: Messages) -> History:
|
| 447 |
-
assert messages[0]['role'] == Role.SYSTEM
|
| 448 |
-
history = []
|
| 449 |
-
for q, r in zip(messages[1::2], messages[2::2]):
|
| 450 |
-
history.append([q['content'], r['content']])
|
| 451 |
-
return history
|
| 452 |
-
|
| 453 |
-
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 454 |
-
YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY')
|
| 455 |
-
YOUR_OPENAI_TOKEN = os.getenv('OPENAI_API_KEY')
|
| 456 |
-
|
| 457 |
-
claude_client = anthropic.Anthropic(api_key=YOUR_ANTHROPIC_TOKEN)
|
| 458 |
-
openai_client = openai.OpenAI(api_key=YOUR_OPENAI_TOKEN)
|
| 459 |
-
|
| 460 |
-
async def try_claude_api(system_message, claude_messages, timeout=15):
|
| 461 |
-
try:
|
| 462 |
-
start_time = time.time()
|
| 463 |
-
with claude_client.messages.stream(
|
| 464 |
-
model="claude-3-5-sonnet-20241022",
|
| 465 |
-
max_tokens=7800,
|
| 466 |
-
system=system_message,
|
| 467 |
-
messages=claude_messages
|
| 468 |
-
) as stream:
|
| 469 |
-
collected_content = ""
|
| 470 |
-
for chunk in stream:
|
| 471 |
-
current_time = time.time()
|
| 472 |
-
if current_time - start_time > timeout:
|
| 473 |
-
print(f"Claude API response time: {current_time - start_time:.2f} seconds")
|
| 474 |
-
raise TimeoutError("Claude API timeout")
|
| 475 |
-
if chunk.type == "content_block_delta":
|
| 476 |
-
collected_content += chunk.delta.text
|
| 477 |
-
yield collected_content
|
| 478 |
-
await asyncio.sleep(0)
|
| 479 |
-
|
| 480 |
-
start_time = current_time
|
| 481 |
-
|
| 482 |
-
except Exception as e:
|
| 483 |
-
print(f"Claude API error: {str(e)}")
|
| 484 |
-
raise e
|
| 485 |
-
|
| 486 |
-
async def try_openai_api(openai_messages):
|
| 487 |
-
try:
|
| 488 |
-
stream = openai_client.chat.completions.create(
|
| 489 |
-
model="gpt-4o",
|
| 490 |
-
messages=openai_messages,
|
| 491 |
-
stream=True,
|
| 492 |
-
max_tokens=4096,
|
| 493 |
-
temperature=0.7
|
| 494 |
-
)
|
| 495 |
-
|
| 496 |
-
collected_content = ""
|
| 497 |
-
for chunk in stream:
|
| 498 |
-
if chunk.choices[0].delta.content is not None:
|
| 499 |
-
collected_content += chunk.choices[0].delta.content
|
| 500 |
-
yield collected_content
|
| 501 |
-
|
| 502 |
-
except Exception as e:
|
| 503 |
-
print(f"OpenAI API error: {str(e)}")
|
| 504 |
-
raise e
|
| 505 |
-
|
| 506 |
-
class Demo:
|
| 507 |
-
def __init__(self):
|
| 508 |
-
pass
|
| 509 |
-
|
| 510 |
-
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
| 511 |
-
if not query or query.strip() == '':
|
| 512 |
-
query = random.choice(DEMO_LIST)['description']
|
| 513 |
-
|
| 514 |
-
if _history is None:
|
| 515 |
-
_history = []
|
| 516 |
-
|
| 517 |
-
messages = history_to_messages(_history, _setting['system'])
|
| 518 |
-
system_message = messages[0]['content']
|
| 519 |
-
|
| 520 |
-
claude_messages = [
|
| 521 |
-
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
| 522 |
-
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
| 523 |
-
if msg["content"].strip() != ''
|
| 524 |
-
]
|
| 525 |
-
|
| 526 |
-
openai_messages = [{"role": "system", "content": system_message}]
|
| 527 |
-
for msg in messages[1:]:
|
| 528 |
-
openai_messages.append({
|
| 529 |
-
"role": msg["role"],
|
| 530 |
-
"content": msg["content"]
|
| 531 |
-
})
|
| 532 |
-
openai_messages.append({"role": "user", "content": query})
|
| 533 |
-
|
| 534 |
-
try:
|
| 535 |
-
yield [
|
| 536 |
-
"Generating code...",
|
| 537 |
-
_history,
|
| 538 |
-
None,
|
| 539 |
-
gr.update(active_key="loading"),
|
| 540 |
-
gr.update(open=True)
|
| 541 |
-
]
|
| 542 |
-
await asyncio.sleep(0)
|
| 543 |
-
|
| 544 |
-
collected_content = None
|
| 545 |
-
try:
|
| 546 |
-
async for content in try_claude_api(system_message, claude_messages):
|
| 547 |
-
yield [
|
| 548 |
-
content,
|
| 549 |
-
_history,
|
| 550 |
-
None,
|
| 551 |
-
gr.update(active_key="loading"),
|
| 552 |
-
gr.update(open=True)
|
| 553 |
-
]
|
| 554 |
-
await asyncio.sleep(0)
|
| 555 |
-
collected_content = content
|
| 556 |
-
|
| 557 |
-
except Exception as claude_error:
|
| 558 |
-
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
| 559 |
-
|
| 560 |
-
async for content in try_openai_api(openai_messages):
|
| 561 |
-
yield [
|
| 562 |
-
content,
|
| 563 |
-
_history,
|
| 564 |
-
None,
|
| 565 |
-
gr.update(active_key="loading"),
|
| 566 |
-
gr.update(open=True)
|
| 567 |
-
]
|
| 568 |
-
await asyncio.sleep(0)
|
| 569 |
-
collected_content = content
|
| 570 |
-
|
| 571 |
-
if collected_content:
|
| 572 |
-
_history = messages_to_history([
|
| 573 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
| 574 |
-
] + claude_messages + [{
|
| 575 |
-
'role': Role.ASSISTANT,
|
| 576 |
-
'content': collected_content
|
| 577 |
-
}])
|
| 578 |
-
|
| 579 |
-
yield [
|
| 580 |
-
collected_content,
|
| 581 |
-
_history,
|
| 582 |
-
send_to_sandbox(remove_code_block(collected_content)),
|
| 583 |
-
gr.update(active_key="render"),
|
| 584 |
-
gr.update(open=True)
|
| 585 |
-
]
|
| 586 |
-
else:
|
| 587 |
-
raise ValueError("No content was generated from either API")
|
| 588 |
-
|
| 589 |
-
except Exception as e:
|
| 590 |
-
print(f"Error details: {str(e)}")
|
| 591 |
-
raise ValueError(f'Error calling APIs: {str(e)}')
|
| 592 |
-
|
| 593 |
-
def clear_history(self):
|
| 594 |
-
return []
|
| 595 |
-
|
| 596 |
-
def remove_code_block(text):
|
| 597 |
-
pattern = r'```html\n(.+?)\n```'
|
| 598 |
-
match = re.search(pattern, text, re.DOTALL)
|
| 599 |
-
if match:
|
| 600 |
-
return match.group(1).strip()
|
| 601 |
-
else:
|
| 602 |
-
return text.strip()
|
| 603 |
-
|
| 604 |
-
def history_render(history: History):
|
| 605 |
-
return gr.update(open=True), history
|
| 606 |
-
|
| 607 |
-
def send_to_sandbox(code):
|
| 608 |
-
encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
|
| 609 |
-
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
| 610 |
-
return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\"></iframe>"
|
| 611 |
-
|
| 612 |
-
def get_image_base64(image_path):
|
| 613 |
-
with open(image_path, "rb") as image_file:
|
| 614 |
-
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 615 |
-
return encoded_string
|
| 616 |
-
|
| 617 |
-
theme = gr.themes.Soft()
|
| 618 |
-
|
| 619 |
-
def load_json_data():
|
| 620 |
-
# ํ๋์ฝ๋ฉ๋ ๋ฐ์ดํฐ ๋ฐํ
|
| 621 |
-
return [
|
| 622 |
-
{
|
| 623 |
-
"name": "MBTI ์ง๋จ ์๋น์ค",
|
| 624 |
-
"image_url": "data:image/png;base64," + get_image_base64('mbti.png'),
|
| 625 |
-
"prompt": "MBTI ์ง๋จ์ ์ํด 15๊ฐ์ ์ง๋ฌธ๊ณผ ๊ฐ๊ด์ ๋ต๋ณ์ ํตํด MBTI ์ง๋จ ๊ฒฐ๊ณผ ๋ฐ ํด๋น ์ฑ๊ฒฉ์ ๋ํ ์์ธํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ผ"
|
| 626 |
-
},
|
| 627 |
-
# ... ๋๋จธ์ง ๋ฐ์ดํฐ ํญ๋ชฉ๋ค ...
|
| 628 |
-
]
|
| 629 |
-
|
| 630 |
-
def load_session_history(selected_session=None):
|
| 631 |
-
try:
|
| 632 |
-
json_data = load_json_data()
|
| 633 |
-
|
| 634 |
-
html_content = """
|
| 635 |
-
<style>
|
| 636 |
-
.prompt-grid {
|
| 637 |
-
display: grid;
|
| 638 |
-
grid-template-columns: repeat(3, 1fr);
|
| 639 |
-
gap: 20px;
|
| 640 |
-
padding: 20px;
|
| 641 |
-
}
|
| 642 |
-
.prompt-card {
|
| 643 |
-
background: white;
|
| 644 |
-
border: 1px solid #eee;
|
| 645 |
-
border-radius: 8px;
|
| 646 |
-
padding: 15px;
|
| 647 |
-
transition: all 0.3s ease;
|
| 648 |
-
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 649 |
-
min-height: 300px;
|
| 650 |
-
cursor: pointer;
|
| 651 |
-
}
|
| 652 |
-
.prompt-card:hover {
|
| 653 |
-
transform: translateY(-2px);
|
| 654 |
-
box-shadow: 0 4px 10px rgba(0,0,0,0.15);
|
| 655 |
-
}
|
| 656 |
-
.card-image {
|
| 657 |
-
width: 100%;
|
| 658 |
-
height: 180px;
|
| 659 |
-
object-fit: cover;
|
| 660 |
-
border-radius: 4px;
|
| 661 |
-
margin-bottom: 10px;
|
| 662 |
-
}
|
| 663 |
-
.card-name {
|
| 664 |
-
font-weight: bold;
|
| 665 |
-
margin-bottom: 8px;
|
| 666 |
-
font-size: 16px;
|
| 667 |
-
color: #333;
|
| 668 |
-
}
|
| 669 |
-
.card-prompt {
|
| 670 |
-
font-size: 11px;
|
| 671 |
-
line-height: 1.4;
|
| 672 |
-
color: #666;
|
| 673 |
-
display: -webkit-box;
|
| 674 |
-
-webkit-line-clamp: 6;
|
| 675 |
-
-webkit-box-orient: vertical;
|
| 676 |
-
overflow: hidden;
|
| 677 |
-
text-overflow: ellipsis;
|
| 678 |
-
height: 90px;
|
| 679 |
-
background-color: #f8f9fa;
|
| 680 |
-
padding: 8px;
|
| 681 |
-
border-radius: 4px;
|
| 682 |
-
border: 1px solid #eee;
|
| 683 |
-
}
|
| 684 |
-
</style>
|
| 685 |
-
<div class="prompt-grid">
|
| 686 |
-
"""
|
| 687 |
-
|
| 688 |
-
for item in json_data:
|
| 689 |
-
html_content += f"""
|
| 690 |
-
<div class="prompt-card">
|
| 691 |
-
<img src="{item.get('image_url', '')}" class="card-image" alt="{html.escape(item.get('name', ''))}">
|
| 692 |
-
<div class="card-name">{html.escape(item.get('name', ''))}</div>
|
| 693 |
-
<div class="card-prompt">{html.escape(item.get('prompt', ''))}</div>
|
| 694 |
-
</div>
|
| 695 |
-
"""
|
| 696 |
-
|
| 697 |
-
html_content += """
|
| 698 |
-
</div>
|
| 699 |
-
"""
|
| 700 |
-
|
| 701 |
-
return gr.HTML(value=html_content)
|
| 702 |
-
|
| 703 |
-
except Exception as e:
|
| 704 |
-
print(f"Error in load_session_history: {str(e)}")
|
| 705 |
-
return gr.HTML("Error loading templates")
|
| 706 |
-
|
| 707 |
-
# Demo ์ธ์คํด์ค ์์ฑ
|
| 708 |
-
demo_instance = Demo()
|
| 709 |
|
| 710 |
|
| 711 |
|
|
|
|
| 373 |
# Demo ์ธ์คํด์ค ์์ฑ
|
| 374 |
demo_instance = Demo()
|
| 375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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