Hjgugugjhuhjggg commited on
Commit
93c284f
·
verified ·
1 Parent(s): 115afce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -11
app.py CHANGED
@@ -52,14 +52,11 @@ class S3ModelLoader:
52
 
53
  def download_model_from_s3(self, model_name):
54
  try:
55
- logging.info(f"Trying to load {model_name} from S3...")
56
  config = AutoConfig.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
57
  model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_name}", config=config)
58
  tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
59
- logging.info(f"Loaded {model_name} from S3 successfully.")
60
  return model, tokenizer
61
- except Exception as e:
62
- logging.error(f"Error loading {model_name} from S3: {e}")
63
  return None, None
64
 
65
  async def load_model_and_tokenizer(self, model_name):
@@ -73,15 +70,12 @@ class S3ModelLoader:
73
 
74
  async def download_and_save_model_from_huggingface(self, model_name):
75
  try:
76
- logging.info(f"Downloading {model_name} from Hugging Face...")
77
  with tqdm(unit="B", unit_scale=True, desc=f"Downloading {model_name}") as t:
78
  model = AutoModelForCausalLM.from_pretrained(model_name, token=HUGGINGFACE_HUB_TOKEN, _tqdm=t)
79
  tokenizer = AutoTokenizer.from_pretrained(model_name, token=HUGGINGFACE_HUB_TOKEN)
80
- logging.info(f"Downloaded {model_name} successfully.")
81
  self.upload_model_to_s3(model_name, model, tokenizer)
82
  return model, tokenizer
83
  except Exception as e:
84
- logging.error(f"Error downloading model from Hugging Face: {e}")
85
  raise HTTPException(status_code=500, detail=f"Error downloading model from Hugging Face: {e}")
86
 
87
  def upload_model_to_s3(self, model_name, model, tokenizer):
@@ -89,9 +83,7 @@ class S3ModelLoader:
89
  s3_uri = self._get_s3_uri(model_name)
90
  model.save_pretrained(s3_uri)
91
  tokenizer.save_pretrained(s3_uri)
92
- logging.info(f"Saved {model_name} to S3 successfully.")
93
  except Exception as e:
94
- logging.error(f"Error saving {model_name} to S3: {e}")
95
  raise HTTPException(status_code=500, detail=f"Error saving model to S3: {e}")
96
 
97
  app = FastAPI()
@@ -182,7 +174,6 @@ def download_all_models_in_background():
182
  try:
183
  response = requests.get(models_url)
184
  if response.status_code != 200:
185
- logging.error("Error al obtener la lista de modelos de Hugging Face.")
186
  raise HTTPException(status_code=500, detail="Error al obtener la lista de modelos.")
187
 
188
  models = response.json()
@@ -190,7 +181,6 @@ def download_all_models_in_background():
190
  model_name = model["id"]
191
  model_loader.download_and_save_model_from_huggingface(model_name)
192
  except Exception as e:
193
- logging.error(f"Error al descargar modelos en segundo plano: {e}")
194
  raise HTTPException(status_code=500, detail="Error al descargar modelos en segundo plano.")
195
 
196
  def run_in_background():
 
52
 
53
  def download_model_from_s3(self, model_name):
54
  try:
 
55
  config = AutoConfig.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
56
  model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_name}", config=config)
57
  tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
 
58
  return model, tokenizer
59
+ except Exception:
 
60
  return None, None
61
 
62
  async def load_model_and_tokenizer(self, model_name):
 
70
 
71
  async def download_and_save_model_from_huggingface(self, model_name):
72
  try:
 
73
  with tqdm(unit="B", unit_scale=True, desc=f"Downloading {model_name}") as t:
74
  model = AutoModelForCausalLM.from_pretrained(model_name, token=HUGGINGFACE_HUB_TOKEN, _tqdm=t)
75
  tokenizer = AutoTokenizer.from_pretrained(model_name, token=HUGGINGFACE_HUB_TOKEN)
 
76
  self.upload_model_to_s3(model_name, model, tokenizer)
77
  return model, tokenizer
78
  except Exception as e:
 
79
  raise HTTPException(status_code=500, detail=f"Error downloading model from Hugging Face: {e}")
80
 
81
  def upload_model_to_s3(self, model_name, model, tokenizer):
 
83
  s3_uri = self._get_s3_uri(model_name)
84
  model.save_pretrained(s3_uri)
85
  tokenizer.save_pretrained(s3_uri)
 
86
  except Exception as e:
 
87
  raise HTTPException(status_code=500, detail=f"Error saving model to S3: {e}")
88
 
89
  app = FastAPI()
 
174
  try:
175
  response = requests.get(models_url)
176
  if response.status_code != 200:
 
177
  raise HTTPException(status_code=500, detail="Error al obtener la lista de modelos.")
178
 
179
  models = response.json()
 
181
  model_name = model["id"]
182
  model_loader.download_and_save_model_from_huggingface(model_name)
183
  except Exception as e:
 
184
  raise HTTPException(status_code=500, detail="Error al descargar modelos en segundo plano.")
185
 
186
  def run_in_background():