ginipick commited on
Commit
afa2ca4
ยท
verified ยท
1 Parent(s): 1c33d61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -17
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
- if not requirements:
1070
- requirements = 'gradio==5.5.0'
1071
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1072
  with open("requirements.txt", "w") as f:
1073
- f.write(requirements)
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()