Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -256,35 +256,28 @@ class Demo:
|
|
| 256 |
def __init__(self):
|
| 257 |
pass
|
| 258 |
|
| 259 |
-
async def generation_code(self, query: Optional[str], _setting: Dict[str, str]
|
| 260 |
if not query or query.strip() == '':
|
| 261 |
query = get_random_placeholder()
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
messages = history_to_messages(_history, _setting['system'])
|
| 267 |
system_message = messages[0]['content']
|
| 268 |
-
|
| 269 |
claude_messages = [
|
| 270 |
-
{"role":
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
| 273 |
]
|
| 274 |
-
|
| 275 |
-
openai_messages = [{"role": "system", "content": system_message}]
|
| 276 |
-
for msg in messages[1:]:
|
| 277 |
-
openai_messages.append({
|
| 278 |
-
"role": msg["role"],
|
| 279 |
-
"content": msg["content"]
|
| 280 |
-
})
|
| 281 |
-
openai_messages.append({"role": "user", "content": query})
|
| 282 |
|
| 283 |
try:
|
| 284 |
# ๋ก๋ฉ ์์
|
| 285 |
yield [
|
| 286 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
| 287 |
-
_history,
|
| 288 |
None,
|
| 289 |
gr.update(active_key="loading"),
|
| 290 |
gr.update(open=True)
|
|
@@ -296,7 +289,6 @@ class Demo:
|
|
| 296 |
async for content in try_claude_api(system_message, claude_messages):
|
| 297 |
yield [
|
| 298 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
| 299 |
-
_history,
|
| 300 |
None,
|
| 301 |
gr.update(active_key="loading"),
|
| 302 |
gr.update(open=True)
|
|
@@ -310,7 +302,6 @@ class Demo:
|
|
| 310 |
async for content in try_openai_api(openai_messages):
|
| 311 |
yield [
|
| 312 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
| 313 |
-
_history,
|
| 314 |
None,
|
| 315 |
gr.update(active_key="loading"),
|
| 316 |
gr.update(open=True)
|
|
@@ -319,17 +310,9 @@ class Demo:
|
|
| 319 |
collected_content = content
|
| 320 |
|
| 321 |
if collected_content:
|
| 322 |
-
_history = messages_to_history([
|
| 323 |
-
{'role': Role.SYSTEM, 'content': system_message}
|
| 324 |
-
] + claude_messages + [{
|
| 325 |
-
'role': Role.ASSISTANT,
|
| 326 |
-
'content': collected_content
|
| 327 |
-
}])
|
| 328 |
-
|
| 329 |
# ์ต์ข
๊ฒฐ๊ณผ๋ง ํ์
|
| 330 |
yield [
|
| 331 |
collected_content,
|
| 332 |
-
_history,
|
| 333 |
send_to_sandbox(remove_code_block(collected_content)),
|
| 334 |
gr.update(active_key="render"),
|
| 335 |
gr.update(open=False)
|
|
@@ -342,9 +325,6 @@ class Demo:
|
|
| 342 |
print(f"Error details: {str(e)}")
|
| 343 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
| 344 |
|
| 345 |
-
def clear_history(self):
|
| 346 |
-
return []
|
| 347 |
-
|
| 348 |
def remove_code_block(text):
|
| 349 |
pattern = r'```html\n(.+?)\n```'
|
| 350 |
match = re.search(pattern, text, re.DOTALL)
|
|
@@ -482,6 +462,13 @@ def create_main_interface():
|
|
| 482 |
custom_css = f.read()
|
| 483 |
|
| 484 |
demo = gr.Blocks(css=custom_css + """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
.container {
|
| 486 |
background: #f0f0f0;
|
| 487 |
min-height: 100vh;
|
|
@@ -591,6 +578,20 @@ def create_main_interface():
|
|
| 591 |
gr.HTML("""
|
| 592 |
<div class="thinking-container">
|
| 593 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
.thinking-container {
|
| 595 |
text-align: center;
|
| 596 |
padding: 20px;
|
|
@@ -756,14 +757,8 @@ def create_main_interface():
|
|
| 756 |
size="large",
|
| 757 |
allow_clear=True,
|
| 758 |
placeholder=get_random_placeholder(),
|
| 759 |
-
style
|
| 760 |
-
|
| 761 |
-
"border": "1px solid #e0e0e0",
|
| 762 |
-
"borderRadius": "10px",
|
| 763 |
-
"padding": "15px",
|
| 764 |
-
"minHeight": "150px",
|
| 765 |
-
"fontFamily": "-apple-system, BlinkMacSystemFont, sans-serif"
|
| 766 |
-
}
|
| 767 |
)
|
| 768 |
|
| 769 |
# ๋ฒํผ ๊ทธ๋ฃน
|
|
@@ -772,33 +767,22 @@ def create_main_interface():
|
|
| 772 |
"Generate",
|
| 773 |
type="primary",
|
| 774 |
size="large",
|
| 775 |
-
|
| 776 |
-
"background": "#007aff",
|
| 777 |
-
"borderRadius": "8px",
|
| 778 |
-
"boxShadow": "0 2px 4px rgba(0,0,0,0.1)"
|
| 779 |
-
}
|
| 780 |
)
|
| 781 |
boost_btn = antd.Button(
|
| 782 |
"Enhance",
|
| 783 |
type="default",
|
| 784 |
size="large",
|
| 785 |
-
|
| 786 |
-
"borderRadius": "8px",
|
| 787 |
-
"border": "1px solid #007aff",
|
| 788 |
-
"color": "#007aff"
|
| 789 |
-
}
|
| 790 |
)
|
| 791 |
deploy_btn = antd.Button(
|
| 792 |
"Share",
|
| 793 |
type="default",
|
| 794 |
size="large",
|
| 795 |
-
|
| 796 |
-
"borderRadius": "8px",
|
| 797 |
-
"border": "1px solid #28c840",
|
| 798 |
-
"color": "#28c840"
|
| 799 |
-
}
|
| 800 |
)
|
| 801 |
|
|
|
|
| 802 |
deploy_result = gr.HTML(
|
| 803 |
label="Share Result",
|
| 804 |
elem_classes="deploy-result"
|
|
@@ -826,13 +810,9 @@ def create_main_interface():
|
|
| 826 |
with antd.Tabs.Item(key="empty"):
|
| 827 |
empty = antd.Empty(
|
| 828 |
description="Enter your question to begin",
|
| 829 |
-
|
| 830 |
-
style
|
| 831 |
-
|
| 832 |
-
"background": "#f8f9fa",
|
| 833 |
-
"borderRadius": "10px",
|
| 834 |
-
"margin": "20px"
|
| 835 |
-
}
|
| 836 |
)
|
| 837 |
|
| 838 |
with antd.Tabs.Item(key="loading"):
|
|
@@ -850,8 +830,8 @@ def create_main_interface():
|
|
| 850 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
|
| 851 |
btn.click(
|
| 852 |
demo_instance.generation_code,
|
| 853 |
-
inputs=[input, setting
|
| 854 |
-
outputs=[code_output,
|
| 855 |
).then( # then์ ์ฌ์ฉํ์ฌ ์ฐ์ ๋์ ์ถ๊ฐ
|
| 856 |
fn=update_placeholder,
|
| 857 |
inputs=[],
|
|
@@ -870,9 +850,39 @@ def create_main_interface():
|
|
| 870 |
outputs=[deploy_result]
|
| 871 |
)
|
| 872 |
|
| 873 |
-
# ์ถ๊ฐ ์คํ์ผ ์ ์ฉ
|
| 874 |
gr.HTML("""
|
| 875 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 876 |
.app-content {
|
| 877 |
padding: 20px;
|
| 878 |
text-align: center;
|
|
|
|
| 256 |
def __init__(self):
|
| 257 |
pass
|
| 258 |
|
| 259 |
+
async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
|
| 260 |
if not query or query.strip() == '':
|
| 261 |
query = get_random_placeholder()
|
| 262 |
+
|
| 263 |
+
messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
|
| 264 |
+
messages.append({'role': Role.USER, 'content': query})
|
| 265 |
+
|
|
|
|
| 266 |
system_message = messages[0]['content']
|
| 267 |
+
|
| 268 |
claude_messages = [
|
| 269 |
+
{"role": "user", "content": query}
|
| 270 |
+
]
|
| 271 |
+
|
| 272 |
+
openai_messages = [
|
| 273 |
+
{"role": "system", "content": system_message},
|
| 274 |
+
{"role": "user", "content": query}
|
| 275 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
try:
|
| 278 |
# ๋ก๋ฉ ์์
|
| 279 |
yield [
|
| 280 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
|
|
|
| 281 |
None,
|
| 282 |
gr.update(active_key="loading"),
|
| 283 |
gr.update(open=True)
|
|
|
|
| 289 |
async for content in try_claude_api(system_message, claude_messages):
|
| 290 |
yield [
|
| 291 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
|
|
|
| 292 |
None,
|
| 293 |
gr.update(active_key="loading"),
|
| 294 |
gr.update(open=True)
|
|
|
|
| 302 |
async for content in try_openai_api(openai_messages):
|
| 303 |
yield [
|
| 304 |
"", # ๋น ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ (์ฝ๋ ์ถ๋ ฅํ์ง ์์)
|
|
|
|
| 305 |
None,
|
| 306 |
gr.update(active_key="loading"),
|
| 307 |
gr.update(open=True)
|
|
|
|
| 310 |
collected_content = content
|
| 311 |
|
| 312 |
if collected_content:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
# ์ต์ข
๊ฒฐ๊ณผ๋ง ํ์
|
| 314 |
yield [
|
| 315 |
collected_content,
|
|
|
|
| 316 |
send_to_sandbox(remove_code_block(collected_content)),
|
| 317 |
gr.update(active_key="render"),
|
| 318 |
gr.update(open=False)
|
|
|
|
| 325 |
print(f"Error details: {str(e)}")
|
| 326 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
| 327 |
|
|
|
|
|
|
|
|
|
|
| 328 |
def remove_code_block(text):
|
| 329 |
pattern = r'```html\n(.+?)\n```'
|
| 330 |
match = re.search(pattern, text, re.DOTALL)
|
|
|
|
| 462 |
custom_css = f.read()
|
| 463 |
|
| 464 |
demo = gr.Blocks(css=custom_css + """
|
| 465 |
+
.empty-content {
|
| 466 |
+
padding: 40px !important;
|
| 467 |
+
background: #f8f9fa !important;
|
| 468 |
+
border-radius: 10px !important;
|
| 469 |
+
margin: 20px !important;
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
.container {
|
| 473 |
background: #f0f0f0;
|
| 474 |
min-height: 100vh;
|
|
|
|
| 578 |
gr.HTML("""
|
| 579 |
<div class="thinking-container">
|
| 580 |
<style>
|
| 581 |
+
.custom-textarea {
|
| 582 |
+
background: #f8f9fa !important;
|
| 583 |
+
border: 1px solid #e0e0e0 !important;
|
| 584 |
+
border-radius: 10px !important;
|
| 585 |
+
padding: 15px !important;
|
| 586 |
+
min-height: 150px !important;
|
| 587 |
+
font-family: -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
.custom-textarea:focus {
|
| 591 |
+
border-color: #007aff !important;
|
| 592 |
+
box-shadow: 0 0 0 2px rgba(0,122,255,0.2) !important;
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
.thinking-container {
|
| 596 |
text-align: center;
|
| 597 |
padding: 20px;
|
|
|
|
| 757 |
size="large",
|
| 758 |
allow_clear=True,
|
| 759 |
placeholder=get_random_placeholder(),
|
| 760 |
+
elem_classes="custom-textarea" # style ๋์ class ์ฌ์ฉ
|
| 761 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
)
|
| 763 |
|
| 764 |
# ๋ฒํผ ๊ทธ๋ฃน
|
|
|
|
| 767 |
"Generate",
|
| 768 |
type="primary",
|
| 769 |
size="large",
|
| 770 |
+
elem_classes="generate-btn"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
)
|
| 772 |
boost_btn = antd.Button(
|
| 773 |
"Enhance",
|
| 774 |
type="default",
|
| 775 |
size="large",
|
| 776 |
+
elem_classes="enhance-btn"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
)
|
| 778 |
deploy_btn = antd.Button(
|
| 779 |
"Share",
|
| 780 |
type="default",
|
| 781 |
size="large",
|
| 782 |
+
elem_classes="share-btn"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
)
|
| 784 |
|
| 785 |
+
|
| 786 |
deploy_result = gr.HTML(
|
| 787 |
label="Share Result",
|
| 788 |
elem_classes="deploy-result"
|
|
|
|
| 810 |
with antd.Tabs.Item(key="empty"):
|
| 811 |
empty = antd.Empty(
|
| 812 |
description="Enter your question to begin",
|
| 813 |
+
|
| 814 |
+
elem_classes="right_content empty-content" # style ๋์ class ์ฌ์ฉ
|
| 815 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
)
|
| 817 |
|
| 818 |
with antd.Tabs.Item(key="loading"):
|
|
|
|
| 830 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
|
| 831 |
btn.click(
|
| 832 |
demo_instance.generation_code,
|
| 833 |
+
inputs=[input, setting],
|
| 834 |
+
outputs=[code_output, sandbox, state_tab, code_drawer]
|
| 835 |
).then( # then์ ์ฌ์ฉํ์ฌ ์ฐ์ ๋์ ์ถ๊ฐ
|
| 836 |
fn=update_placeholder,
|
| 837 |
inputs=[],
|
|
|
|
| 850 |
outputs=[deploy_result]
|
| 851 |
)
|
| 852 |
|
|
|
|
| 853 |
gr.HTML("""
|
| 854 |
<style>
|
| 855 |
+
.generate-btn {
|
| 856 |
+
background: #007aff !important;
|
| 857 |
+
border-radius: 8px !important;
|
| 858 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
.enhance-btn {
|
| 862 |
+
border-radius: 8px !important;
|
| 863 |
+
border: 1px solid #007aff !important;
|
| 864 |
+
color: #007aff !important;
|
| 865 |
+
}
|
| 866 |
+
|
| 867 |
+
.share-btn {
|
| 868 |
+
border-radius: 8px !important;
|
| 869 |
+
border: 1px solid #28c840 !important;
|
| 870 |
+
color: #28c840 !important;
|
| 871 |
+
}
|
| 872 |
+
|
| 873 |
+
/* hover ํจ๊ณผ */
|
| 874 |
+
.generate-btn:hover {
|
| 875 |
+
background: #0056b3 !important;
|
| 876 |
+
}
|
| 877 |
+
|
| 878 |
+
.enhance-btn:hover {
|
| 879 |
+
background: rgba(0,122,255,0.1) !important;
|
| 880 |
+
}
|
| 881 |
+
|
| 882 |
+
.share-btn:hover {
|
| 883 |
+
background: rgba(40,200,64,0.1) !important;
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
.app-content {
|
| 887 |
padding: 20px;
|
| 888 |
text-align: center;
|