Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -814,87 +814,91 @@ def execute_code(query: str):
|
|
814 |
# 6) 데모 클래스
|
815 |
# ------------------------
|
816 |
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
|
|
|
|
|
|
|
|
826 |
다음 게임을 제작해주세요.
|
827 |
중요 요구사항:
|
828 |
1. 코드는 가능한 한 간결하게 작성할 것
|
829 |
2. 불필요한 주석이나 설명은 제외할 것
|
830 |
-
3. 코드는
|
831 |
4. 모든 코드는 하나의 HTML 파일에 통합할 것
|
832 |
5. 핵심 기능만 구현하고 부가 기능은 생략할 것
|
833 |
|
834 |
게임 요청: {query}
|
835 |
"""
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
]
|
845 |
-
|
846 |
-
openai_messages = [{"role": "system", "content": system_message}]
|
847 |
-
for msg in messages[1:]:
|
848 |
-
openai_messages.append({
|
849 |
-
"role": msg["role"],
|
850 |
-
"content": msg["content"]
|
851 |
-
})
|
852 |
-
openai_messages.append({"role": "user", "content": query})
|
853 |
-
|
854 |
-
try:
|
855 |
-
# "Generating code..." 출력
|
856 |
-
yield [
|
857 |
-
"Generating code...",
|
858 |
-
_history,
|
859 |
-
None,
|
860 |
-
gr.update(active_key="loading"),
|
861 |
-
gr.update(open=True)
|
862 |
]
|
863 |
-
|
864 |
-
|
865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
866 |
try:
|
867 |
-
#
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
collected_content =
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
898 |
⚠️ **경고: 생성된 코드가 너무 깁니다 ({code_lines}줄)**
|
899 |
|
900 |
이로 인해 실행 시 오류가 발생할 수 있습니다. 다음과 같이 시도해 보세요:
|
@@ -906,6 +910,7 @@ async def generation_code(self, query: Optional[str], _setting: Dict[str, str],
|
|
906 |
{clean_code[:2000]}
|
907 |
... (코드가 너무 깁니다) ...
|
908 |
"""
|
|
|
909 |
collected_content = warning_msg
|
910 |
# 히스토리에는 추가하지 않음
|
911 |
yield [
|
@@ -923,18 +928,23 @@ _history = messages_to_history([
|
|
923 |
'role': Role.ASSISTANT,
|
924 |
'content': collected_content
|
925 |
}])
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
|
|
|
|
|
|
|
|
|
|
938 |
|
939 |
|
940 |
# ------------------------
|
|
|
814 |
# 6) 데모 클래스
|
815 |
# ------------------------
|
816 |
|
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']
|
824 |
+
|
825 |
+
if _history is None:
|
826 |
+
_history = []
|
827 |
+
|
828 |
+
# 프롬프트 제한 및 강화
|
829 |
+
query = f"""
|
830 |
다음 게임을 제작해주세요.
|
831 |
중요 요구사항:
|
832 |
1. 코드는 가능한 한 간결하게 작성할 것
|
833 |
2. 불필요한 주석이나 설명은 제외할 것
|
834 |
+
3. 코드는 600줄을 넘지 않을 것
|
835 |
4. 모든 코드는 하나의 HTML 파일에 통합할 것
|
836 |
5. 핵심 기능만 구현하고 부가 기능은 생략할 것
|
837 |
|
838 |
게임 요청: {query}
|
839 |
"""
|
840 |
+
|
841 |
+
messages = history_to_messages(_history, _setting['system'])
|
842 |
+
system_message = messages[0]['content']
|
843 |
+
|
844 |
+
claude_messages = [
|
845 |
+
{"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
|
846 |
+
for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
|
847 |
+
if msg["content"].strip() != ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
848 |
]
|
849 |
+
|
850 |
+
openai_messages = [{"role": "system", "content": system_message}]
|
851 |
+
for msg in messages[1:]:
|
852 |
+
openai_messages.append({
|
853 |
+
"role": msg["role"],
|
854 |
+
"content": msg["content"]
|
855 |
+
})
|
856 |
+
openai_messages.append({"role": "user", "content": query})
|
857 |
+
|
858 |
try:
|
859 |
+
# "Generating code..." 출력
|
860 |
+
yield [
|
861 |
+
"Generating code...",
|
862 |
+
_history,
|
863 |
+
None,
|
864 |
+
gr.update(active_key="loading"),
|
865 |
+
gr.update(open=True)
|
866 |
+
]
|
867 |
+
await asyncio.sleep(0)
|
868 |
+
|
869 |
+
collected_content = None
|
870 |
+
try:
|
871 |
+
# Claude API 시도
|
872 |
+
async for content in try_claude_api(system_message, claude_messages):
|
873 |
+
yield [
|
874 |
+
content,
|
875 |
+
_history,
|
876 |
+
None,
|
877 |
+
gr.update(active_key="loading"),
|
878 |
+
gr.update(open=True)
|
879 |
+
]
|
880 |
+
await asyncio.sleep(0)
|
881 |
+
collected_content = content
|
882 |
+
except Exception:
|
883 |
+
# OpenAI fallback
|
884 |
+
async for content in try_openai_api(openai_messages):
|
885 |
+
yield [
|
886 |
+
content,
|
887 |
+
_history,
|
888 |
+
None,
|
889 |
+
gr.update(active_key="loading"),
|
890 |
+
gr.update(open=True)
|
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 |
이로 인해 실행 시 오류가 발생할 수 있습니다. 다음과 같이 시도해 보세요:
|
|
|
910 |
{clean_code[:2000]}
|
911 |
... (코드가 너무 깁니다) ...
|
912 |
"""
|
913 |
+
|
914 |
collected_content = warning_msg
|
915 |
# 히스토리에는 추가하지 않음
|
916 |
yield [
|
|
|
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 |
# ------------------------
|