Hjgugugjhuhjggg commited on
Commit
0b77f45
1 Parent(s): d0dc403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -128
app.py CHANGED
@@ -1,18 +1,24 @@
1
  import os
2
  import json
3
- import uuid
4
- import requests
5
- import threading
6
  import logging
 
7
  from fastapi import FastAPI, HTTPException
8
  from pydantic import BaseModel
9
  from google.cloud import storage
10
  from google.auth import exceptions
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,11 +26,10 @@ 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
- # 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)
@@ -50,6 +55,14 @@ class GCSHandler:
50
  logger.debug(f"Comprobando existencia de archivo '{blob_name}': {exists}")
51
  return exists
52
 
 
 
 
 
 
 
 
 
53
  def upload_file(self, blob_name, file_stream):
54
  blob = self.bucket.blob(blob_name)
55
  try:
@@ -59,14 +72,6 @@ class GCSHandler:
59
  logger.error(f"Error subiendo el archivo '{blob_name}' a GCS: {e}")
60
  raise HTTPException(status_code=500, detail=f"Error subiendo archivo '{blob_name}' a GCS")
61
 
62
- def download_file(self, blob_name):
63
- blob = self.bucket.blob(blob_name)
64
- if not blob.exists():
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)
72
  url = blob.generate_signed_url(expiration=expiration)
@@ -86,6 +91,7 @@ def download_model_from_huggingface(model_name):
86
  "config.json",
87
  "tokenizer.json",
88
  "model.safetensors",
 
89
  ]
90
  for file_name in model_files:
91
  file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
@@ -102,109 +108,98 @@ def download_model_from_huggingface(model_name):
102
  logger.error(f"Error descargando archivos de Hugging Face: {e}")
103
  raise HTTPException(status_code=500, detail=f"Error descargando archivos de Hugging Face: {e}")
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  @app.post("/predict/")
106
  async def predict(request: DownloadModelRequest):
107
  logger.info(f"Iniciando predicción para el modelo '{request.model_name}' con tarea '{request.pipeline_task}'...")
108
  try:
109
  gcs_handler = GCSHandler(GCS_BUCKET_NAME)
110
  model_prefix = request.model_name
111
- model_files = [
112
- "pytorch_model.bin",
113
- "config.json",
114
- "tokenizer.json",
115
- "model.safetensors",
116
- ]
117
-
118
- model_files_exist = all(gcs_handler.file_exists(f"{model_prefix}/{file}") for file in model_files)
119
-
120
- if not model_files_exist:
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
@@ -212,29 +207,18 @@ async def predict(request: DownloadModelRequest):
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():
@@ -242,4 +226,3 @@ async def startup_event():
242
 
243
  if __name__ == "__main__":
244
  uvicorn.run(app, host="0.0.0.0", port=7860)
245
-
 
1
  import os
2
  import json
 
 
 
3
  import logging
4
+ import io
5
  from fastapi import FastAPI, HTTPException
6
  from pydantic import BaseModel
7
  from google.cloud import storage
8
  from google.auth import exceptions
9
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
10
  from dotenv import load_dotenv
11
+ import torch
12
+ import safetensors.torch
13
+ import requests
14
+ from diffusers import StableDiffusionPipeline
15
+ from audiocraft.models import AudioLM
16
+ import asyncio
17
+ import threading
18
  import uvicorn
19
+ from transformers import pipeline as tts_pipeline
20
+ import soundfile as sf # Para manejar el audio de salida
21
 
 
22
  load_dotenv()
23
 
24
  API_KEY = os.getenv("API_KEY")
 
26
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
27
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
28
 
 
29
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
30
  logger = logging.getLogger(__name__)
31
 
32
+ # Configuración de GCS
33
  try:
34
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
35
  storage_client = storage.Client.from_service_account_info(credentials_info)
 
55
  logger.debug(f"Comprobando existencia de archivo '{blob_name}': {exists}")
56
  return exists
57
 
58
+ def download_file_as_stream(self, blob_name):
59
+ blob = self.bucket.blob(blob_name)
60
+ if not blob.exists():
61
+ logger.error(f"Archivo '{blob_name}' no encontrado en GCS.")
62
+ raise HTTPException(status_code=404, detail=f"File '{blob_name}' not found.")
63
+ logger.debug(f"Descargando archivo '{blob_name}' de GCS.")
64
+ return blob.open("rb") # Devuelve un stream (modo lectura binaria)
65
+
66
  def upload_file(self, blob_name, file_stream):
67
  blob = self.bucket.blob(blob_name)
68
  try:
 
72
  logger.error(f"Error subiendo el archivo '{blob_name}' a GCS: {e}")
73
  raise HTTPException(status_code=500, detail=f"Error subiendo archivo '{blob_name}' a GCS")
74
 
 
 
 
 
 
 
 
 
75
  def generate_signed_url(self, blob_name, expiration=3600):
76
  blob = self.bucket.blob(blob_name)
77
  url = blob.generate_signed_url(expiration=expiration)
 
91
  "config.json",
92
  "tokenizer.json",
93
  "model.safetensors",
94
+ "pytorch_model.bin"
95
  ]
96
  for file_name in model_files:
97
  file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
 
108
  logger.error(f"Error descargando archivos de Hugging Face: {e}")
109
  raise HTTPException(status_code=500, detail=f"Error descargando archivos de Hugging Face: {e}")
110
 
111
+ def load_model_from_gcs(model_name, gcs_handler):
112
+ model_files = {
113
+ "config": f"{model_name}/config.json",
114
+ "tokenizer": f"{model_name}/tokenizer.json",
115
+ "model_bin": f"{model_name}/pytorch_model.bin",
116
+ "model_safetensors": f"{model_name}/model.safetensors"
117
+ }
118
+
119
+ model_data = {}
120
+ for key, blob_name in model_files.items():
121
+ if not gcs_handler.file_exists(blob_name):
122
+ logger.info(f"{key.capitalize()} no encontrado en GCS, descargando desde Hugging Face...")
123
+ download_model_from_huggingface(model_name)
124
+ model_data[key] = gcs_handler.download_file_as_stream(blob_name)
125
+
126
+ return model_data
127
+
128
+ def load_diffuser_model_from_streams(model_data, model_name):
129
+ model_bin_stream = model_data.get("model_bin")
130
+ model_safetensors_stream = model_data.get("model_safetensors")
131
+
132
+ if model_bin_stream or model_safetensors_stream:
133
+ # Cargar el modelo de difusión desde los streams de GCS
134
+ logger.info(f"Cargando modelo Diffusers para '{model_name}'...")
135
+ pipe = StableDiffusionPipeline.from_pretrained(io.BytesIO(model_bin_stream.read()))
136
+ else:
137
+ raise HTTPException(status_code=404, detail="No se encontró modelo compatible en el bucket.")
138
+
139
+ return pipe
140
+
141
+ def load_audiocraft_model_from_streams(model_data, model_name):
142
+ model_bin_stream = model_data.get("model_bin")
143
+ model_safetensors_stream = model_data.get("model_safetensors")
144
+
145
+ if model_bin_stream or model_safetensors_stream:
146
+ # Cargar el modelo AudioCraft desde los streams de GCS
147
+ logger.info(f"Cargando modelo Audiocraft para '{model_name}'...")
148
+ model = AudioLM.from_pretrained(io.BytesIO(model_bin_stream.read()))
149
+ else:
150
+ raise HTTPException(status_code=404, detail="No se encontró modelo compatible en el bucket.")
151
+
152
+ return model
153
+
154
  @app.post("/predict/")
155
  async def predict(request: DownloadModelRequest):
156
  logger.info(f"Iniciando predicción para el modelo '{request.model_name}' con tarea '{request.pipeline_task}'...")
157
  try:
158
  gcs_handler = GCSHandler(GCS_BUCKET_NAME)
159
  model_prefix = request.model_name
160
+
161
+ model_data = load_model_from_gcs(model_prefix, gcs_handler)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
+ # Cargar los archivos de modelo y tokenizer directamente desde los streams
164
+ config_stream = model_data["config"]
165
+ tokenizer_stream = model_data["tokenizer"]
166
+
167
+ if request.pipeline_task == "text-generation":
168
+ # Usar el modelo HuggingFace normal si es una tarea de texto
169
+ model = load_model_from_streams(model_data, model_prefix)
170
+ tokenizer = AutoTokenizer.from_pretrained(io.BytesIO(tokenizer_stream.read()))
171
+ pipe = pipeline(request.pipeline_task, model=model, tokenizer=tokenizer)
172
  result = pipe(request.input_text)
 
 
 
 
173
  elif request.pipeline_task == "image-generation":
174
+ # Usar el modelo Diffuser si es tarea de generación de imágenes
175
+ pipe = load_diffuser_model_from_streams(model_data, model_prefix)
176
+ result = pipe(request.input_text).images
177
+ elif request.pipeline_task == "audio-generation":
178
+ # Usar el modelo Audiocraft si es tarea de generación de audio
179
+ model = load_audiocraft_model_from_streams(model_data, model_prefix)
180
+ result = model.generate(request.input_text)
181
+ elif request.pipeline_task == "text-to-speech":
182
+ # TTS pipeline utilizando transformers
183
+ tts_pipe = tts_pipeline("text-to-speech", model=model, tokenizer=tokenizer)
184
+ audio_output = tts_pipe(request.input_text)[0]['audio']
185
+ # Se devuelve el archivo de audio
186
+ audio_path = "output.wav"
187
+ sf.write(audio_path, audio_output, 16000) # Guardar el audio en un archivo
188
+ result = audio_path
189
+ elif request.pipeline_task == "text-to-audio":
190
+ # Usar audiocraft o modelo específico para text-to-audio
191
+ model = load_audiocraft_model_from_streams(model_data, model_prefix)
192
+ audio_output = model.generate(request.input_text)
193
+ # Guardar o procesar el audio de salida
194
+ audio_path = "output_audio.wav"
195
+ sf.write(audio_path, audio_output, 16000)
196
+ result = audio_path
197
+ else:
198
+ raise HTTPException(status_code=400, detail="Tarea no soportada.")
199
+
200
+ logger.info(f"Resultado generado para la tarea '{request.pipeline_task}': {result[0]}")
201
+ return {"response": result[0]}
202
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  except HTTPException as e:
204
  logger.error(f"HTTPException: {e.detail}")
205
  raise e
 
207
  logger.error(f"Error inesperado: {e}")
208
  raise HTTPException(status_code=500, detail=f"Error: {e}")
209
 
210
+ def download_model_in_background(model_name):
 
 
211
  try:
212
+ gcs_handler = GCSHandler(GCS_BUCKET_NAME)
213
+ logger.info(f"Iniciando descarga en segundo plano del modelo '{model_name}' a GCS...")
214
+ download_model_from_huggingface(model_name)
215
+ logger.info(f"Descarga del modelo '{model_name}' completada.")
 
 
 
 
 
 
 
216
  except Exception as e:
217
+ logger.error(f"Error al descargar el modelo '{model_name}' en segundo plano: {e}")
 
218
 
 
219
  def run_in_background():
220
  logger.info("Iniciando la descarga de modelos en segundo plano...")
221
+ threading.Thread(target=download_model_in_background, args=("modelo_ejemplo",)).start()
222
 
223
  @app.on_event("startup")
224
  async def startup_event():
 
226
 
227
  if __name__ == "__main__":
228
  uvicorn.run(app, host="0.0.0.0", port=7860)