seawolf2357 commited on
Commit
b478352
ยท
verified ยท
1 Parent(s): 7f3a5ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -11
app.py CHANGED
@@ -835,21 +835,41 @@ def deploy_to_huggingface(code: str):
835
  return "ํ˜„์žฌ HuggingFace API ์š”์ฒญ์ด ์ œํ•œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”."
836
  raise e
837
 
838
- # 6) ์ฝ”๋“œ ์ •๋ฆฌ - ์ค‘๋ณต ์ œ๊ฑฐ
839
  code = code.replace("```python", "").replace("```", "").strip()
840
 
841
- # ํ•„์ˆ˜ imports๋งŒ ํฌํ•จ
842
- imports = "import gradio as gr\nimport numpy as np\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
843
 
844
- # ๋ฉ”์ธ ์ฝ”๋“œ์—์„œ ์ค‘๋ณต imports ์ œ๊ฑฐ
845
- code_lines = code.split('\n')
846
- main_code = []
847
- for line in code_lines:
848
- if not line.startswith('import ') and not line.startswith('from '):
849
- main_code.append(line)
 
 
850
 
 
 
 
851
  # 7) ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ ์ƒ์„ฑ
852
- full_app_code = imports + "\n".join(main_code)
853
 
854
  # 8) ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
855
  with open("app.py", "w", encoding="utf-8") as f:
@@ -863,7 +883,7 @@ def deploy_to_huggingface(code: str):
863
  )
864
 
865
  # requirements.txt ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
866
- requirements = "gradio>=4.0.0\nnumpy"
867
 
868
  with open("requirements.txt", "w") as f:
869
  f.write(requirements)
 
835
  return "ํ˜„์žฌ HuggingFace API ์š”์ฒญ์ด ์ œํ•œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”."
836
  raise e
837
 
838
+ # 6) ์ฝ”๋“œ ์ •๋ฆฌ ๋ฐ import ๋ถ„์„
839
  code = code.replace("```python", "").replace("```", "").strip()
840
 
841
+ # Import ๋ฌธ ์ถ”์ถœ ๋ฐ requirements.txt ์ƒ์„ฑ์„ ์œ„ํ•œ ๋งคํ•‘
842
+ import_mapping = {
843
+ 'gradio': 'gradio>=4.0.0',
844
+ 'numpy': 'numpy',
845
+ 'pandas': 'pandas',
846
+ 'torch': 'torch',
847
+ 'matplotlib': 'matplotlib',
848
+ 'plotly': 'plotly',
849
+ 'transformers': 'transformers',
850
+ 'PIL': 'Pillow',
851
+ 'cv2': 'opencv-python',
852
+ 'sklearn': 'scikit-learn',
853
+ 'tensorflow': 'tensorflow',
854
+ 'scipy': 'scipy'
855
+ }
856
+
857
+ required_packages = set(['gradio>=4.0.0']) # gradio๋Š” ๊ธฐ๋ณธ์œผ๋กœ ํฌํ•จ
858
 
859
+ # ์ฝ”๋“œ์—์„œ import ๋ฌธ ๋ถ„์„
860
+ for line in code.split('\n'):
861
+ if line.startswith('import ') or line.startswith('from '):
862
+ # 'import' ๋˜๋Š” 'from' ๋ฌธ์—์„œ ํŒจํ‚ค์ง€ ์ด๋ฆ„ ์ถ”์ถœ
863
+ if line.startswith('import '):
864
+ package = line.split('import ')[1].split()[0].split('.')[0]
865
+ else:
866
+ package = line.split('from ')[1].split()[0].split('.')[0]
867
 
868
+ if package in import_mapping:
869
+ required_packages.add(import_mapping[package])
870
+
871
  # 7) ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ ์ƒ์„ฑ
872
+ full_app_code = code
873
 
874
  # 8) ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
875
  with open("app.py", "w", encoding="utf-8") as f:
 
883
  )
884
 
885
  # requirements.txt ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
886
+ requirements = '\n'.join(sorted(required_packages))
887
 
888
  with open("requirements.txt", "w") as f:
889
  f.write(requirements)