Hjgugugjhuhjggg
commited on
Commit
•
23013d1
1
Parent(s):
9ef439e
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ from google.auth import exceptions
|
|
11 |
from transformers import pipeline
|
12 |
from dotenv import load_dotenv
|
13 |
import uvicorn
|
|
|
14 |
|
15 |
load_dotenv()
|
16 |
|
@@ -116,86 +117,24 @@ async def predict(request: DownloadModelRequest):
|
|
116 |
logger.info(f"Modelos no encontrados en GCS, descargando '{model_prefix}' desde Hugging Face...")
|
117 |
download_model_from_huggingface(model_prefix)
|
118 |
|
119 |
-
model_files_streams = {
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
pipe = pipeline("image-generation", model=model_stream)
|
138 |
-
images = pipe(request.input_text)
|
139 |
-
image = images[0]
|
140 |
-
image_filename = f"{uuid.uuid4().hex}.png"
|
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}")
|
147 |
-
return {"response": {"image_url": image_url}}
|
148 |
-
except Exception as e:
|
149 |
-
logger.error(f"Error generando la imagen: {e}")
|
150 |
-
raise HTTPException(status_code=400, detail="Error generando la imagen.")
|
151 |
-
|
152 |
-
elif request.pipeline_task == "image-editing":
|
153 |
-
try:
|
154 |
-
pipe = pipeline("image-editing", model=model_stream)
|
155 |
-
edited_images = pipe(request.input_text)
|
156 |
-
edited_image = edited_images[0]
|
157 |
-
edited_image_filename = f"{uuid.uuid4().hex}_edited.png"
|
158 |
-
edited_image.save(edited_image_filename)
|
159 |
-
|
160 |
-
gcs_handler.upload_file(f"images/{edited_image_filename}", open(edited_image_filename, "rb"))
|
161 |
-
edited_image_url = gcs_handler.generate_signed_url(f"images/{edited_image_filename}")
|
162 |
-
logger.info(f"Imagen editada y subida correctamente con URL: {edited_image_url}")
|
163 |
-
return {"response": {"edited_image_url": edited_image_url}}
|
164 |
-
except Exception as e:
|
165 |
-
logger.error(f"Error editando la imagen: {e}")
|
166 |
-
raise HTTPException(status_code=400, detail="Error editando la imagen.")
|
167 |
-
|
168 |
-
elif request.pipeline_task == "image-to-image":
|
169 |
-
try:
|
170 |
-
pipe = pipeline("image-to-image", model=model_stream)
|
171 |
-
transformed_images = pipe(request.input_text)
|
172 |
-
transformed_image = transformed_images[0]
|
173 |
-
transformed_image_filename = f"{uuid.uuid4().hex}_transformed.png"
|
174 |
-
transformed_image.save(transformed_image_filename)
|
175 |
-
|
176 |
-
gcs_handler.upload_file(f"images/{transformed_image_filename}", open(transformed_image_filename, "rb"))
|
177 |
-
transformed_image_url = gcs_handler.generate_signed_url(f"images/{transformed_image_filename}")
|
178 |
-
logger.info(f"Imagen transformada y subida correctamente con URL: {transformed_image_url}")
|
179 |
-
return {"response": {"transformed_image_url": transformed_image_url}}
|
180 |
-
except Exception as e:
|
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"
|
187 |
-
model_3d_path = f"3d-models/{model_3d_filename}"
|
188 |
-
with open(model_3d_path, "w") as f:
|
189 |
-
f.write("Simulated 3D model data")
|
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}")
|
|
|
11 |
from transformers import pipeline
|
12 |
from dotenv import load_dotenv
|
13 |
import uvicorn
|
14 |
+
import tempfile
|
15 |
|
16 |
load_dotenv()
|
17 |
|
|
|
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}")
|