Update app.py
Browse files
app.py
CHANGED
@@ -787,7 +787,6 @@ def generate_space_name():
|
|
787 |
return ''.join(random.choice(letters) for i in range(6))
|
788 |
|
789 |
def deploy_to_huggingface(code: str):
|
790 |
-
"""HuggingFace Spaces์ Gradio ์ฑ ๋ฐฐํฌ (private์ผ๋ก ์ค์ )"""
|
791 |
try:
|
792 |
# 1) ๊ธฐ๋ณธ ๊ฒ์ฆ
|
793 |
token = os.getenv("HF_TOKEN")
|
@@ -829,41 +828,40 @@ def deploy_to_huggingface(code: str):
|
|
829 |
repo_type="space",
|
830 |
space_sdk="gradio",
|
831 |
token=token,
|
832 |
-
private=True
|
833 |
)
|
834 |
except Exception as e:
|
835 |
if "Too Many Requests" in str(e):
|
836 |
return "ํ์ฌ HuggingFace API ์์ฒญ์ด ์ ํ๋์์ต๋๋ค. ์ ์ ํ ๋ค์ ์๋ํด์ฃผ์ธ์."
|
837 |
raise e
|
838 |
|
839 |
-
# 6)
|
840 |
-
|
841 |
-
|
|
|
|
|
842 |
import gradio as gr
|
843 |
import numpy as np
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
import
|
850 |
-
|
851 |
-
|
|
|
|
|
|
|
852 |
|
853 |
# Main application code
|
854 |
-
{
|
855 |
-
|
856 |
-
# Launch configuration
|
857 |
-
if __name__ == "__main__":
|
858 |
-
demo.launch()
|
859 |
"""
|
860 |
|
861 |
-
#
|
862 |
-
# app.py ์์ฑ
|
863 |
with open("app.py", "w", encoding="utf-8") as f:
|
864 |
f.write(full_app_code)
|
865 |
|
866 |
-
# app.py ์
๋ก๋
|
867 |
api.upload_file(
|
868 |
path_or_fileobj="app.py",
|
869 |
path_in_repo="app.py",
|
@@ -871,21 +869,15 @@ if __name__ == "__main__":
|
|
871 |
repo_type="space"
|
872 |
)
|
873 |
|
874 |
-
# requirements.txt ์์ฑ
|
875 |
requirements = """
|
876 |
gradio>=4.0.0
|
877 |
-
torch
|
878 |
-
transformers
|
879 |
numpy
|
880 |
-
pandas
|
881 |
-
matplotlib
|
882 |
-
plotly
|
883 |
""".strip()
|
884 |
|
885 |
with open("requirements.txt", "w") as f:
|
886 |
f.write(requirements)
|
887 |
|
888 |
-
# requirements.txt ์
๋ก๋
|
889 |
api.upload_file(
|
890 |
path_or_fileobj="requirements.txt",
|
891 |
path_in_repo="requirements.txt",
|
@@ -893,7 +885,7 @@ plotly
|
|
893 |
repo_type="space"
|
894 |
)
|
895 |
|
896 |
-
#
|
897 |
deploy_count += 1
|
898 |
with open(rate_limit_file, 'w') as f:
|
899 |
json.dump({
|
@@ -901,7 +893,7 @@ plotly
|
|
901 |
'deploy_count': deploy_count
|
902 |
}, f)
|
903 |
|
904 |
-
#
|
905 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
906 |
return f"""
|
907 |
๋ฐฐํฌ ์๋ฃ!
|
|
|
787 |
return ''.join(random.choice(letters) for i in range(6))
|
788 |
|
789 |
def deploy_to_huggingface(code: str):
|
|
|
790 |
try:
|
791 |
# 1) ๊ธฐ๋ณธ ๊ฒ์ฆ
|
792 |
token = os.getenv("HF_TOKEN")
|
|
|
828 |
repo_type="space",
|
829 |
space_sdk="gradio",
|
830 |
token=token,
|
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 |
+
# 6) ์ฝ๋ ์ ๋ฆฌ - ์ค๋ณต ์ ๊ฑฐ
|
839 |
+
code = code.replace("```python", "").replace("```", "").strip()
|
840 |
+
|
841 |
+
# ํ์ imports๋ง ํฌํจ
|
842 |
+
imports = """
|
843 |
import gradio as gr
|
844 |
import numpy as np
|
845 |
+
"""
|
846 |
+
# ๋ฉ์ธ ์ฝ๋์์ ์ค๋ณต imports ์ ๊ฑฐ
|
847 |
+
code_lines = code.split('\n')
|
848 |
+
main_code = []
|
849 |
+
for line in code_lines:
|
850 |
+
if not line.startswith('import ') and not line.startswith('from '):
|
851 |
+
main_code.append(line)
|
852 |
+
|
853 |
+
# 7) ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์
์ฝ๋ ์์ฑ
|
854 |
+
full_app_code = f"""
|
855 |
+
{imports.strip()}
|
856 |
|
857 |
# Main application code
|
858 |
+
{'\n'.join(main_code)}
|
|
|
|
|
|
|
|
|
859 |
"""
|
860 |
|
861 |
+
# 8) ํ์ผ ์์ฑ ๋ฐ ์
๋ก๋
|
|
|
862 |
with open("app.py", "w", encoding="utf-8") as f:
|
863 |
f.write(full_app_code)
|
864 |
|
|
|
865 |
api.upload_file(
|
866 |
path_or_fileobj="app.py",
|
867 |
path_in_repo="app.py",
|
|
|
869 |
repo_type="space"
|
870 |
)
|
871 |
|
872 |
+
# requirements.txt ์์ฑ ๋ฐ ์
๋ก๋
|
873 |
requirements = """
|
874 |
gradio>=4.0.0
|
|
|
|
|
875 |
numpy
|
|
|
|
|
|
|
876 |
""".strip()
|
877 |
|
878 |
with open("requirements.txt", "w") as f:
|
879 |
f.write(requirements)
|
880 |
|
|
|
881 |
api.upload_file(
|
882 |
path_or_fileobj="requirements.txt",
|
883 |
path_in_repo="requirements.txt",
|
|
|
885 |
repo_type="space"
|
886 |
)
|
887 |
|
888 |
+
# 9) Rate Limit ์ ๋ณด ์
๋ฐ์ดํธ
|
889 |
deploy_count += 1
|
890 |
with open(rate_limit_file, 'w') as f:
|
891 |
json.dump({
|
|
|
893 |
'deploy_count': deploy_count
|
894 |
}, f)
|
895 |
|
896 |
+
# 10) ๊ฒฐ๊ณผ ๋ฐํ
|
897 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
898 |
return f"""
|
899 |
๋ฐฐํฌ ์๋ฃ!
|