Spaces:
Runtime error

Crislene commited on
Commit
4c08847
·
verified ·
1 Parent(s): 1c42786

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -1
app.py CHANGED
@@ -1,4 +1,118 @@
1
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def greet(name):
4
  return f"Hello {name}!"
@@ -7,3 +121,42 @@ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
 
8
  if __name__ == "__main__":
9
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import zipfile
4
+ from IPython.display import display, HTML
5
+ from google.colab import files
6
+
7
+ def generate_valid_deployment_files(main_app_code=None):
8
+ """
9
+ Gera arquivos de deploy com requirements.txt válido
10
+ """
11
+ print("🛠️ Gerando arquivos de deploy compatíveis com Hugging Face Spaces...")
12
+
13
+ try:
14
+ # 1. Capturar pacotes instalados (sem opções como -U)
15
+ pip_list = !pip list --format=freeze
16
+ requirements = set()
17
+
18
+ for package in pip_list:
19
+ if '==' in package:
20
+ pkg_name, pkg_version = package.split('==', 1)
21
+ requirements.add(f"{pkg_name}=={pkg_version.split(' ')[0]}")
22
+
23
+ # 2. Garantir pacotes essenciais
24
+ essential_packages = {
25
+ 'gradio>=3.0.0',
26
+ 'fastapi>=0.68.0',
27
+ 'uvicorn>=0.15.0',
28
+ 'python-multipart>=0.0.5'
29
+ }
30
+ requirements.update(essential_packages)
31
+
32
+ # 3. Criar app.py se não fornecido
33
+ if main_app_code is None:
34
+ main_app_code = """import gradio as gr
35
+
36
+ def greet(name):
37
+ return f"Hello {name}!"
38
+
39
+ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
40
+
41
+ if __name__ == "__main__":
42
+ demo.launch()
43
+ """
44
+ print("ℹ️ Usando template padrão para app.py")
45
+
46
+ # 4. Criar estrutura de arquivos
47
+ os.makedirs('hf_deploy', exist_ok=True)
48
+
49
+ # 5. Salvar requirements.txt válido
50
+ with open('hf_deploy/requirements.txt', 'w') as f:
51
+ f.write('\n'.join(sorted(requirements)))
52
+
53
+ # 6. Salvar app.py
54
+ with open('hf_deploy/app.py', 'w') as f:
55
+ f.write(main_app_code)
56
+
57
+ # 7. Criar ZIP
58
+ zip_filename = 'hf_deployment.zip'
59
+ with zipfile.ZipFile(zip_filename, 'w') as zipf:
60
+ for file in os.listdir('hf_deploy'):
61
+ zipf.write(f'hf_deploy/{file}', arcname=file)
62
+
63
+ # 8. Limpar e fazer download
64
+ !rm -rf hf_deploy
65
+ files.download(zip_filename)
66
+ print("✅ Download do pacote válido iniciado!")
67
+
68
+ return True
69
+
70
+ except Exception as e:
71
+ print(f"❌ Erro: {str(e)}")
72
+ return False
73
+
74
+ # Como usar:
75
+ # 1. Execute todas as células de instalação e configuração
76
+ # 2. Defina seu código principal (opcional)
77
+ # my_app_code = """seu código aqui"""
78
+ # 3. Execute:
79
+ generate_valid_deployment_files()
80
+ # OU
81
+ # generate_valid_deployment_files(main_app_code=my_app_code)
82
+ import os
83
+ import re
84
+ import zipfile
85
+ from IPython.display import display, HTML
86
+ from google.colab import files
87
+
88
+ def generate_valid_deployment_files(main_app_code=None):
89
+ """
90
+ Gera arquivos de deploy com requirements.txt válido
91
+ """
92
+ print("🛠️ Gerando arquivos de deploy compatíveis com Hugging Face Spaces...")
93
+
94
+ try:
95
+ # 1. Capturar pacotes instalados (sem opções como -U)
96
+ pip_list = !pip list --format=freeze
97
+ requirements = set()
98
+
99
+ for package in pip_list:
100
+ if '==' in package:
101
+ pkg_name, pkg_version = package.split('==', 1)
102
+ requirements.add(f"{pkg_name}=={pkg_version.split(' ')[0]}")
103
+
104
+ # 2. Garantir pacotes essenciais
105
+ essential_packages = {
106
+ 'gradio>=3.0.0',
107
+ 'fastapi>=0.68.0',
108
+ 'uvicorn>=0.15.0',
109
+ 'python-multipart>=0.0.5'
110
+ }
111
+ requirements.update(essential_packages)
112
+
113
+ # 3. Criar app.py se não fornecido
114
+ if main_app_code is None:
115
+ main_app_code = """import gradio as gr
116
 
117
  def greet(name):
118
  return f"Hello {name}!"
 
121
 
122
  if __name__ == "__main__":
123
  demo.launch()
124
+ """
125
+ print("ℹ️ Usando template padrão para app.py")
126
+
127
+ # 4. Criar estrutura de arquivos
128
+ os.makedirs('hf_deploy', exist_ok=True)
129
+
130
+ # 5. Salvar requirements.txt válido
131
+ with open('hf_deploy/requirements.txt', 'w') as f:
132
+ f.write('\n'.join(sorted(requirements)))
133
+
134
+ # 6. Salvar app.py
135
+ with open('hf_deploy/app.py', 'w') as f:
136
+ f.write(main_app_code)
137
+
138
+ # 7. Criar ZIP
139
+ zip_filename = 'hf_deployment.zip'
140
+ with zipfile.ZipFile(zip_filename, 'w') as zipf:
141
+ for file in os.listdir('hf_deploy'):
142
+ zipf.write(f'hf_deploy/{file}', arcname=file)
143
+
144
+ # 8. Limpar e fazer download
145
+ !rm -rf hf_deploy
146
+ files.download(zip_filename)
147
+ print("✅ Download do pacote válido iniciado!")
148
+
149
+ return True
150
+
151
+ except Exception as e:
152
+ print(f"❌ Erro: {str(e)}")
153
+ return False
154
+
155
+ # Como usar:
156
+ # 1. Execute todas as células de instalação e configuração
157
+ # 2. Defina seu código principal (opcional)
158
+ # my_app_code = """seu código aqui"""
159
+ # 3. Execute:
160
+ generate_valid_deployment_files()
161
+ # OU
162
+ # generate_valid_deployment_files(main_app_code=my_app_code)