Update app.py
Browse files
app.py
CHANGED
@@ -974,134 +974,8 @@ def deploy_to_huggingface(code: str):
|
|
974 |
except Exception as e:
|
975 |
raise e
|
976 |
|
977 |
-
# 4) 코드 정리
|
978 |
code = code.replace("```python", "").replace("```", "").strip()
|
979 |
-
|
980 |
-
# Import 문 추출 및 requirements.txt 생성을 위한 매핑
|
981 |
-
import_mapping = {
|
982 |
-
'numpy': 'numpy',
|
983 |
-
'pandas': 'pandas',
|
984 |
-
'torch': 'torch',
|
985 |
-
'matplotlib': 'matplotlib',
|
986 |
-
'plotly': 'plotly',
|
987 |
-
'transformers': 'transformers',
|
988 |
-
'PIL': 'Pillow',
|
989 |
-
'cv2': 'opencv-python',
|
990 |
-
'sklearn': 'scikit-learn',
|
991 |
-
'tensorflow': 'tensorflow',
|
992 |
-
'scipy': 'scipy',
|
993 |
-
'librosa': 'librosa',
|
994 |
-
'soundfile': 'soundfile',
|
995 |
-
'nltk': 'nltk',
|
996 |
-
'spacy': 'spacy',
|
997 |
-
'gensim': 'gensim',
|
998 |
-
'keras': 'keras',
|
999 |
-
'seaborn': 'seaborn',
|
1000 |
-
'bokeh': 'bokeh',
|
1001 |
-
'requests': 'requests',
|
1002 |
-
'beautifulsoup4': 'beautifulsoup4',
|
1003 |
-
'scikit-image': 'scikit-image',
|
1004 |
-
'opencv-python': 'opencv-python',
|
1005 |
-
'tqdm': 'tqdm',
|
1006 |
-
'streamlit': 'streamlit',
|
1007 |
-
'dash': 'dash',
|
1008 |
-
'flask': 'flask',
|
1009 |
-
'django': 'django',
|
1010 |
-
'sqlalchemy': 'sqlalchemy',
|
1011 |
-
'pymongo': 'pymongo',
|
1012 |
-
'redis': 'redis',
|
1013 |
-
'psycopg2': 'psycopg2-binary',
|
1014 |
-
'mysql-connector': 'mysql-connector-python',
|
1015 |
-
'pytest': 'pytest',
|
1016 |
-
'unittest': 'unittest2',
|
1017 |
-
'sphinx': 'sphinx',
|
1018 |
-
'jupyter': 'jupyter',
|
1019 |
-
'ipython': 'ipython',
|
1020 |
-
'fastapi': 'fastapi',
|
1021 |
-
'uvicorn': 'uvicorn',
|
1022 |
-
'aiohttp': 'aiohttp',
|
1023 |
-
'websockets': 'websockets',
|
1024 |
-
'pyyaml': 'pyyaml',
|
1025 |
-
'json': 'json5',
|
1026 |
-
'xml': 'lxml',
|
1027 |
-
'html': 'html5lib',
|
1028 |
-
'urllib3': 'urllib3',
|
1029 |
-
'cryptography': 'cryptography',
|
1030 |
-
'bcrypt': 'bcrypt',
|
1031 |
-
'jwt': 'pyjwt',
|
1032 |
-
'pillow': 'Pillow',
|
1033 |
-
'imageio': 'imageio',
|
1034 |
-
'moviepy': 'moviepy',
|
1035 |
-
'ffmpeg': 'ffmpeg-python',
|
1036 |
-
'pydub': 'pydub',
|
1037 |
-
'wave': 'wave',
|
1038 |
-
'pyaudio': 'pyaudio',
|
1039 |
-
'sounddevice': 'sounddevice',
|
1040 |
-
'pygame': 'pygame',
|
1041 |
-
'kivy': 'kivy',
|
1042 |
-
'pyqt5': 'PyQt5',
|
1043 |
-
'tkinter': 'tk',
|
1044 |
-
'wx': 'wxPython',
|
1045 |
-
'pyside2': 'PySide2',
|
1046 |
-
'deap': 'deap',
|
1047 |
-
'gym': 'gym',
|
1048 |
-
'stable-baselines3': 'stable-baselines3',
|
1049 |
-
'optuna': 'optuna',
|
1050 |
-
'hyperopt': 'hyperopt',
|
1051 |
-
'ray': 'ray',
|
1052 |
-
'dask': 'dask',
|
1053 |
-
'vaex': 'vaex',
|
1054 |
-
'modin': 'modin',
|
1055 |
-
'cupy': 'cupy',
|
1056 |
-
'jax': 'jax',
|
1057 |
-
'numba': 'numba',
|
1058 |
-
'cython': 'cython',
|
1059 |
-
'sympy': 'sympy',
|
1060 |
-
'statsmodels': 'statsmodels',
|
1061 |
-
'prophet': 'prophet',
|
1062 |
-
'lightgbm': 'lightgbm',
|
1063 |
-
'xgboost': 'xgboost',
|
1064 |
-
'catboost': 'catboost',
|
1065 |
-
'shap': 'shap',
|
1066 |
-
'lime': 'lime',
|
1067 |
-
'eli5': 'eli5',
|
1068 |
-
'yellowbrick': 'yellowbrick',
|
1069 |
-
'altair': 'altair',
|
1070 |
-
'plotnine': 'plotnine',
|
1071 |
-
'folium': 'folium',
|
1072 |
-
'geopandas': 'geopandas',
|
1073 |
-
'shapely': 'shapely',
|
1074 |
-
'rasterio': 'rasterio',
|
1075 |
-
'networkx': 'networkx',
|
1076 |
-
'graphviz': 'graphviz',
|
1077 |
-
'pydot': 'pydot',
|
1078 |
-
'dlib': 'dlib',
|
1079 |
-
'pygraphviz': 'pygraphviz'
|
1080 |
-
}
|
1081 |
-
|
1082 |
-
required_packages = set()
|
1083 |
-
|
1084 |
-
# 코드에서 import 문 분석
|
1085 |
-
import_pattern = r'^(?:from\s+(\S+)|import\s+([^,\s]+)(?:\s*,\s*([^,\s]+))*)'
|
1086 |
-
|
1087 |
-
for line in code.split('\n'):
|
1088 |
-
line = line.strip()
|
1089 |
-
if line.startswith('import ') or line.startswith('from '):
|
1090 |
-
matches = re.match(import_pattern, line)
|
1091 |
-
if matches:
|
1092 |
-
if matches.group(1): # from ... import ...
|
1093 |
-
package = matches.group(1).split('.')[0]
|
1094 |
-
if package in import_mapping:
|
1095 |
-
required_packages.add(import_mapping[package])
|
1096 |
-
else: # import ...
|
1097 |
-
packages = [p.strip() for p in re.findall(r'[\w.]+', line[7:])]
|
1098 |
-
for package in packages:
|
1099 |
-
package = package.split('.')[0]
|
1100 |
-
if package in import_mapping:
|
1101 |
-
required_packages.add(import_mapping[package])
|
1102 |
-
|
1103 |
-
# gradio는 항상 포함
|
1104 |
-
required_packages.add('gradio==5.5.0')
|
1105 |
|
1106 |
# 5) 전체 애플리케이션 코드 생성
|
1107 |
if "demo.launch()" not in code:
|
@@ -1120,8 +994,21 @@ def deploy_to_huggingface(code: str):
|
|
1120 |
repo_type="space"
|
1121 |
)
|
1122 |
|
1123 |
-
# requirements.txt 생성 및 업로드
|
1124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1125 |
with open("requirements.txt", "w") as f:
|
1126 |
f.write(requirements)
|
1127 |
|
@@ -1132,7 +1019,7 @@ def deploy_to_huggingface(code: str):
|
|
1132 |
repo_type="space"
|
1133 |
)
|
1134 |
|
1135 |
-
#
|
1136 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
1137 |
return f'배포 완료! Private Space로 생성되었습니다. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">여기를 클릭하여 Space 열기</a>'
|
1138 |
|
|
|
974 |
except Exception as e:
|
975 |
raise e
|
976 |
|
977 |
+
# 4) 코드 정리
|
978 |
code = code.replace("```python", "").replace("```", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
979 |
|
980 |
# 5) 전체 애플리케이션 코드 생성
|
981 |
if "demo.launch()" not in code:
|
|
|
994 |
repo_type="space"
|
995 |
)
|
996 |
|
997 |
+
# 7) requirements.txt 생성 및 업로드
|
998 |
+
analysis_result = analyze_code(code)
|
999 |
+
requirements = ""
|
1000 |
+
|
1001 |
+
# HTML에서 requirements.txt 섹션 찾기
|
1002 |
+
if "<h3>📋 Requirements.txt</h3>" in analysis_result:
|
1003 |
+
start_idx = analysis_result.find("<pre>") + 5
|
1004 |
+
end_idx = analysis_result.find("</pre>")
|
1005 |
+
if start_idx > 4 and end_idx > 0:
|
1006 |
+
requirements = analysis_result[start_idx:end_idx].strip()
|
1007 |
+
|
1008 |
+
# requirements가 비어있으면 기본값 설정
|
1009 |
+
if not requirements:
|
1010 |
+
requirements = 'gradio==5.5.0'
|
1011 |
+
|
1012 |
with open("requirements.txt", "w") as f:
|
1013 |
f.write(requirements)
|
1014 |
|
|
|
1019 |
repo_type="space"
|
1020 |
)
|
1021 |
|
1022 |
+
# 8) 결과 반환
|
1023 |
space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
|
1024 |
return f'배포 완료! Private Space로 생성되었습니다. <a href="{space_url}" target="_blank" style="color: #1890ff; text-decoration: underline; cursor: pointer;">여기를 클릭하여 Space 열기</a>'
|
1025 |
|