openfree commited on
Commit
89d6b7d
·
verified ·
1 Parent(s): 3fc84bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -21
app.py CHANGED
@@ -525,22 +525,48 @@ def deploy_to_vercel(code: str):
525
 
526
  def remove_code_block(text):
527
  """
528
- 메시지 내의 ```html ... ``` 부분만 추출하여 반환
529
  """
530
- pattern = r'```html\n(.+?)\n```'
 
531
  match = re.search(pattern, text, re.DOTALL)
532
  if match:
533
  return match.group(1).strip()
534
- else:
535
- return text.strip()
 
 
 
 
 
 
 
536
 
537
  def send_to_sandbox(code):
538
  """
539
- HTML 코드를 iframe으로 렌더링하기 위한 data URI 생성
540
  """
541
- encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
543
- return f"<iframe src=\"{data_uri}\" width=\"100%\" height=\"920px\" style=\"border:none;\"></iframe>"
544
 
545
  def boost_prompt(prompt: str) -> str:
546
  """
@@ -605,6 +631,36 @@ def history_render(history: History):
605
  """
606
  return gr.update(open=True), history
607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
608
 
609
  # ------------------------
610
  # 6) 데모 클래스
@@ -685,10 +741,11 @@ class Demo:
685
  }])
686
 
687
  # 최종 결과(코드) + 샌드박스 미리보기
 
688
  yield [
689
  collected_content,
690
  _history,
691
- send_to_sandbox(remove_code_block(collected_content)),
692
  gr.update(active_key="render"),
693
  gr.update(open=True)
694
  ]
@@ -912,7 +969,9 @@ with gr.Blocks(css_paths=["app.css"], theme=theme) as demo:
912
  input_text = antd.InputTextarea(
913
  size="large",
914
  allow_clear=True,
915
- placeholder=random.choice(DEMO_LIST)['description']
 
 
916
  )
917
  gr.HTML('<div class="help-text">💡 원하는 게임의 설명을 입력하세요. 예: "Create a platformer game with jumping mechanics"</div>')
918
 
@@ -991,18 +1050,6 @@ with gr.Blocks(css_paths=["app.css"], theme=theme) as demo:
991
  )
992
 
993
  # (G) 'Code실행' 버튼 => 미리보기 iframe 로드
994
- def execute_code(query: str):
995
- if not query or query.strip() == '':
996
- return None, gr.update(active_key="empty")
997
- try:
998
- if '```html' in query and '```' in query:
999
- code = remove_code_block(query)
1000
- else:
1001
- code = query.strip()
1002
- return send_to_sandbox(code), gr.update(active_key="render")
1003
- except Exception:
1004
- return None, gr.update(active_key="empty")
1005
-
1006
  execute_btn.click(
1007
  fn=execute_code,
1008
  inputs=[input_text],
 
525
 
526
  def remove_code_block(text):
527
  """
528
+ More robust function to extract code from markdown code blocks
529
  """
530
+ # First try to find ```html blocks
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
+ # If no html block found, try any code block
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
+ # If no code block, return the original text (might be raw code)
543
+ return text.strip()
544
 
545
  def send_to_sandbox(code):
546
  """
547
+ Improved function to create iframe with proper code cleaning
548
  """
549
+ # Make sure we have clean code without markdown delimiters
550
+ clean_code = remove_code_block(code)
551
+
552
+ # Ensure the code has basic HTML structure if it's missing
553
+ if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
554
+ clean_code = f"""<!DOCTYPE html>
555
+ <html>
556
+ <head>
557
+ <meta charset="UTF-8">
558
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
559
+ <title>Game Preview</title>
560
+ </head>
561
+ <body>
562
+ {clean_code}
563
+ </body>
564
+ </html>"""
565
+
566
+ # Create data URI for iframe
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>'
570
 
571
  def boost_prompt(prompt: str) -> str:
572
  """
 
631
  """
632
  return gr.update(open=True), history
633
 
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
+ # Handle markdown code blocks or raw code
642
+ clean_code = remove_code_block(query)
643
+
644
+ # Add HTML structure if it's just a fragment
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>
648
+ <html>
649
+ <head>
650
+ <meta charset="UTF-8">
651
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
652
+ <title>Game Preview</title>
653
+ </head>
654
+ <body>
655
+ {clean_code}
656
+ </body>
657
+ </html>"""
658
+
659
+ return send_to_sandbox(clean_code), gr.update(active_key="render")
660
+ except Exception as e:
661
+ print(f"Execute code error: {str(e)}")
662
+ return None, gr.update(active_key="empty")
663
+
664
 
665
  # ------------------------
666
  # 6) 데모 클래스
 
741
  }])
742
 
743
  # 최종 결과(코드) + 샌드박스 미리보기
744
+ clean_code = remove_code_block(collected_content)
745
  yield [
746
  collected_content,
747
  _history,
748
+ send_to_sandbox(clean_code),
749
  gr.update(active_key="render"),
750
  gr.update(open=True)
751
  ]
 
969
  input_text = antd.InputTextarea(
970
  size="large",
971
  allow_clear=True,
972
+ placeholder=random.choice(DEMO_LIST)['description'],
973
+ rows=10, # 입력창 높이 증가
974
+ max_length=100000 # 입력 최대 길이 증가
975
  )
976
  gr.HTML('<div class="help-text">💡 원하는 게임의 설명을 입력하세요. 예: "Create a platformer game with jumping mechanics"</div>')
977
 
 
1050
  )
1051
 
1052
  # (G) 'Code실행' 버튼 => 미리보기 iframe 로드
 
 
 
 
 
 
 
 
 
 
 
 
1053
  execute_btn.click(
1054
  fn=execute_code,
1055
  inputs=[input_text],