openfree commited on
Commit
a6911e3
·
verified ·
1 Parent(s): d3acbda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -44
app.py CHANGED
@@ -817,7 +817,7 @@ def execute_code(query: str):
817
  class Demo:
818
  def __init__(self):
819
  pass
820
-
821
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
822
  if not query or query.strip() == '':
823
  query = random.choice(DEMO_LIST)['description']
@@ -834,7 +834,6 @@ class Demo:
834
  3. 코드는 600줄을 넘지 않을 것
835
  4. 모든 코드는 하나의 HTML 파일에 통합할 것
836
  5. 핵심 기능만 구현하고 부가 기능은 생략할 것
837
-
838
  게임 요청: {query}
839
  """
840
 
@@ -865,7 +864,6 @@ class Demo:
865
  gr.update(open=True)
866
  ]
867
  await asyncio.sleep(0)
868
-
869
  collected_content = None
870
  try:
871
  # Claude API 시도
@@ -891,61 +889,54 @@ class Demo:
891
  ]
892
  await asyncio.sleep(0)
893
  collected_content = content
894
-
895
  if collected_content:
896
  # 코드 크기 확인하고 필요하면 경고 추가
897
  clean_code = remove_code_block(collected_content)
898
  code_lines = clean_code.count('\n') + 1
899
 
900
- if code_lines > 700: # 여유 있게 250줄로 체크
901
  warning_msg = f"""
902
  ⚠️ **경고: 생성된 코드가 너무 깁니다 ({code_lines}줄)**
903
-
904
  이로 인해 실행 시 오류가 발생할 수 있습니다. 다음과 같이 시도해 보세요:
905
  1. 더 간단한 게임을 요청하세요
906
  2. 특정 기능만 명시하여 요청하세요 (예: "간단한 Snake 게임, 점수 시스템 없이")
907
  3. "코드" 버튼을 사용하여 직접 실행해 보세요
908
-
909
  ```html
910
  {clean_code[:2000]}
911
- ... (코드가 너무 깁니다) ...
912
- """
913
 
914
- collected_content = warning_msg
915
- # 히스토리에는 추가하지 않음
916
- yield [
917
- collected_content,
918
- _history,
919
- None,
920
- gr.update(active_key="empty"),
921
- gr.update(open=True)
922
- ]
923
- else:
924
- # 히스토리 갱신
925
- _history = messages_to_history([
926
- {'role': Role.SYSTEM, 'content': system_message}
927
- ] + claude_messages + [{
928
- 'role': Role.ASSISTANT,
929
- 'content': collected_content
930
- }])
931
-
932
- # 최종 결과(코드) + 샌드박스 미리보기
933
- yield [
934
- collected_content,
935
- _history,
936
- send_to_sandbox(clean_code),
937
- gr.update(active_key="render"),
938
- gr.update(open=True)
939
- ]
940
- else:
941
- raise ValueError("No content was generated from either API")
942
- except Exception as e:
943
- raise ValueError(f'Error calling APIs: {str(e)}')
944
-
945
- def clear_history(self):
946
- return []
947
-
948
-
949
 
950
  # ------------------------
951
  # 7) Gradio / Modelscope UI 빌드
 
817
  class Demo:
818
  def __init__(self):
819
  pass
820
+
821
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
822
  if not query or query.strip() == '':
823
  query = random.choice(DEMO_LIST)['description']
 
834
  3. 코드는 600줄을 넘지 않을 것
835
  4. 모든 코드는 하나의 HTML 파일에 통합할 것
836
  5. 핵심 기능만 구현하고 부가 기능은 생략할 것
 
837
  게임 요청: {query}
838
  """
839
 
 
864
  gr.update(open=True)
865
  ]
866
  await asyncio.sleep(0)
 
867
  collected_content = None
868
  try:
869
  # Claude API 시도
 
889
  ]
890
  await asyncio.sleep(0)
891
  collected_content = content
 
892
  if collected_content:
893
  # 코드 크기 확인하고 필요하면 경고 추가
894
  clean_code = remove_code_block(collected_content)
895
  code_lines = clean_code.count('\n') + 1
896
 
897
+ if code_lines > 700: # 여유 있게 700줄로 체크
898
  warning_msg = f"""
899
  ⚠️ **경고: 생성된 코드가 너무 깁니다 ({code_lines}줄)**
 
900
  이로 인해 실행 시 오류가 발생할 수 있습니다. 다음과 같이 시도해 보세요:
901
  1. 더 간단한 게임을 요청하세요
902
  2. 특정 기능만 명시하여 요청하세요 (예: "간단한 Snake 게임, 점수 시스템 없이")
903
  3. "코드" 버튼을 사용하여 직접 실행해 보세요
 
904
  ```html
905
  {clean_code[:2000]}
906
+ ... (코드가 너무 깁니다) ... """
 
907
 
908
+ collected_content = warning_msg
909
+ # 히스토리에는 추가하지 않음
910
+ yield [
911
+ collected_content,
912
+ _history,
913
+ None,
914
+ gr.update(active_key="empty"),
915
+ gr.update(open=True)
916
+ ]
917
+ else:
918
+ # 히스토리 갱신
919
+ _history = messages_to_history([
920
+ {'role': Role.SYSTEM, 'content': system_message}
921
+ ] + claude_messages + [{
922
+ 'role': Role.ASSISTANT,
923
+ 'content': collected_content
924
+ }])
925
+ # 최종 결과(코드) + 샌드박스 미리보기
926
+ yield [
927
+ collected_content,
928
+ _history,
929
+ send_to_sandbox(clean_code),
930
+ gr.update(active_key="render"),
931
+ gr.update(open=True)
932
+ ]
933
+ else:
934
+ raise ValueError("No content was generated from either API")
935
+ except Exception as e:
936
+ raise ValueError(f'Error calling APIs: {str(e)}')
937
+
938
+ def clear_history(self):
939
+ return []
 
 
 
940
 
941
  # ------------------------
942
  # 7) Gradio / Modelscope UI 빌드