Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -77,8 +77,7 @@ class S3DirectStream:
|
|
77 |
def load_model_from_stream(self, model_prefix):
|
78 |
try:
|
79 |
logger.info(f"Loading model {model_prefix}...")
|
80 |
-
if self.file_exists_in_s3(f"{model_prefix}/config.json")
|
81 |
-
any(self.file_exists_in_s3(f"{model_prefix}/{file}") for file in self._get_model_files(model_prefix)):
|
82 |
logger.info(f"Model {model_prefix} found in S3. Loading...")
|
83 |
return self.load_model_from_existing_s3(model_prefix)
|
84 |
|
@@ -97,13 +96,13 @@ class S3DirectStream:
|
|
97 |
config = AutoConfig.from_pretrained(model_prefix, **config_dict)
|
98 |
logger.info(f"Config loaded for {model_prefix}.")
|
99 |
|
100 |
-
model_files = self._get_model_files(model_prefix)
|
101 |
if not model_files:
|
102 |
logger.error(f"No model files found for {model_prefix} in S3")
|
103 |
raise EnvironmentError(f"No model files found for {model_prefix} in S3")
|
104 |
|
105 |
state_dict = {}
|
106 |
-
for model_file in model_files:
|
107 |
model_path = os.path.join(model_prefix, model_file)
|
108 |
logger.info(f"Loading model file: {model_path}")
|
109 |
model_stream = self.stream_from_s3(model_path)
|
@@ -172,24 +171,6 @@ class S3DirectStream:
|
|
172 |
logger.error(f"An unexpected error occurred: {e}")
|
173 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred during model download") from e
|
174 |
|
175 |
-
|
176 |
-
def _get_model_files(self, model_prefix, revision="main"): #This function is not used anymore
|
177 |
-
index_url = f"https://huggingface.co/{model_prefix}/resolve/{revision}/"
|
178 |
-
try:
|
179 |
-
index_response = requests.get(index_url)
|
180 |
-
index_response.raise_for_status()
|
181 |
-
logger.info(f"Hugging Face API Response: Status Code = {index_response.status_code}, Headers = {index_response.headers}")
|
182 |
-
index_content = index_response.text
|
183 |
-
logger.info(f"Index content: {index_content}")
|
184 |
-
model_files = [f for f in index_content.split('\n') if f.endswith(('.bin', '.safetensors'))]
|
185 |
-
return model_files
|
186 |
-
except requests.exceptions.RequestException as e:
|
187 |
-
logger.error(f"Error retrieving model index: {e}")
|
188 |
-
raise HTTPException(status_code=500, detail=f"Error retrieving model files from Hugging Face") from e
|
189 |
-
except (IndexError, ValueError) as e:
|
190 |
-
logger.error(f"Error parsing model file names from Hugging Face: {e}")
|
191 |
-
raise HTTPException(status_code=500, detail=f"Error retrieving model files from Hugging Face") from e
|
192 |
-
|
193 |
def download_and_upload_to_s3_url(self, url, s3_key):
|
194 |
logger.info(f"Downloading from {url}...")
|
195 |
with requests.get(url, stream=True) as response:
|
|
|
77 |
def load_model_from_stream(self, model_prefix):
|
78 |
try:
|
79 |
logger.info(f"Loading model {model_prefix}...")
|
80 |
+
if self.file_exists_in_s3(f"{model_prefix}/config.json"): #Only check for config.json
|
|
|
81 |
logger.info(f"Model {model_prefix} found in S3. Loading...")
|
82 |
return self.load_model_from_existing_s3(model_prefix)
|
83 |
|
|
|
96 |
config = AutoConfig.from_pretrained(model_prefix, **config_dict)
|
97 |
logger.info(f"Config loaded for {model_prefix}.")
|
98 |
|
99 |
+
model_files = self._get_model_files(model_prefix) #This line is not used anymore
|
100 |
if not model_files:
|
101 |
logger.error(f"No model files found for {model_prefix} in S3")
|
102 |
raise EnvironmentError(f"No model files found for {model_prefix} in S3")
|
103 |
|
104 |
state_dict = {}
|
105 |
+
for model_file in model_files: #This loop is not used anymore
|
106 |
model_path = os.path.join(model_prefix, model_file)
|
107 |
logger.info(f"Loading model file: {model_path}")
|
108 |
model_stream = self.stream_from_s3(model_path)
|
|
|
171 |
logger.error(f"An unexpected error occurred: {e}")
|
172 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred during model download") from e
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
def download_and_upload_to_s3_url(self, url, s3_key):
|
175 |
logger.info(f"Downloading from {url}...")
|
176 |
with requests.get(url, stream=True) as response:
|