Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -67,7 +67,9 @@ class S3DirectStream:
|
|
67 |
|
68 |
def load_model_from_s3(self, model_name):
|
69 |
try:
|
70 |
-
|
|
|
|
|
71 |
model_files = self.get_model_file_parts(model_prefix)
|
72 |
|
73 |
if not model_files:
|
@@ -89,14 +91,22 @@ class S3DirectStream:
|
|
89 |
model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_prefix}", config=config_json, from_tf=False)
|
90 |
return model
|
91 |
|
|
|
|
|
92 |
except Exception as e:
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
def load_tokenizer_from_s3(self, model_name):
|
96 |
try:
|
97 |
-
|
|
|
98 |
tokenizer_data = tokenizer_stream.read().decode("utf-8")
|
99 |
-
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_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}")
|
@@ -107,7 +117,8 @@ class S3DirectStream:
|
|
107 |
|
108 |
for file in tqdm(files_to_download, desc="Subiendo archivos a S3"):
|
109 |
file_name = os.path.basename(file)
|
110 |
-
|
|
|
111 |
if not self.file_exists_in_s3(s3_key):
|
112 |
self.upload_file_to_s3(file, s3_key)
|
113 |
|
@@ -116,12 +127,23 @@ class S3DirectStream:
|
|
116 |
|
117 |
def upload_file_to_s3(self, file_path, s3_key):
|
118 |
try:
|
119 |
-
|
120 |
-
|
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):
|
126 |
try:
|
127 |
self.s3_client.head_object(Bucket=self.bucket_name, Key=s3_key)
|
|
|
67 |
|
68 |
def load_model_from_s3(self, model_name):
|
69 |
try:
|
70 |
+
profile, model = model_name.split("/", 1) if "/" in model_name else ("", model_name)
|
71 |
+
|
72 |
+
model_prefix = f"{profile}/{model}".lower()
|
73 |
model_files = self.get_model_file_parts(model_prefix)
|
74 |
|
75 |
if not model_files:
|
|
|
91 |
model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_prefix}", config=config_json, from_tf=False)
|
92 |
return model
|
93 |
|
94 |
+
except HTTPException as e:
|
95 |
+
raise e
|
96 |
except Exception as e:
|
97 |
+
try:
|
98 |
+
logger.error(f"Error al cargar el modelo desde S3, intentando desde Hugging Face: {e}")
|
99 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
100 |
+
return model
|
101 |
+
except Exception as hf_error:
|
102 |
+
raise HTTPException(status_code=500, detail=f"Error al cargar el modelo desde Hugging Face: {hf_error}")
|
103 |
|
104 |
def load_tokenizer_from_s3(self, model_name):
|
105 |
try:
|
106 |
+
profile, model = model_name.split("/", 1) if "/" in model_name else ("", model_name)
|
107 |
+
tokenizer_stream = self.stream_from_s3(f"{profile}/{model}/tokenizer.json")
|
108 |
tokenizer_data = tokenizer_stream.read().decode("utf-8")
|
109 |
+
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{profile}/{model}")
|
110 |
return tokenizer
|
111 |
except Exception as e:
|
112 |
raise HTTPException(status_code=500, detail=f"Error al cargar el tokenizer desde S3: {e}")
|
|
|
117 |
|
118 |
for file in tqdm(files_to_download, desc="Subiendo archivos a S3"):
|
119 |
file_name = os.path.basename(file)
|
120 |
+
profile, model = model_name.split("/", 1) if "/" in model_name else ("", model_name)
|
121 |
+
s3_key = f"{profile}/{model}/{file_name}"
|
122 |
if not self.file_exists_in_s3(s3_key):
|
123 |
self.upload_file_to_s3(file, s3_key)
|
124 |
|
|
|
127 |
|
128 |
def upload_file_to_s3(self, file_path, s3_key):
|
129 |
try:
|
130 |
+
self.create_s3_folders(s3_key)
|
131 |
+
s3_client.put_object(Bucket=self.bucket_name, Key=s3_key, Body=open(file_path, 'rb'))
|
132 |
os.remove(file_path)
|
133 |
except Exception as e:
|
134 |
raise HTTPException(status_code=500, detail=f"Error al subir archivo a S3: {e}")
|
135 |
|
136 |
+
def create_s3_folders(self, s3_key):
|
137 |
+
try:
|
138 |
+
folder_keys = s3_key.split('/')
|
139 |
+
for i in range(1, len(folder_keys)):
|
140 |
+
folder_key = '/'.join(folder_keys[:i]) + '/'
|
141 |
+
if not self.file_exists_in_s3(folder_key):
|
142 |
+
self.s3_client.put_object(Bucket=self.bucket_name, Key=folder_key, Body='')
|
143 |
+
|
144 |
+
except Exception as e:
|
145 |
+
raise HTTPException(status_code=500, detail=f"Error al crear carpetas en S3: {e}")
|
146 |
+
|
147 |
def file_exists_in_s3(self, s3_key):
|
148 |
try:
|
149 |
self.s3_client.head_object(Bucket=self.bucket_name, Key=s3_key)
|