Update app.py
Browse files
app.py
CHANGED
@@ -47,12 +47,14 @@ class S3DirectStream:
|
|
47 |
config_file = hf_hub_download(repo_id=model_name, filename="config.json", token=HUGGINGFACE_HUB_TOKEN)
|
48 |
tokenizer_file = hf_hub_download(repo_id=model_name, filename="tokenizer.json", token=HUGGINGFACE_HUB_TOKEN)
|
49 |
|
50 |
-
# Verificar si
|
51 |
if not await self.file_exists_in_s3(f"{model_name}/config.json"):
|
|
|
52 |
with open(config_file, "rb") as file:
|
53 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/config.json", Body=file)
|
54 |
|
55 |
if not await self.file_exists_in_s3(f"{model_name}/tokenizer.json"):
|
|
|
56 |
with open(tokenizer_file, "rb") as file:
|
57 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/tokenizer.json", Body=file)
|
58 |
|
@@ -67,6 +69,15 @@ class S3DirectStream:
|
|
67 |
except self.s3_client.exceptions.ClientError:
|
68 |
return False
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
async def load_model_from_s3(self, model_name):
|
71 |
try:
|
72 |
model_name = model_name.replace("/", "-").lower()
|
|
|
47 |
config_file = hf_hub_download(repo_id=model_name, filename="config.json", token=HUGGINGFACE_HUB_TOKEN)
|
48 |
tokenizer_file = hf_hub_download(repo_id=model_name, filename="tokenizer.json", token=HUGGINGFACE_HUB_TOKEN)
|
49 |
|
50 |
+
# Verificar si la carpeta y los archivos ya existen en S3
|
51 |
if not await self.file_exists_in_s3(f"{model_name}/config.json"):
|
52 |
+
self.create_folder_if_not_exists(model_name)
|
53 |
with open(config_file, "rb") as file:
|
54 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/config.json", Body=file)
|
55 |
|
56 |
if not await self.file_exists_in_s3(f"{model_name}/tokenizer.json"):
|
57 |
+
self.create_folder_if_not_exists(model_name)
|
58 |
with open(tokenizer_file, "rb") as file:
|
59 |
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/tokenizer.json", Body=file)
|
60 |
|
|
|
69 |
except self.s3_client.exceptions.ClientError:
|
70 |
return False
|
71 |
|
72 |
+
def create_folder_if_not_exists(self, model_name):
|
73 |
+
try:
|
74 |
+
# Las carpetas no existen como tal en S3, pero se pueden crear archivos vacíos para simular carpetas
|
75 |
+
# Crear un archivo vacío para simular la carpeta
|
76 |
+
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/")
|
77 |
+
except Exception as e:
|
78 |
+
logging.error(f"Error al crear la carpeta en S3: {e}")
|
79 |
+
raise HTTPException(status_code=500, detail=f"Error al crear la carpeta en S3: {str(e)}")
|
80 |
+
|
81 |
async def load_model_from_s3(self, model_name):
|
82 |
try:
|
83 |
model_name = model_name.replace("/", "-").lower()
|