Update app.py
Browse files
app.py
CHANGED
@@ -145,7 +145,7 @@ def get_package_name(import_name):
|
|
145 |
return None
|
146 |
return IMPORT_TO_PACKAGE.get(base_import, base_import)
|
147 |
|
148 |
-
def analyze_code(code: str) -> str:
|
149 |
"""์ฝ๋ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ HTML ํ์์ผ๋ก ๋ฐํ"""
|
150 |
analysis = []
|
151 |
|
@@ -239,6 +239,38 @@ def analyze_code(code: str) -> str:
|
|
239 |
analysis.append(f"<li><strong>{component}</strong></li>")
|
240 |
analysis.append("</ul>")
|
241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
return "\n".join(analysis)
|
243 |
|
244 |
async def try_claude_api(system_message, claude_messages, timeout=15):
|
@@ -450,12 +482,15 @@ def deploy_to_huggingface(code: str, token: str):
|
|
450 |
|
451 |
class Demo:
|
452 |
def __init__(self):
|
453 |
-
|
454 |
-
|
455 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
456 |
if not query or query.strip() == '':
|
457 |
query = random.choice(DEMO_LIST)['description']
|
458 |
|
|
|
|
|
|
|
459 |
if _history is None:
|
460 |
_history = []
|
461 |
|
@@ -480,7 +515,8 @@ class Demo:
|
|
480 |
collected_content = None
|
481 |
try:
|
482 |
async for content in try_claude_api(system_message, claude_messages):
|
483 |
-
|
|
|
484 |
yield code
|
485 |
collected_content = code
|
486 |
|
@@ -488,7 +524,8 @@ class Demo:
|
|
488 |
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
489 |
|
490 |
async for content in try_openai_api(openai_messages):
|
491 |
-
|
|
|
492 |
yield code
|
493 |
collected_content = code
|
494 |
|
@@ -500,13 +537,17 @@ class Demo:
|
|
500 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
501 |
|
502 |
def clear_history(self):
|
|
|
503 |
return []
|
|
|
|
|
|
|
504 |
|
505 |
# ์์ ํ๋กฌํํธ
|
506 |
example_prompts = [
|
507 |
-
"
|
508 |
"BMI ๊ณ์ฐ๊ธฐ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. ํค์ ๋ชธ๋ฌด๊ฒ๋ฅผ ์
๋ ฅํ๋ฉด BMI ์ง์์ ๋น๋ง๋๋ฅผ ๊ณ์ฐํด์ฃผ๋ ์ฑ์
๋๋ค.",
|
509 |
-
"
|
510 |
"๋จ์ ๋ณํ๊ธฐ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. ๊ธธ์ด(m, cm, km ๋ฑ), ๋ฌด๊ฒ(kg, g ๋ฑ), ์จ๋(์ญ์จ, ํ์จ) ๋ฑ์ ๋ณํํ ์ ์๋ ์ฑ์
๋๋ค.",
|
511 |
"ํฌ๋ชจ๋๋ก ํ์ด๋จธ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. 25๋ถ ์ง์ค, 5๋ถ ํด์์ ๋ฐ๋ณตํ๋ ํ์ด๋จธ๋ก, ์ฌ์ดํด ํ์๋ ํ์๋ฉ๋๋ค."
|
512 |
]
|
@@ -575,6 +616,7 @@ with gr.Blocks(css=css) as demo:
|
|
575 |
"system": SystemPrompt,
|
576 |
})
|
577 |
is_generating = gr.State(False)
|
|
|
578 |
|
579 |
gr.HTML("""
|
580 |
<div class="header">
|
@@ -617,7 +659,7 @@ with gr.Blocks(css=css) as demo:
|
|
617 |
with gr.TabItem("๋ถ์"):
|
618 |
code_analysis = gr.HTML(label="์ฝ๋ ๋ถ์")
|
619 |
|
620 |
-
#
|
621 |
with gr.Group(elem_classes="deploy-section"):
|
622 |
gr.HTML("<h3>๋ฐฐํฌ ์ค์ </h3>")
|
623 |
hf_token = gr.Textbox(
|
@@ -636,14 +678,14 @@ with gr.Blocks(css=css) as demo:
|
|
636 |
""")
|
637 |
|
638 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
639 |
-
def start_generating():
|
640 |
-
return "๐ ์ฝ๋ ์์ฑ ์ค...", gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), True
|
641 |
-
|
642 |
def end_generating():
|
643 |
return "", gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), False
|
644 |
|
645 |
-
def update_code_analysis(code):
|
646 |
-
analysis = analyze_code(code)
|
647 |
return analysis
|
648 |
|
649 |
def handle_boost(prompt):
|
@@ -652,14 +694,15 @@ with gr.Blocks(css=css) as demo:
|
|
652 |
|
653 |
generate_btn.click(
|
654 |
fn=start_generating,
|
655 |
-
|
|
|
656 |
).then(
|
657 |
fn=demo_instance.generation_code,
|
658 |
inputs=[input_text, setting, history],
|
659 |
outputs=code_output
|
660 |
).then(
|
661 |
fn=update_code_analysis,
|
662 |
-
inputs=[code_output],
|
663 |
outputs=[code_analysis]
|
664 |
).then(
|
665 |
fn=end_generating,
|
|
|
145 |
return None
|
146 |
return IMPORT_TO_PACKAGE.get(base_import, base_import)
|
147 |
|
148 |
+
def analyze_code(code: str, query: str = "") -> str:
|
149 |
"""์ฝ๋ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ HTML ํ์์ผ๋ก ๋ฐํ"""
|
150 |
analysis = []
|
151 |
|
|
|
239 |
analysis.append(f"<li><strong>{component}</strong></li>")
|
240 |
analysis.append("</ul>")
|
241 |
|
242 |
+
# 4. ํ๋กฌํํธ ๋์ ๋ถ์
|
243 |
+
if query:
|
244 |
+
analysis.append("<h2>๐ง ํ๋กฌํํธ ๋์ ๋ถ์</h2>")
|
245 |
+
analysis.append(f"<p>์์ฒญํ์ <strong>\"{query}\"</strong>์ ๋ํ ๋์์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:</p>")
|
246 |
+
|
247 |
+
# ์ฃผ์ ๊ธฐ๋ฅ ๋ถ์
|
248 |
+
features = []
|
249 |
+
if "๊ณ์ฐ๊ธฐ" in query or "๊ณ์ฐ" in query:
|
250 |
+
if "BMI" in code:
|
251 |
+
features.append("BMI ๊ณ์ฐ ๊ธฐ๋ฅ")
|
252 |
+
if "๋จ์ ๋ณํ" in query and ("convert" in code or "๋ณํ" in code):
|
253 |
+
features.append("๋จ์ ๋ณํ ๊ธฐ๋ฅ")
|
254 |
+
if "๋ฌ๋ ฅ" in query and "calendar" in code:
|
255 |
+
features.append("๋ฌ๋ ฅ ํ์ ๊ธฐ๋ฅ")
|
256 |
+
if "๋ฉ๋ชจ" in query and ("์ ์ฅ" in code or "save" in code):
|
257 |
+
features.append("๋ฉ๋ชจ ์ ์ฅ ๋ฐ ๊ด๋ฆฌ ๊ธฐ๋ฅ")
|
258 |
+
if "ํ์ด๋จธ" in query and ("timer" in code or "ํ์ด๋จธ" in code):
|
259 |
+
features.append("ํ์ด๋จธ ๊ธฐ๋ฅ")
|
260 |
+
|
261 |
+
# ์ผ๋ฐ์ ์ธ ๋ถ์ ์ถ๊ฐ
|
262 |
+
features.append("์ง๊ด์ ์ธ ์ฌ์ฉ์ ์ธํฐํ์ด์ค")
|
263 |
+
features.append("์ฌ์ฉ์ ์
๋ ฅ ๊ฒ์ฆ ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ")
|
264 |
+
features.append("๋ชจ๋ํ ๋์์ธ๊ณผ ๋ ์ด์์")
|
265 |
+
|
266 |
+
if features:
|
267 |
+
analysis.append("<ul>")
|
268 |
+
for feature in features:
|
269 |
+
analysis.append(f"<li>{feature}</li>")
|
270 |
+
analysis.append("</ul>")
|
271 |
+
|
272 |
+
analysis.append("<p>์ด ์ฝ๋๋ Gradio๋ฅผ ํ์ฉํ์ฌ ์์ฒญํ์ ๊ธฐ๋ฅ์ ๊ตฌํํ์ผ๋ฉฐ, ์ฌ์ฉ์ ํธ์์ฑ๊ณผ ์ง๊ด์ ์ธ UI๋ฅผ ๊ฐ์ถ๊ณ ์์ต๋๋ค.</p>")
|
273 |
+
|
274 |
return "\n".join(analysis)
|
275 |
|
276 |
async def try_claude_api(system_message, claude_messages, timeout=15):
|
|
|
482 |
|
483 |
class Demo:
|
484 |
def __init__(self):
|
485 |
+
self.current_query = ""
|
486 |
+
|
487 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
|
488 |
if not query or query.strip() == '':
|
489 |
query = random.choice(DEMO_LIST)['description']
|
490 |
|
491 |
+
# ํ์ฌ ์ฟผ๋ฆฌ ์ ์ฅ
|
492 |
+
self.current_query = query
|
493 |
+
|
494 |
if _history is None:
|
495 |
_history = []
|
496 |
|
|
|
515 |
collected_content = None
|
516 |
try:
|
517 |
async for content in try_claude_api(system_message, claude_messages):
|
518 |
+
# ์ฝ๋ ๋ธ๋ก ํ์ ์ ๊ฑฐ
|
519 |
+
code = remove_code_block(content)
|
520 |
yield code
|
521 |
collected_content = code
|
522 |
|
|
|
524 |
print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
|
525 |
|
526 |
async for content in try_openai_api(openai_messages):
|
527 |
+
# ์ฝ๋ ๋ธ๋ก ํ์ ์ ๊ฑฐ
|
528 |
+
code = remove_code_block(content)
|
529 |
yield code
|
530 |
collected_content = code
|
531 |
|
|
|
537 |
raise ValueError(f'Error calling APIs: {str(e)}')
|
538 |
|
539 |
def clear_history(self):
|
540 |
+
self.current_query = ""
|
541 |
return []
|
542 |
+
|
543 |
+
def get_current_query(self):
|
544 |
+
return self.current_query
|
545 |
|
546 |
# ์์ ํ๋กฌํํธ
|
547 |
example_prompts = [
|
548 |
+
"ํ๊ธ ์
๋ ฅ์ ์์ฑ ์์ฑ TTS๋ฅผ ๊ตฌ๊ธ gtts๊ธฐ๋ฐ์ผ๋ก ์์ฑํ๋ผ.",
|
549 |
"BMI ๊ณ์ฐ๊ธฐ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. ํค์ ๋ชธ๋ฌด๊ฒ๋ฅผ ์
๋ ฅํ๋ฉด BMI ์ง์์ ๋น๋ง๋๋ฅผ ๊ณ์ฐํด์ฃผ๋ ์ฑ์
๋๋ค.",
|
550 |
+
"MBTI ์ง๋จ ์๋น์ค: 10๊ฐ์ง ์ง๋ฌธ๊ณผ ๋ต๋ณ ์ ํํ๋ฉด 16๊ฐ์ง ์ ํ ์ง๋จ๊ณผ ์์ธ ์ค๋ช
์ ํ๋ผ",
|
551 |
"๋จ์ ๋ณํ๊ธฐ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. ๊ธธ์ด(m, cm, km ๋ฑ), ๋ฌด๊ฒ(kg, g ๋ฑ), ์จ๋(์ญ์จ, ํ์จ) ๋ฑ์ ๋ณํํ ์ ์๋ ์ฑ์
๋๋ค.",
|
552 |
"ํฌ๋ชจ๋๋ก ํ์ด๋จธ๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์. 25๋ถ ์ง์ค, 5๋ถ ํด์์ ๋ฐ๋ณตํ๋ ํ์ด๋จธ๋ก, ์ฌ์ดํด ํ์๋ ํ์๋ฉ๋๋ค."
|
553 |
]
|
|
|
616 |
"system": SystemPrompt,
|
617 |
})
|
618 |
is_generating = gr.State(False)
|
619 |
+
current_query = gr.State("")
|
620 |
|
621 |
gr.HTML("""
|
622 |
<div class="header">
|
|
|
659 |
with gr.TabItem("๋ถ์"):
|
660 |
code_analysis = gr.HTML(label="์ฝ๋ ๋ถ์")
|
661 |
|
662 |
+
# Group์ผ๋ก ๋ณ๊ฒฝ
|
663 |
with gr.Group(elem_classes="deploy-section"):
|
664 |
gr.HTML("<h3>๋ฐฐํฌ ์ค์ </h3>")
|
665 |
hf_token = gr.Textbox(
|
|
|
678 |
""")
|
679 |
|
680 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
681 |
+
def start_generating(query):
|
682 |
+
return "๐ ์ฝ๋ ์์ฑ ์ค...", gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), True, query
|
683 |
+
|
684 |
def end_generating():
|
685 |
return "", gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), False
|
686 |
|
687 |
+
def update_code_analysis(code, query):
|
688 |
+
analysis = analyze_code(code, query)
|
689 |
return analysis
|
690 |
|
691 |
def handle_boost(prompt):
|
|
|
694 |
|
695 |
generate_btn.click(
|
696 |
fn=start_generating,
|
697 |
+
inputs=[input_text],
|
698 |
+
outputs=[status_html, generate_btn, boost_btn, clear_btn, is_generating, current_query]
|
699 |
).then(
|
700 |
fn=demo_instance.generation_code,
|
701 |
inputs=[input_text, setting, history],
|
702 |
outputs=code_output
|
703 |
).then(
|
704 |
fn=update_code_analysis,
|
705 |
+
inputs=[code_output, current_query],
|
706 |
outputs=[code_analysis]
|
707 |
).then(
|
708 |
fn=end_generating,
|