Hjgugugjhuhjggg commited on
Commit
6742879
·
verified ·
1 Parent(s): 3e70746

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -11,6 +11,9 @@ import time
11
  import asyncio
12
  from fastapi.responses import StreamingResponse, Response
13
  import torch
 
 
 
14
 
15
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s")
16
 
@@ -64,7 +67,6 @@ class S3ModelLoader:
64
  model_files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=f"lilmeaty_garca/{model_name}")
65
  if "Contents" not in model_files:
66
  raise FileNotFoundError(f"Model files not found in S3 for {model_name}")
67
-
68
  s3_model_path = f"s3://{self.bucket_name}/lilmeaty_garca/{model_name.replace('/', '-')}"
69
  logging.info(f"Model {model_name} found on S3 at {s3_model_path}")
70
  return s3_model_path
@@ -72,25 +74,14 @@ class S3ModelLoader:
72
  logging.error(f"Error downloading from S3: {e}")
73
  raise HTTPException(status_code=500, detail=f"Error downloading model from S3: {e}")
74
 
75
- async def load_model_and_tokenizer(self, model_name):
76
- try:
77
- s3_model_path = await asyncio.to_thread(self._download_from_s3, model_name)
78
- # Load from S3 directly (no local storage)
79
- config = AutoConfig.from_pretrained(s3_model_path)
80
- tokenizer = AutoTokenizer.from_pretrained(s3_model_path, config=config)
81
- model = AutoModelForCausalLM.from_pretrained(s3_model_path, config=config)
82
-
83
- logging.info(f"Model {model_name} loaded successfully from S3.")
84
- return model, tokenizer
85
- except Exception as e:
86
- logging.exception(f"Error loading model: {e}")
87
- raise HTTPException(status_code=500, detail=f"Error loading model: {e}")
88
-
89
  def download_model_from_huggingface(self, model_name):
90
  try:
91
  logging.info(f"Downloading model {model_name} from Hugging Face...")
92
- model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN, filename=model_name.split("/")[-1])
93
- self.s3_client.upload_file(model_dir, self.bucket_name, f"lilmeaty_garca/{model_name}")
 
 
 
94
  logging.info(f"Model {model_name} saved to S3 successfully.")
95
  except Exception as e:
96
  logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
@@ -102,7 +93,6 @@ class S3ModelLoader:
102
  if response.status_code != 200:
103
  logging.error("Error getting Hugging Face model list.")
104
  raise HTTPException(status_code=500, detail="Error getting model list.")
105
-
106
  models = response.json()
107
  for model in models:
108
  model_name = model["id"]
@@ -118,7 +108,6 @@ class S3ModelLoader:
118
  async def startup_event():
119
  model_loader.run_in_background()
120
 
121
- # Initialize S3 client with boto3
122
  s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_REGION)
123
  model_loader = S3ModelLoader(S3_BUCKET_NAME, s3_client)
124
 
@@ -204,6 +193,21 @@ async def generate(request: Request, body: GenerateRequest):
204
  logging.error(f"Error processing request: {str(e)}")
205
  raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  if __name__ == "__main__":
208
  import uvicorn
209
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
11
  import asyncio
12
  from fastapi.responses import StreamingResponse, Response
13
  import torch
14
+ from io import BytesIO
15
+ import numpy as np
16
+ import soundfile as sf
17
 
18
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s")
19
 
 
67
  model_files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=f"lilmeaty_garca/{model_name}")
68
  if "Contents" not in model_files:
69
  raise FileNotFoundError(f"Model files not found in S3 for {model_name}")
 
70
  s3_model_path = f"s3://{self.bucket_name}/lilmeaty_garca/{model_name.replace('/', '-')}"
71
  logging.info(f"Model {model_name} found on S3 at {s3_model_path}")
72
  return s3_model_path
 
74
  logging.error(f"Error downloading from S3: {e}")
75
  raise HTTPException(status_code=500, detail=f"Error downloading model from S3: {e}")
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def download_model_from_huggingface(self, model_name):
78
  try:
79
  logging.info(f"Downloading model {model_name} from Hugging Face...")
80
+ model_dir = hf_hub_download(model_name, token=HUGGINGFACE_HUB_TOKEN)
81
+ model_files = os.listdir(model_dir)
82
+ for model_file in model_files:
83
+ s3_path = f"lilmeaty_garca/{model_name}/{model_file}"
84
+ self.s3_client.upload_file(os.path.join(model_dir, model_file), self.bucket_name, s3_path)
85
  logging.info(f"Model {model_name} saved to S3 successfully.")
86
  except Exception as e:
87
  logging.error(f"Error downloading model {model_name} from Hugging Face: {e}")
 
93
  if response.status_code != 200:
94
  logging.error("Error getting Hugging Face model list.")
95
  raise HTTPException(status_code=500, detail="Error getting model list.")
 
96
  models = response.json()
97
  for model in models:
98
  model_name = model["id"]
 
108
  async def startup_event():
109
  model_loader.run_in_background()
110
 
 
111
  s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_REGION)
112
  model_loader = S3ModelLoader(S3_BUCKET_NAME, s3_client)
113
 
 
193
  logging.error(f"Error processing request: {str(e)}")
194
  raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
195
 
196
+ def download_model_from_s3_or_hf(model_name):
197
+ try:
198
+ model_dir = model_loader._download_from_s3(model_name)
199
+ return model_dir
200
+ except Exception:
201
+ model_loader.download_model_from_huggingface(model_name)
202
+ return model_loader._download_from_s3(model_name)
203
+
204
+ def ensure_s3_directories(model_name):
205
+ try:
206
+ s3_path = f"lilmeaty_garca/{model_name}"
207
+ s3_client.put_object(Bucket=S3_BUCKET_NAME, Key=s3_path)
208
+ except Exception as e:
209
+ logging.error(f"Error ensuring S3 directories exist for model {model_name}: {e}")
210
+
211
  if __name__ == "__main__":
212
  import uvicorn
213
  uvicorn.run(app, host="0.0.0.0", port=7860)