Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -526,30 +526,46 @@ def deploy_to_vercel(code: str):
|
|
526 |
def remove_code_block(text):
|
527 |
"""
|
528 |
More robust function to extract code from markdown code blocks
|
|
|
529 |
"""
|
530 |
-
#
|
531 |
pattern = r'```html\s*([\s\S]+?)\s*```'
|
532 |
match = re.search(pattern, text, re.DOTALL)
|
533 |
if match:
|
534 |
return match.group(1).strip()
|
535 |
|
536 |
-
#
|
537 |
pattern = r'```(?:\w+)?\s*([\s\S]+?)\s*```'
|
538 |
match = re.search(pattern, text, re.DOTALL)
|
539 |
if match:
|
540 |
return match.group(1).strip()
|
541 |
|
542 |
-
#
|
|
|
|
|
|
|
|
|
543 |
return text.strip()
|
544 |
|
545 |
def send_to_sandbox(code):
|
546 |
"""
|
547 |
Improved function to create iframe with proper code cleaning
|
|
|
548 |
"""
|
549 |
-
#
|
550 |
clean_code = remove_code_block(code)
|
551 |
|
552 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
553 |
if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
|
554 |
clean_code = f"""<!DOCTYPE html>
|
555 |
<html>
|
@@ -563,7 +579,7 @@ def send_to_sandbox(code):
|
|
563 |
</body>
|
564 |
</html>"""
|
565 |
|
566 |
-
#
|
567 |
encoded_html = base64.b64encode(clean_code.encode('utf-8')).decode('utf-8')
|
568 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
569 |
return f'<iframe src="{data_uri}" width="100%" height="920px" style="border:none;"></iframe>'
|
@@ -634,14 +650,21 @@ def history_render(history: History):
|
|
634 |
def execute_code(query: str):
|
635 |
"""
|
636 |
Improved function to execute code directly from input
|
|
|
637 |
"""
|
638 |
if not query or query.strip() == '':
|
639 |
return None, gr.update(active_key="empty")
|
640 |
try:
|
641 |
-
#
|
642 |
clean_code = remove_code_block(query)
|
643 |
|
644 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
645 |
if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
|
646 |
if not ('<body' in clean_code and '</body>' in clean_code):
|
647 |
clean_code = f"""<!DOCTYPE html>
|
@@ -741,7 +764,15 @@ class Demo:
|
|
741 |
}])
|
742 |
|
743 |
# 최종 결과(코드) + 샌드박스 미리보기
|
|
|
744 |
clean_code = remove_code_block(collected_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
745 |
yield [
|
746 |
collected_content,
|
747 |
_history,
|
|
|
526 |
def remove_code_block(text):
|
527 |
"""
|
528 |
More robust function to extract code from markdown code blocks
|
529 |
+
텍스트에서 ```html 및 ``` 태그를 완전히 제거하는 함수
|
530 |
"""
|
531 |
+
# ```html 태그로 둘러싸인 코드 블록 찾기
|
532 |
pattern = r'```html\s*([\s\S]+?)\s*```'
|
533 |
match = re.search(pattern, text, re.DOTALL)
|
534 |
if match:
|
535 |
return match.group(1).strip()
|
536 |
|
537 |
+
# 일반 코드 블록 처리
|
538 |
pattern = r'```(?:\w+)?\s*([\s\S]+?)\s*```'
|
539 |
match = re.search(pattern, text, re.DOTALL)
|
540 |
if match:
|
541 |
return match.group(1).strip()
|
542 |
|
543 |
+
# 텍스트에 ```html과 ```가 포함된 경우 명시적으로 제거
|
544 |
+
text = re.sub(r'```html\s*', '', text)
|
545 |
+
text = re.sub(r'\s*```', '', text)
|
546 |
+
|
547 |
+
# 코드 블록이 없는 경우 원본 텍스트 반환
|
548 |
return text.strip()
|
549 |
|
550 |
def send_to_sandbox(code):
|
551 |
"""
|
552 |
Improved function to create iframe with proper code cleaning
|
553 |
+
```html 태그가 확실히 제거된 코드를 iframe으로 렌더링
|
554 |
"""
|
555 |
+
# 코드에서 마크다운 표기 제거
|
556 |
clean_code = remove_code_block(code)
|
557 |
|
558 |
+
# 디버깅: 코드 앞부분 확인
|
559 |
+
code_start = clean_code[:50] if len(clean_code) > 50 else clean_code
|
560 |
+
print(f"Code start: {code_start}")
|
561 |
+
|
562 |
+
# ```html 태그가 여전히 있으면 명시적으로 제거
|
563 |
+
if clean_code.startswith('```html'):
|
564 |
+
clean_code = clean_code[7:].strip()
|
565 |
+
if clean_code.endswith('```'):
|
566 |
+
clean_code = clean_code[:-3].strip()
|
567 |
+
|
568 |
+
# 기본 HTML 구조 추가
|
569 |
if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
|
570 |
clean_code = f"""<!DOCTYPE html>
|
571 |
<html>
|
|
|
579 |
</body>
|
580 |
</html>"""
|
581 |
|
582 |
+
# iframe 생성
|
583 |
encoded_html = base64.b64encode(clean_code.encode('utf-8')).decode('utf-8')
|
584 |
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
|
585 |
return f'<iframe src="{data_uri}" width="100%" height="920px" style="border:none;"></iframe>'
|
|
|
650 |
def execute_code(query: str):
|
651 |
"""
|
652 |
Improved function to execute code directly from input
|
653 |
+
코드 실행 시 ```html 태그를 확실히 제거
|
654 |
"""
|
655 |
if not query or query.strip() == '':
|
656 |
return None, gr.update(active_key="empty")
|
657 |
try:
|
658 |
+
# 코드 정제 - 마크다운 태그 철저히 제거
|
659 |
clean_code = remove_code_block(query)
|
660 |
|
661 |
+
# ```html 태그가 여전히 있으면 명시적으로 제거
|
662 |
+
if clean_code.startswith('```html'):
|
663 |
+
clean_code = clean_code[7:].strip()
|
664 |
+
if clean_code.endswith('```'):
|
665 |
+
clean_code = clean_code[:-3].strip()
|
666 |
+
|
667 |
+
# HTML 구조 추가
|
668 |
if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
|
669 |
if not ('<body' in clean_code and '</body>' in clean_code):
|
670 |
clean_code = f"""<!DOCTYPE html>
|
|
|
764 |
}])
|
765 |
|
766 |
# 최종 결과(코드) + 샌드박스 미리보기
|
767 |
+
# 코드 블록 추출 시 확실하게 ```html 제거
|
768 |
clean_code = remove_code_block(collected_content)
|
769 |
+
|
770 |
+
# 디버그 출력
|
771 |
+
print(f"Original content start: {collected_content[:30]}")
|
772 |
+
print(f"Cleaned code start: {clean_code[:30]}")
|
773 |
+
|
774 |
+
# 마크다운 형식의 collected_content는 그대로 출력
|
775 |
+
# 하지만 샌드박스에는 정제된 clean_code 전달
|
776 |
yield [
|
777 |
collected_content,
|
778 |
_history,
|