seawolf2357 commited on
Commit
abfad22
ยท
verified ยท
1 Parent(s): 49a1175

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
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 # private๋กœ ์„ค์ •
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
- full_app_code = f"""
841
- # Required imports
 
 
842
  import gradio as gr
843
  import numpy as np
844
- import pandas as pd
845
- import torch
846
- import matplotlib.pyplot as plt
847
- import plotly.express as px
848
- from pathlib import Path
849
- import json
850
- import os
851
- import sys
 
 
 
852
 
853
  # Main application code
854
- {code}
855
-
856
- # Launch configuration
857
- if __name__ == "__main__":
858
- demo.launch()
859
  """
860
 
861
- # 7) ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
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
- # 8) Rate Limit ์ •๋ณด ์—…๋ฐ์ดํŠธ
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
- # 9) ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
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
  ๋ฐฐํฌ ์™„๋ฃŒ!