seawolf2357 commited on
Commit
4e264fd
ยท
verified ยท
1 Parent(s): c46c8e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -49
app.py CHANGED
@@ -793,35 +793,13 @@ def deploy_to_huggingface(code: str):
793
  if not token:
794
  return "HuggingFace ํ† ํฐ์ด ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
795
 
796
- # 2) Rate Limit ๊ด€๋ฆฌ
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
- # 5) Space ์ƒ์„ฑ (private๋กœ ์„ค์ •)
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
- # 6) ์ฝ”๋“œ ์ •๋ฆฌ ๋ฐ import ๋ถ„์„
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(['gradio==5.5.0']) # gradio๋Š” ๊ธฐ๋ณธ์œผ๋กœ ํฌํ•จ
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
- else:
 
 
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
- # 7) ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ ์ƒ์„ฑ
962
- # demo.launch() ํ˜ธ์ถœ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—†์œผ๋ฉด ์ถ”๊ฐ€
 
 
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
- # 8) ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
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
- error_msg = str(e)
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()