Hjgugugjhuhjggg commited on
Commit
9ef439e
1 Parent(s): 2058dee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -45
app.py CHANGED
@@ -12,7 +12,6 @@ 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 +19,9 @@ 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)
@@ -65,7 +62,7 @@ class GCSHandler:
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,7 +73,6 @@ class GCSHandler:
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,7 +86,6 @@ def download_model_from_huggingface(model_name):
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)
@@ -131,14 +126,12 @@ async def predict(request: DownloadModelRequest):
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)
@@ -148,7 +141,6 @@ async def predict(request: DownloadModelRequest):
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}")
@@ -189,7 +181,6 @@ async def predict(request: DownloadModelRequest):
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"
@@ -199,46 +190,16 @@ async def predict(request: DownloadModelRequest):
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)
 
12
  from dotenv import load_dotenv
13
  import uvicorn
14
 
 
15
  load_dotenv()
16
 
17
  API_KEY = os.getenv("API_KEY")
 
19
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
20
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
21
 
 
22
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
23
  logger = logging.getLogger(__name__)
24
 
 
25
  try:
26
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
27
  storage_client = storage.Client.from_service_account_info(credentials_info)
 
62
  logger.error(f"Archivo '{blob_name}' no encontrado en GCS.")
63
  raise HTTPException(status_code=404, detail=f"File '{blob_name}' not found.")
64
  logger.debug(f"Descargando archivo '{blob_name}' de GCS.")
65
+ return blob.open("rb")
66
 
67
  def generate_signed_url(self, blob_name, expiration=3600):
68
  blob = self.bucket.blob(blob_name)
 
73
  def download_model_from_huggingface(model_name):
74
  url = f"https://huggingface.co/{model_name}/tree/main"
75
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
 
76
  try:
77
  logger.info(f"Descargando el modelo '{model_name}' desde Hugging Face...")
78
  response = requests.get(url, headers=headers)
 
86
  for file_name in model_files:
87
  file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
88
  file_content = requests.get(file_url).content
 
89
  blob_name = f"{model_name}/{file_name}"
90
  blob = bucket.blob(blob_name)
91
  blob.upload_from_string(file_content)
 
126
  logger.error(f"Faltan archivos necesarios para el modelo '{model_prefix}'.")
127
  raise HTTPException(status_code=500, detail="Required model files missing.")
128
 
 
129
  if request.pipeline_task in ["text-generation", "translation", "summarization"]:
130
  pipe = pipeline(request.pipeline_task, model=model_stream, tokenizer=tokenizer_stream)
131
  result = pipe(request.input_text)
132
  logger.info(f"Resultado generado para la tarea '{request.pipeline_task}': {result[0]}")
133
  return {"response": result[0]}
134
 
 
135
  elif request.pipeline_task == "image-generation":
136
  try:
137
  pipe = pipeline("image-generation", model=model_stream)
 
141
  image_path = f"images/{image_filename}"
142
  image.save(image_path)
143
 
 
144
  gcs_handler.upload_file(image_path, open(image_path, "rb"))
145
  image_url = gcs_handler.generate_signed_url(image_path)
146
  logger.info(f"Imagen generada y subida correctamente con URL: {image_url}")
 
181
  logger.error(f"Error transformando la imagen: {e}")
182
  raise HTTPException(status_code=400, detail="Error transformando la imagen.")
183
 
 
184
  elif request.pipeline_task == "text-to-3d":
185
  try:
186
  model_3d_filename = f"{uuid.uuid4().hex}.obj"
 
190
 
191
  gcs_handler.upload_file(f"3d-models/{model_3d_filename}", open(model_3d_path, "rb"))
192
  model_3d_url = gcs_handler.generate_signed_url(f"3d-models/{model_3d_filename}")
193
+ logger.info(f"Modelo 3D generado y subido correctamente con URL: {model_3d_url}")
194
  return {"response": {"model_3d_url": model_3d_url}}
195
+
196
  except Exception as e:
197
  logger.error(f"Error generando el modelo 3D: {e}")
198
  raise HTTPException(status_code=400, detail="Error generando el modelo 3D.")
 
 
 
 
 
 
 
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  except Exception as e:
201
+ logger.error(f"Error en la predicción: {e}")
202
+ raise HTTPException(status_code=500, detail=f"Error en la predicción: {e}")
 
 
 
 
 
 
 
 
 
203
 
204
  if __name__ == "__main__":
205
+ uvicorn.run(app, host="0.0.0.0", port=7860)