Update app.py
Browse files
app.py
CHANGED
@@ -1015,13 +1015,13 @@ def deploy_to_huggingface(code: str):
|
|
1015 |
token = os.getenv("HF_TOKEN")
|
1016 |
if not token:
|
1017 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
1018 |
-
|
1019 |
# 2) Space ์์ฑ ์ค๋น
|
1020 |
api = HfApi(token=token)
|
1021 |
space_name = generate_space_name()
|
1022 |
username = api.whoami()['name']
|
1023 |
repo_id = f"{username}/{space_name}"
|
1024 |
-
|
1025 |
# 3) Space ์์ฑ (private๋ก ์ค์ )
|
1026 |
try:
|
1027 |
create_repo(
|
@@ -1033,16 +1033,16 @@ def deploy_to_huggingface(code: str):
|
|
1033 |
)
|
1034 |
except Exception as e:
|
1035 |
raise e
|
1036 |
-
|
1037 |
# 4) ์ฝ๋ ์ ๋ฆฌ
|
1038 |
code = code.replace("```python", "").replace("```", "").strip()
|
1039 |
-
|
1040 |
# 5) ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ์์ฑ
|
1041 |
if "demo.launch()" not in code:
|
1042 |
full_app_code = code + "\n\nif __name__ == '__main__':\n demo.launch()"
|
1043 |
else:
|
1044 |
full_app_code = code
|
1045 |
-
|
1046 |
# 6) ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋
|
1047 |
with open("app.py", "w", encoding="utf-8") as f:
|
1048 |
f.write(full_app_code)
|
@@ -1053,24 +1053,57 @@ def deploy_to_huggingface(code: str):
|
|
1053 |
repo_id=repo_id,
|
1054 |
repo_type="space"
|
1055 |
)
|
1056 |
-
|
1057 |
# 7) requirements.txt ์์ฑ ๋ฐ ์
๋ก๋
|
1058 |
analysis_result = analyze_code(code)
|
1059 |
requirements = ""
|
1060 |
-
|
1061 |
# HTML์์ requirements.txt ์น์
์ฐพ๊ธฐ
|
1062 |
if "<h3>๐ Requirements.txt</h3>" in analysis_result:
|
1063 |
start_idx = analysis_result.find("<pre>") + 5
|
1064 |
end_idx = analysis_result.find("</pre>")
|
1065 |
if start_idx > 4 and end_idx > 0:
|
1066 |
requirements = analysis_result[start_idx:end_idx].strip()
|
1067 |
-
|
1068 |
-
# requirements
|
1069 |
-
|
1070 |
-
|
1071 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1072 |
with open("requirements.txt", "w") as f:
|
1073 |
-
f.write(
|
1074 |
|
1075 |
api.upload_file(
|
1076 |
path_or_fileobj="requirements.txt",
|
@@ -1078,15 +1111,12 @@ def deploy_to_huggingface(code: str):
|
|
1078 |
repo_id=repo_id,
|
1079 |
repo_type="space"
|
1080 |
)
|
1081 |
-
|
1082 |
# 8) ๊ฒฐ๊ณผ ๋ฐํ
|
1083 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
1084 |
return f'๋ฐฐํฌ ์๋ฃ! Private Space๋ก ์์ฑ๋์์ต๋๋ค. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ Space ์ด๊ธฐ</a>'
|
1085 |
-
|
1086 |
except Exception as e:
|
1087 |
return f"๋ฐฐํฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
1088 |
-
|
1089 |
-
|
1090 |
|
1091 |
# Demo ์ธ์คํด์ค ์์ฑ
|
1092 |
demo_instance = Demo()
|
|
|
1015 |
token = os.getenv("HF_TOKEN")
|
1016 |
if not token:
|
1017 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
1018 |
+
|
1019 |
# 2) Space ์์ฑ ์ค๋น
|
1020 |
api = HfApi(token=token)
|
1021 |
space_name = generate_space_name()
|
1022 |
username = api.whoami()['name']
|
1023 |
repo_id = f"{username}/{space_name}"
|
1024 |
+
|
1025 |
# 3) Space ์์ฑ (private๋ก ์ค์ )
|
1026 |
try:
|
1027 |
create_repo(
|
|
|
1033 |
)
|
1034 |
except Exception as e:
|
1035 |
raise e
|
1036 |
+
|
1037 |
# 4) ์ฝ๋ ์ ๋ฆฌ
|
1038 |
code = code.replace("```python", "").replace("```", "").strip()
|
1039 |
+
|
1040 |
# 5) ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ์์ฑ
|
1041 |
if "demo.launch()" not in code:
|
1042 |
full_app_code = code + "\n\nif __name__ == '__main__':\n demo.launch()"
|
1043 |
else:
|
1044 |
full_app_code = code
|
1045 |
+
|
1046 |
# 6) ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋
|
1047 |
with open("app.py", "w", encoding="utf-8") as f:
|
1048 |
f.write(full_app_code)
|
|
|
1053 |
repo_id=repo_id,
|
1054 |
repo_type="space"
|
1055 |
)
|
1056 |
+
|
1057 |
# 7) requirements.txt ์์ฑ ๋ฐ ์
๋ก๋
|
1058 |
analysis_result = analyze_code(code)
|
1059 |
requirements = ""
|
|
|
1060 |
# HTML์์ requirements.txt ์น์
์ฐพ๊ธฐ
|
1061 |
if "<h3>๐ Requirements.txt</h3>" in analysis_result:
|
1062 |
start_idx = analysis_result.find("<pre>") + 5
|
1063 |
end_idx = analysis_result.find("</pre>")
|
1064 |
if start_idx > 4 and end_idx > 0:
|
1065 |
requirements = analysis_result[start_idx:end_idx].strip()
|
1066 |
+
|
1067 |
+
# ๊ธฐ๋ณธ requirements ์ค์
|
1068 |
+
default_requirements = [
|
1069 |
+
'gradio==5.5.0',
|
1070 |
+
'huggingface_hub',
|
1071 |
+
'transformers',
|
1072 |
+
'torch',
|
1073 |
+
'torchvision',
|
1074 |
+
'diffusers',
|
1075 |
+
'accelerate',
|
1076 |
+
'safetensors',
|
1077 |
+
'scipy',
|
1078 |
+
'pillow',
|
1079 |
+
'numpy'
|
1080 |
+
]
|
1081 |
+
|
1082 |
+
# ๊ธฐ์กด requirements๊ฐ ์์ผ๋ฉด ํ์ฑํ์ฌ ์ค๋ณต ์ ๊ฑฐ
|
1083 |
+
existing_requirements = set()
|
1084 |
+
if requirements:
|
1085 |
+
existing_requirements = {line.split('==')[0] if '==' in line else line
|
1086 |
+
for line in requirements.split('\n') if line.strip()}
|
1087 |
+
|
1088 |
+
# ์ต์ข
requirements ์์ฑ
|
1089 |
+
final_requirements = set()
|
1090 |
+
|
1091 |
+
# ๊ธฐ์กด requirements ์ถ๊ฐ (๋ฒ์ ์ ๋ณด ์ ์ง)
|
1092 |
+
if requirements:
|
1093 |
+
for req in requirements.split('\n'):
|
1094 |
+
if req.strip():
|
1095 |
+
pkg_name = req.split('==')[0] if '==' in req else req
|
1096 |
+
final_requirements.add(req.strip())
|
1097 |
+
|
1098 |
+
# ๊ธฐ๋ณธ requirements ์ถ๊ฐ (์๋ ๊ฒ๋ง)
|
1099 |
+
for req in default_requirements:
|
1100 |
+
pkg_name = req.split('==')[0] if '==' in req else req
|
1101 |
+
if pkg_name not in {r.split('==')[0] if '==' in r else r for r in final_requirements}:
|
1102 |
+
final_requirements.add(req)
|
1103 |
+
|
1104 |
+
# requirements.txt ์์ฑ
|
1105 |
with open("requirements.txt", "w") as f:
|
1106 |
+
f.write('\n'.join(sorted(final_requirements)))
|
1107 |
|
1108 |
api.upload_file(
|
1109 |
path_or_fileobj="requirements.txt",
|
|
|
1111 |
repo_id=repo_id,
|
1112 |
repo_type="space"
|
1113 |
)
|
1114 |
+
|
1115 |
# 8) ๊ฒฐ๊ณผ ๋ฐํ
|
1116 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
1117 |
return f'๋ฐฐํฌ ์๋ฃ! Private Space๋ก ์์ฑ๋์์ต๋๋ค. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ Space ์ด๊ธฐ</a>'
|
|
|
1118 |
except Exception as e:
|
1119 |
return f"๋ฐฐํฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
|
|
|
|
1120 |
|
1121 |
# Demo ์ธ์คํด์ค ์์ฑ
|
1122 |
demo_instance = Demo()
|