Hjgugugjhuhjggg commited on
Commit
d0dc403
1 Parent(s): 23013d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -21
app.py CHANGED
@@ -11,8 +11,8 @@ from google.auth import exceptions
11
  from transformers import pipeline
12
  from dotenv import load_dotenv
13
  import uvicorn
14
- import tempfile
15
 
 
16
  load_dotenv()
17
 
18
  API_KEY = os.getenv("API_KEY")
@@ -20,9 +20,11 @@ GCS_BUCKET_NAME = os.getenv("GCS_BUCKET_NAME")
20
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
21
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
22
 
 
23
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
24
  logger = logging.getLogger(__name__)
25
 
 
26
  try:
27
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
28
  storage_client = storage.Client.from_service_account_info(credentials_info)
@@ -63,7 +65,7 @@ class GCSHandler:
63
  logger.error(f"Archivo '{blob_name}' no encontrado en GCS.")
64
  raise HTTPException(status_code=404, detail=f"File '{blob_name}' not found.")
65
  logger.debug(f"Descargando archivo '{blob_name}' de GCS.")
66
- return blob.open("rb")
67
 
68
  def generate_signed_url(self, blob_name, expiration=3600):
69
  blob = self.bucket.blob(blob_name)
@@ -74,6 +76,7 @@ class GCSHandler:
74
  def download_model_from_huggingface(model_name):
75
  url = f"https://huggingface.co/{model_name}/tree/main"
76
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
 
77
  try:
78
  logger.info(f"Descargando el modelo '{model_name}' desde Hugging Face...")
79
  response = requests.get(url, headers=headers)
@@ -87,6 +90,7 @@ def download_model_from_huggingface(model_name):
87
  for file_name in model_files:
88
  file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
89
  file_content = requests.get(file_url).content
 
90
  blob_name = f"{model_name}/{file_name}"
91
  blob = bucket.blob(blob_name)
92
  blob.upload_from_string(file_content)
@@ -117,28 +121,125 @@ async def predict(request: DownloadModelRequest):
117
  logger.info(f"Modelos no encontrados en GCS, descargando '{model_prefix}' desde Hugging Face...")
118
  download_model_from_huggingface(model_prefix)
119
 
120
- model_files_streams = {}
121
- with tempfile.TemporaryDirectory() as temp_dir:
122
- for file in model_files:
123
- if gcs_handler.file_exists(f"{model_prefix}/{file}"):
124
- file_path = os.path.join(temp_dir, file)
125
- with open(file_path, "wb") as f:
126
- gcs_handler.download_file(f"{model_prefix}/{file}").readinto(f)
127
- model_files_streams[file] = file_path
128
-
129
- if not all(key in model_files_streams for key in ["config.json", "tokenizer.json", "pytorch_model.bin"]):
130
- logger.error(f"Faltan archivos necesarios para el modelo '{model_prefix}'.")
131
- raise HTTPException(status_code=500, detail="Required model files missing.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- if request.pipeline_task in ["text-generation", "translation", "summarization"]:
134
- pipe = pipeline(request.pipeline_task, model=model_files_streams["pytorch_model.bin"], tokenizer=model_files_streams["tokenizer.json"])
135
- result = pipe(request.input_text)
136
- logger.info(f"Resultado generado para la tarea '{request.pipeline_task}': {result[0]}")
137
- return {"response": result[0]}
 
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  except Exception as e:
140
- logger.error(f"Error en la predicción: {e}")
141
- raise HTTPException(status_code=500, detail=f"Error en la predicción: {e}")
 
 
 
 
 
 
 
 
 
142
 
143
  if __name__ == "__main__":
144
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
11
  from transformers import pipeline
12
  from dotenv import load_dotenv
13
  import uvicorn
 
14
 
15
+ # Configuración de carga de variables de entorno
16
  load_dotenv()
17
 
18
  API_KEY = os.getenv("API_KEY")
 
20
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
21
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
22
 
23
+ # Configuración del logger
24
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
25
  logger = logging.getLogger(__name__)
26
 
27
+ # Inicializar el cliente de Google Cloud Storage
28
  try:
29
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
30
  storage_client = storage.Client.from_service_account_info(credentials_info)
 
65
  logger.error(f"Archivo '{blob_name}' no encontrado en GCS.")
66
  raise HTTPException(status_code=404, detail=f"File '{blob_name}' not found.")
67
  logger.debug(f"Descargando archivo '{blob_name}' de GCS.")
68
+ return blob.open("rb") # Abre el archivo en modo lectura de bytes
69
 
70
  def generate_signed_url(self, blob_name, expiration=3600):
71
  blob = self.bucket.blob(blob_name)
 
76
  def download_model_from_huggingface(model_name):
77
  url = f"https://huggingface.co/{model_name}/tree/main"
78
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
79
+
80
  try:
81
  logger.info(f"Descargando el modelo '{model_name}' desde Hugging Face...")
82
  response = requests.get(url, headers=headers)
 
90
  for file_name in model_files:
91
  file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
92
  file_content = requests.get(file_url).content
93
+ # Subir el archivo directamente desde el contenido
94
  blob_name = f"{model_name}/{file_name}"
95
  blob = bucket.blob(blob_name)
96
  blob.upload_from_string(file_content)
 
121
  logger.info(f"Modelos no encontrados en GCS, descargando '{model_prefix}' desde Hugging Face...")
122
  download_model_from_huggingface(model_prefix)
123
 
124
+ model_files_streams = {file: gcs_handler.download_file(f"{model_prefix}/{file}") for file in model_files if gcs_handler.file_exists(f"{model_prefix}/{file}")}
125
+
126
+ config_stream = model_files_streams.get("config.json")
127
+ tokenizer_stream = model_files_streams.get("tokenizer.json")
128
+ model_stream = model_files_streams.get("pytorch_model.bin")
129
+
130
+ if not config_stream or not tokenizer_stream or not model_stream:
131
+ logger.error(f"Faltan archivos necesarios para el modelo '{model_prefix}'.")
132
+ raise HTTPException(status_code=500, detail="Required model files missing.")
133
+
134
+ # Tareas basadas en texto
135
+ if request.pipeline_task in ["text-generation", "translation", "summarization"]:
136
+ pipe = pipeline(request.pipeline_task, model=model_stream, tokenizer=tokenizer_stream)
137
+ result = pipe(request.input_text)
138
+ logger.info(f"Resultado generado para la tarea '{request.pipeline_task}': {result[0]}")
139
+ return {"response": result[0]}
140
+
141
+ # Tareas de imagen
142
+ elif request.pipeline_task == "image-generation":
143
+ try:
144
+ pipe = pipeline("image-generation", model=model_stream)
145
+ images = pipe(request.input_text)
146
+ image = images[0]
147
+ image_filename = f"{uuid.uuid4().hex}.png"
148
+ image_path = f"images/{image_filename}"
149
+ image.save(image_path)
150
+
151
+ # Subir la imagen generada a GCS
152
+ gcs_handler.upload_file(image_path, open(image_path, "rb"))
153
+ image_url = gcs_handler.generate_signed_url(image_path)
154
+ logger.info(f"Imagen generada y subida correctamente con URL: {image_url}")
155
+ return {"response": {"image_url": image_url}}
156
+ except Exception as e:
157
+ logger.error(f"Error generando la imagen: {e}")
158
+ raise HTTPException(status_code=400, detail="Error generando la imagen.")
159
+
160
+ elif request.pipeline_task == "image-editing":
161
+ try:
162
+ pipe = pipeline("image-editing", model=model_stream)
163
+ edited_images = pipe(request.input_text)
164
+ edited_image = edited_images[0]
165
+ edited_image_filename = f"{uuid.uuid4().hex}_edited.png"
166
+ edited_image.save(edited_image_filename)
167
+
168
+ gcs_handler.upload_file(f"images/{edited_image_filename}", open(edited_image_filename, "rb"))
169
+ edited_image_url = gcs_handler.generate_signed_url(f"images/{edited_image_filename}")
170
+ logger.info(f"Imagen editada y subida correctamente con URL: {edited_image_url}")
171
+ return {"response": {"edited_image_url": edited_image_url}}
172
+ except Exception as e:
173
+ logger.error(f"Error editando la imagen: {e}")
174
+ raise HTTPException(status_code=400, detail="Error editando la imagen.")
175
+
176
+ elif request.pipeline_task == "image-to-image":
177
+ try:
178
+ pipe = pipeline("image-to-image", model=model_stream)
179
+ transformed_images = pipe(request.input_text)
180
+ transformed_image = transformed_images[0]
181
+ transformed_image_filename = f"{uuid.uuid4().hex}_transformed.png"
182
+ transformed_image.save(transformed_image_filename)
183
+
184
+ gcs_handler.upload_file(f"images/{transformed_image_filename}", open(transformed_image_filename, "rb"))
185
+ transformed_image_url = gcs_handler.generate_signed_url(f"images/{transformed_image_filename}")
186
+ logger.info(f"Imagen transformada y subida correctamente con URL: {transformed_image_url}")
187
+ return {"response": {"transformed_image_url": transformed_image_url}}
188
+ except Exception as e:
189
+ logger.error(f"Error transformando la imagen: {e}")
190
+ raise HTTPException(status_code=400, detail="Error transformando la imagen.")
191
+
192
+ # Tarea de generación de modelo 3D (simulada)
193
+ elif request.pipeline_task == "text-to-3d":
194
+ try:
195
+ model_3d_filename = f"{uuid.uuid4().hex}.obj"
196
+ model_3d_path = f"3d-models/{model_3d_filename}"
197
+ with open(model_3d_path, "w") as f:
198
+ f.write("Simulated 3D model data")
199
+
200
+ gcs_handler.upload_file(f"3d-models/{model_3d_filename}", open(model_3d_path, "rb"))
201
+ model_3d_url = gcs_handler.generate_signed_url(f"3d-models/{model_3d_filename}")
202
+ logger.info(f"Modelo 3D generado y subido con URL: {model_3d_url}")
203
+ return {"response": {"model_3d_url": model_3d_url}}
204
+ except Exception as e:
205
+ logger.error(f"Error generando el modelo 3D: {e}")
206
+ raise HTTPException(status_code=400, detail="Error generando el modelo 3D.")
207
 
208
+ except HTTPException as e:
209
+ logger.error(f"HTTPException: {e.detail}")
210
+ raise e
211
+ except Exception as e:
212
+ logger.error(f"Error inesperado: {e}")
213
+ raise HTTPException(status_code=500, detail=f"Error: {e}")
214
 
215
+ # Función para ejecutar en segundo plano la descarga de modelos
216
+ def download_all_models_in_background():
217
+ models_url = "https://huggingface.co/api/models"
218
+ try:
219
+ logger.info("Obteniendo lista de modelos desde Hugging Face...")
220
+ response = requests.get(models_url)
221
+ if response.status_code != 200:
222
+ logger.error("Error al obtener la lista de modelos de Hugging Face.")
223
+ raise HTTPException(status_code=500, detail="Error al obtener la lista de modelos.")
224
+
225
+ models = response.json()
226
+ for model in models:
227
+ model_name = model["id"]
228
+ logger.info(f"Descargando el modelo '{model_name}' desde Hugging Face...")
229
+ download_model_from_huggingface(model_name)
230
  except Exception as e:
231
+ logger.error(f"Error al descargar modelos en segundo plano: {e}")
232
+ raise HTTPException(status_code=500, detail="Error al descargar modelos en segundo plano.")
233
+
234
+ # Iniciar la descarga de modelos en segundo plano
235
+ def run_in_background():
236
+ logger.info("Iniciando la descarga de modelos en segundo plano...")
237
+ threading.Thread(target=download_all_models_in_background, daemon=True).start()
238
+
239
+ @app.on_event("startup")
240
+ async def startup_event():
241
+ run_in_background()
242
 
243
  if __name__ == "__main__":
244
  uvicorn.run(app, host="0.0.0.0", port=7860)
245
+