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)