Update app.py
Browse files
app.py
CHANGED
@@ -793,35 +793,13 @@ def deploy_to_huggingface(code: str):
|
|
793 |
if not token:
|
794 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
795 |
|
796 |
-
# 2)
|
797 |
-
rate_limit_file = "rate_limit.json"
|
798 |
-
current_time = time.time()
|
799 |
-
|
800 |
-
try:
|
801 |
-
with open(rate_limit_file, 'r') as f:
|
802 |
-
rate_limit_data = json.load(f)
|
803 |
-
last_deploy_time = rate_limit_data.get('last_deploy_time', 0)
|
804 |
-
deploy_count = rate_limit_data.get('deploy_count', 0)
|
805 |
-
except FileNotFoundError:
|
806 |
-
last_deploy_time = 0
|
807 |
-
deploy_count = 0
|
808 |
-
|
809 |
-
# 3) Rate Limit ์ฒดํฌ
|
810 |
-
if current_time - last_deploy_time < 54000 and deploy_count >= 5:
|
811 |
-
remaining_time = int((54000 - (current_time - last_deploy_time)) / 3600)
|
812 |
-
return f"๋ฐฐํฌ ํ์ ์ ํ์ ๋๋ฌํ์ต๋๋ค. {remaining_time}์๊ฐ ํ์ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
813 |
-
|
814 |
-
if current_time - last_deploy_time >= 54000:
|
815 |
-
deploy_count = 0
|
816 |
-
last_deploy_time = current_time
|
817 |
-
|
818 |
-
# 4) Space ์์ฑ ์ค๋น
|
819 |
api = HfApi(token=token)
|
820 |
space_name = generate_space_name()
|
821 |
username = api.whoami()['name']
|
822 |
repo_id = f"{username}/{space_name}"
|
823 |
|
824 |
-
#
|
825 |
try:
|
826 |
create_repo(
|
827 |
repo_id,
|
@@ -831,11 +809,9 @@ def deploy_to_huggingface(code: str):
|
|
831 |
private=True
|
832 |
)
|
833 |
except Exception as e:
|
834 |
-
if "Too Many Requests" in str(e):
|
835 |
-
return "ํ์ฌ HuggingFace API ์์ฒญ์ด ์ ํ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
836 |
raise e
|
837 |
|
838 |
-
#
|
839 |
code = code.replace("```python", "").replace("```", "").strip()
|
840 |
|
841 |
# Import ๋ฌธ ์ถ์ถ ๋ฐ requirements.txt ์์ฑ์ ์ํ ๋งคํ
|
@@ -939,7 +915,7 @@ def deploy_to_huggingface(code: str):
|
|
939 |
'pygraphviz': 'pygraphviz'
|
940 |
}
|
941 |
|
942 |
-
required_packages = set(
|
943 |
|
944 |
# ์ฝ๋์์ import ๋ฌธ ๋ถ์
|
945 |
import_pattern = r'^(?:from\s+(\S+)|import\s+([^,\s]+)(?:\s*,\s*([^,\s]+))*)'
|
@@ -949,23 +925,27 @@ def deploy_to_huggingface(code: str):
|
|
949 |
if line.startswith('import ') or line.startswith('from '):
|
950 |
matches = re.match(import_pattern, line)
|
951 |
if matches:
|
952 |
-
if matches.group(1):
|
953 |
package = matches.group(1).split('.')[0]
|
954 |
-
|
|
|
|
|
955 |
packages = [p.strip() for p in re.findall(r'[\w.]+', line[7:])]
|
956 |
for package in packages:
|
957 |
package = package.split('.')[0]
|
958 |
if package in import_mapping:
|
959 |
required_packages.add(import_mapping[package])
|
960 |
|
961 |
-
#
|
962 |
-
|
|
|
|
|
963 |
if "demo.launch()" not in code:
|
964 |
full_app_code = code + "\n\nif __name__ == '__main__':\n demo.launch()"
|
965 |
else:
|
966 |
full_app_code = code
|
967 |
|
968 |
-
#
|
969 |
with open("app.py", "w", encoding="utf-8") as f:
|
970 |
f.write(full_app_code)
|
971 |
|
@@ -978,7 +958,6 @@ def deploy_to_huggingface(code: str):
|
|
978 |
|
979 |
# requirements.txt ์์ฑ ๋ฐ ์
๋ก๋
|
980 |
requirements = '\n'.join(sorted(required_packages))
|
981 |
-
|
982 |
with open("requirements.txt", "w") as f:
|
983 |
f.write(requirements)
|
984 |
|
@@ -989,25 +968,12 @@ def deploy_to_huggingface(code: str):
|
|
989 |
repo_type="space"
|
990 |
)
|
991 |
|
992 |
-
|
993 |
-
|
994 |
-
# 9) Rate Limit ์ ๋ณด ์
๋ฐ์ดํธ
|
995 |
-
deploy_count += 1
|
996 |
-
with open(rate_limit_file, 'w') as f:
|
997 |
-
json.dump({
|
998 |
-
'last_deploy_time': current_time,
|
999 |
-
'deploy_count': deploy_count
|
1000 |
-
}, f)
|
1001 |
-
|
1002 |
-
# 10) ๊ฒฐ๊ณผ ๋ฐํ
|
1003 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
1004 |
return f'๋ฐฐํฌ ์๋ฃ! Private Space๋ก ์์ฑ๋์์ต๋๋ค. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ Space ์ด๊ธฐ</a>'
|
1005 |
|
1006 |
except Exception as e:
|
1007 |
-
|
1008 |
-
if "Too Many Requests" in error_msg:
|
1009 |
-
return "HuggingFace API ์์ฒญ ํ๋๋ฅผ ์ด๊ณผํ์ต๋๋ค. 15์๊ฐ ํ์ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
1010 |
-
return f"๋ฐฐํฌ ์ค ์ค๋ฅ ๋ฐ์: {error_msg}"
|
1011 |
|
1012 |
# Demo ์ธ์คํด์ค ์์ฑ
|
1013 |
demo_instance = Demo()
|
|
|
793 |
if not token:
|
794 |
return "HuggingFace ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค."
|
795 |
|
796 |
+
# 2) Space ์์ฑ ์ค๋น
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
797 |
api = HfApi(token=token)
|
798 |
space_name = generate_space_name()
|
799 |
username = api.whoami()['name']
|
800 |
repo_id = f"{username}/{space_name}"
|
801 |
|
802 |
+
# 3) Space ์์ฑ (private๋ก ์ค์ )
|
803 |
try:
|
804 |
create_repo(
|
805 |
repo_id,
|
|
|
809 |
private=True
|
810 |
)
|
811 |
except Exception as e:
|
|
|
|
|
812 |
raise e
|
813 |
|
814 |
+
# 4) ์ฝ๋ ์ ๋ฆฌ ๋ฐ import ๋ถ์
|
815 |
code = code.replace("```python", "").replace("```", "").strip()
|
816 |
|
817 |
# Import ๋ฌธ ์ถ์ถ ๋ฐ requirements.txt ์์ฑ์ ์ํ ๋งคํ
|
|
|
915 |
'pygraphviz': 'pygraphviz'
|
916 |
}
|
917 |
|
918 |
+
required_packages = set()
|
919 |
|
920 |
# ์ฝ๋์์ import ๋ฌธ ๋ถ์
|
921 |
import_pattern = r'^(?:from\s+(\S+)|import\s+([^,\s]+)(?:\s*,\s*([^,\s]+))*)'
|
|
|
925 |
if line.startswith('import ') or line.startswith('from '):
|
926 |
matches = re.match(import_pattern, line)
|
927 |
if matches:
|
928 |
+
if matches.group(1): # from ... import ...
|
929 |
package = matches.group(1).split('.')[0]
|
930 |
+
if package in import_mapping:
|
931 |
+
required_packages.add(import_mapping[package])
|
932 |
+
else: # import ...
|
933 |
packages = [p.strip() for p in re.findall(r'[\w.]+', line[7:])]
|
934 |
for package in packages:
|
935 |
package = package.split('.')[0]
|
936 |
if package in import_mapping:
|
937 |
required_packages.add(import_mapping[package])
|
938 |
|
939 |
+
# gradio๋ ํญ์ ํฌํจ
|
940 |
+
required_packages.add('gradio==5.5.0')
|
941 |
+
|
942 |
+
# 5) ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ์์ฑ
|
943 |
if "demo.launch()" not in code:
|
944 |
full_app_code = code + "\n\nif __name__ == '__main__':\n demo.launch()"
|
945 |
else:
|
946 |
full_app_code = code
|
947 |
|
948 |
+
# 6) ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋
|
949 |
with open("app.py", "w", encoding="utf-8") as f:
|
950 |
f.write(full_app_code)
|
951 |
|
|
|
958 |
|
959 |
# requirements.txt ์์ฑ ๋ฐ ์
๋ก๋
|
960 |
requirements = '\n'.join(sorted(required_packages))
|
|
|
961 |
with open("requirements.txt", "w") as f:
|
962 |
f.write(requirements)
|
963 |
|
|
|
968 |
repo_type="space"
|
969 |
)
|
970 |
|
971 |
+
# 7) ๊ฒฐ๊ณผ ๋ฐํ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
972 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
973 |
return f'๋ฐฐํฌ ์๋ฃ! Private Space๋ก ์์ฑ๋์์ต๋๋ค. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">์ฌ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ Space ์ด๊ธฐ</a>'
|
974 |
|
975 |
except Exception as e:
|
976 |
+
return f"๋ฐฐํฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
|
|
|
|
|
|
977 |
|
978 |
# Demo ์ธ์คํด์ค ์์ฑ
|
979 |
demo_instance = Demo()
|