Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from fastapi.responses import JSONResponse
|
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
from tqdm import tqdm
|
10 |
-
import io
|
11 |
|
12 |
logging.basicConfig(level=logging.INFO)
|
13 |
logger = logging.getLogger(__name__)
|
@@ -50,25 +49,20 @@ class S3DirectStream:
|
|
50 |
|
51 |
def stream_from_s3(self, key):
|
52 |
try:
|
53 |
-
logger.info(f"Descargando {key} desde S3...")
|
54 |
response = self.s3_client.get_object(Bucket=self.bucket_name, Key=key)
|
55 |
return response['Body']
|
56 |
except self.s3_client.exceptions.NoSuchKey:
|
57 |
-
logger.error(f"El archivo {key} no existe en el bucket S3.")
|
58 |
raise HTTPException(status_code=404, detail=f"El archivo {key} no existe en el bucket S3.")
|
59 |
except Exception as e:
|
60 |
-
logger.error(f"Error al descargar {key} desde S3: {str(e)}")
|
61 |
raise HTTPException(status_code=500, detail=f"Error al descargar {key} desde S3: {str(e)}")
|
62 |
|
63 |
def get_model_file_parts(self, model_name):
|
64 |
try:
|
65 |
model_prefix = model_name.lower()
|
66 |
-
logger.info(f"Obteniendo archivos para el modelo {model_name} desde S3...")
|
67 |
files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=model_prefix)
|
68 |
model_files = [obj['Key'] for obj in files.get('Contents', []) if model_prefix in obj['Key']]
|
69 |
return model_files
|
70 |
except Exception as e:
|
71 |
-
logger.error(f"Error al obtener archivos del modelo {model_name} desde S3: {e}")
|
72 |
raise HTTPException(status_code=500, detail=f"Error al obtener archivos del modelo {model_name} desde S3: {e}")
|
73 |
|
74 |
def load_model_from_s3(self, model_name):
|
@@ -77,20 +71,16 @@ class S3DirectStream:
|
|
77 |
model_files = self.get_model_file_parts(model_prefix)
|
78 |
|
79 |
if not model_files:
|
80 |
-
logger.info(f"El modelo {model_name} no est谩 en S3, descargando desde Hugging Face...")
|
81 |
self.download_and_upload_from_huggingface(model_name)
|
82 |
model_files = self.get_model_file_parts(model_prefix)
|
83 |
|
84 |
if not model_files:
|
85 |
-
logger.error(f"Archivos del modelo {model_name} no encontrados en S3.")
|
86 |
raise HTTPException(status_code=404, detail=f"Archivos del modelo {model_name} no encontrados en S3.")
|
87 |
|
88 |
-
logger.info(f"Cargando archivos del modelo {model_name}...")
|
89 |
config_stream = self.stream_from_s3(f"{model_prefix}/config.json")
|
90 |
config_data = config_stream.read()
|
91 |
|
92 |
if not config_data:
|
93 |
-
logger.error(f"El archivo de configuraci贸n {model_prefix}/config.json est谩 vac铆o.")
|
94 |
raise HTTPException(status_code=500, detail=f"El archivo de configuraci贸n {model_prefix}/config.json est谩 vac铆o.")
|
95 |
|
96 |
config_text = config_data.decode("utf-8")
|
@@ -100,23 +90,19 @@ class S3DirectStream:
|
|
100 |
return model
|
101 |
|
102 |
except Exception as e:
|
103 |
-
logger.error(f"Error al cargar el modelo desde S3: {e}")
|
104 |
raise HTTPException(status_code=500, detail=f"Error al cargar el modelo desde S3: {e}")
|
105 |
|
106 |
def load_tokenizer_from_s3(self, model_name):
|
107 |
try:
|
108 |
-
logger.info(f"Cargando el tokenizer del modelo {model_name} desde S3...")
|
109 |
tokenizer_stream = self.stream_from_s3(f"{model_name}/tokenizer.json")
|
110 |
tokenizer_data = tokenizer_stream.read().decode("utf-8")
|
111 |
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
|
112 |
return tokenizer
|
113 |
except Exception as e:
|
114 |
-
logger.error(f"Error al cargar el tokenizer desde S3: {e}")
|
115 |
raise HTTPException(status_code=500, detail=f"Error al cargar el tokenizer desde S3: {e}")
|
116 |
|
117 |
def download_and_upload_from_huggingface(self, model_name):
|
118 |
try:
|
119 |
-
logger.info(f"Descargando modelo {model_name} desde Hugging Face...")
|
120 |
files_to_download = hf_hub_download(repo_id=model_name, use_auth_token=HUGGINGFACE_TOKEN, local_dir=model_name)
|
121 |
|
122 |
for file in tqdm(files_to_download, desc="Subiendo archivos a S3"):
|
@@ -126,7 +112,6 @@ class S3DirectStream:
|
|
126 |
self.upload_file_to_s3(file, s3_key)
|
127 |
|
128 |
except Exception as e:
|
129 |
-
logger.error(f"Error al descargar y subir modelo desde Hugging Face: {e}")
|
130 |
raise HTTPException(status_code=500, detail=f"Error al descargar y subir modelo desde Hugging Face: {e}")
|
131 |
|
132 |
def upload_file_to_s3(self, file_path, s3_key):
|
@@ -134,9 +119,7 @@ class S3DirectStream:
|
|
134 |
with open(file_path, 'rb') as data:
|
135 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=s3_key, Body=data)
|
136 |
os.remove(file_path)
|
137 |
-
logger.info(f"Archivo {file_path} subido correctamente a S3 y eliminado localmente.")
|
138 |
except Exception as e:
|
139 |
-
logger.error(f"Error al subir archivo a S3: {e}")
|
140 |
raise HTTPException(status_code=500, detail=f"Error al subir archivo a S3: {e}")
|
141 |
|
142 |
def file_exists_in_s3(self, s3_key):
|
@@ -161,7 +144,6 @@ async def predict(model_request: dict):
|
|
161 |
tokenizer = streamer.load_tokenizer_from_s3(model_name)
|
162 |
|
163 |
if task not in PIPELINE_MAP:
|
164 |
-
logger.error("Pipeline task no soportado")
|
165 |
raise HTTPException(status_code=400, detail="Pipeline task no soportado")
|
166 |
|
167 |
nlp_pipeline = pipeline(PIPELINE_MAP[task], model=model, tokenizer=tokenizer)
|
@@ -174,7 +156,6 @@ async def predict(model_request: dict):
|
|
174 |
return JSONResponse(content={"result": result})
|
175 |
|
176 |
except Exception as e:
|
177 |
-
logger.error(f"Error al realizar la predicci贸n: {e}")
|
178 |
raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
|
179 |
|
180 |
if __name__ == "__main__":
|
|
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
from tqdm import tqdm
|
|
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
logger = logging.getLogger(__name__)
|
|
|
49 |
|
50 |
def stream_from_s3(self, key):
|
51 |
try:
|
|
|
52 |
response = self.s3_client.get_object(Bucket=self.bucket_name, Key=key)
|
53 |
return response['Body']
|
54 |
except self.s3_client.exceptions.NoSuchKey:
|
|
|
55 |
raise HTTPException(status_code=404, detail=f"El archivo {key} no existe en el bucket S3.")
|
56 |
except Exception as e:
|
|
|
57 |
raise HTTPException(status_code=500, detail=f"Error al descargar {key} desde S3: {str(e)}")
|
58 |
|
59 |
def get_model_file_parts(self, model_name):
|
60 |
try:
|
61 |
model_prefix = model_name.lower()
|
|
|
62 |
files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=model_prefix)
|
63 |
model_files = [obj['Key'] for obj in files.get('Contents', []) if model_prefix in obj['Key']]
|
64 |
return model_files
|
65 |
except Exception as e:
|
|
|
66 |
raise HTTPException(status_code=500, detail=f"Error al obtener archivos del modelo {model_name} desde S3: {e}")
|
67 |
|
68 |
def load_model_from_s3(self, model_name):
|
|
|
71 |
model_files = self.get_model_file_parts(model_prefix)
|
72 |
|
73 |
if not model_files:
|
|
|
74 |
self.download_and_upload_from_huggingface(model_name)
|
75 |
model_files = self.get_model_file_parts(model_prefix)
|
76 |
|
77 |
if not model_files:
|
|
|
78 |
raise HTTPException(status_code=404, detail=f"Archivos del modelo {model_name} no encontrados en S3.")
|
79 |
|
|
|
80 |
config_stream = self.stream_from_s3(f"{model_prefix}/config.json")
|
81 |
config_data = config_stream.read()
|
82 |
|
83 |
if not config_data:
|
|
|
84 |
raise HTTPException(status_code=500, detail=f"El archivo de configuraci贸n {model_prefix}/config.json est谩 vac铆o.")
|
85 |
|
86 |
config_text = config_data.decode("utf-8")
|
|
|
90 |
return model
|
91 |
|
92 |
except Exception as e:
|
|
|
93 |
raise HTTPException(status_code=500, detail=f"Error al cargar el modelo desde S3: {e}")
|
94 |
|
95 |
def load_tokenizer_from_s3(self, model_name):
|
96 |
try:
|
|
|
97 |
tokenizer_stream = self.stream_from_s3(f"{model_name}/tokenizer.json")
|
98 |
tokenizer_data = tokenizer_stream.read().decode("utf-8")
|
99 |
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
|
100 |
return tokenizer
|
101 |
except Exception as e:
|
|
|
102 |
raise HTTPException(status_code=500, detail=f"Error al cargar el tokenizer desde S3: {e}")
|
103 |
|
104 |
def download_and_upload_from_huggingface(self, model_name):
|
105 |
try:
|
|
|
106 |
files_to_download = hf_hub_download(repo_id=model_name, use_auth_token=HUGGINGFACE_TOKEN, local_dir=model_name)
|
107 |
|
108 |
for file in tqdm(files_to_download, desc="Subiendo archivos a S3"):
|
|
|
112 |
self.upload_file_to_s3(file, s3_key)
|
113 |
|
114 |
except Exception as e:
|
|
|
115 |
raise HTTPException(status_code=500, detail=f"Error al descargar y subir modelo desde Hugging Face: {e}")
|
116 |
|
117 |
def upload_file_to_s3(self, file_path, s3_key):
|
|
|
119 |
with open(file_path, 'rb') as data:
|
120 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=s3_key, Body=data)
|
121 |
os.remove(file_path)
|
|
|
122 |
except Exception as e:
|
|
|
123 |
raise HTTPException(status_code=500, detail=f"Error al subir archivo a S3: {e}")
|
124 |
|
125 |
def file_exists_in_s3(self, s3_key):
|
|
|
144 |
tokenizer = streamer.load_tokenizer_from_s3(model_name)
|
145 |
|
146 |
if task not in PIPELINE_MAP:
|
|
|
147 |
raise HTTPException(status_code=400, detail="Pipeline task no soportado")
|
148 |
|
149 |
nlp_pipeline = pipeline(PIPELINE_MAP[task], model=model, tokenizer=tokenizer)
|
|
|
156 |
return JSONResponse(content={"result": result})
|
157 |
|
158 |
except Exception as e:
|
|
|
159 |
raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
|
160 |
|
161 |
if __name__ == "__main__":
|