Hjgugugjhuhjggg commited on
Commit
7150020
verified
1 Parent(s): 93c284f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -3,32 +3,31 @@ import logging
3
  import requests
4
  import threading
5
  from io import BytesIO
6
- from fastapi import FastAPI, HTTPException, Response, Request
7
  from fastapi.responses import StreamingResponse
8
  from pydantic import BaseModel
9
  from transformers import (
10
  AutoConfig,
11
  AutoModelForCausalLM,
12
  AutoTokenizer,
13
- pipeline,
14
  GenerationConfig
15
  )
16
  import boto3
17
- from huggingface_hub import hf_hub_download
18
- import soundfile as sf
19
- import numpy as np
20
  import torch
21
  import uvicorn
22
  from tqdm import tqdm
23
 
 
24
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
25
 
 
26
  AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
27
  AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
28
  AWS_REGION = os.getenv("AWS_REGION")
29
  S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
30
  HUGGINGFACE_HUB_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
31
 
 
32
  class GenerateRequest(BaseModel):
33
  model_name: str
34
  input_text: str
@@ -42,6 +41,10 @@ class GenerateRequest(BaseModel):
42
  num_return_sequences: int = 1
43
  do_sample: bool = True
44
 
 
 
 
 
45
  class S3ModelLoader:
46
  def __init__(self, bucket_name, s3_client):
47
  self.bucket_name = bucket_name
@@ -71,8 +74,8 @@ class S3ModelLoader:
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:
@@ -86,13 +89,12 @@ class S3ModelLoader:
86
  except Exception as e:
87
  raise HTTPException(status_code=500, detail=f"Error saving model to S3: {e}")
88
 
 
89
  app = FastAPI()
90
 
91
- 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)
92
- model_loader = S3ModelLoader(S3_BUCKET_NAME, s3_client)
93
-
94
  @app.post("/generate")
95
- async def generate(request: Request, body: GenerateRequest):
96
  try:
97
  model, tokenizer = await model_loader.load_model_and_tokenizer(body.model_name)
98
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -169,7 +171,8 @@ async def generate(request: Request, body: GenerateRequest):
169
  except Exception as e:
170
  raise HTTPException(status_code=500, detail=str(e))
171
 
172
- def download_all_models_in_background():
 
173
  models_url = "https://huggingface.co/api/models"
174
  try:
175
  response = requests.get(models_url)
@@ -179,16 +182,14 @@ def download_all_models_in_background():
179
  models = response.json()
180
  for model in models:
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():
187
  threading.Thread(target=download_all_models_in_background, daemon=True).start()
188
 
189
- @app.on_event("startup")
190
- async def startup_event():
191
- run_in_background()
192
-
193
  if __name__ == "__main__":
194
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
3
  import requests
4
  import threading
5
  from io import BytesIO
6
+ from fastapi import FastAPI, HTTPException, Response
7
  from fastapi.responses import StreamingResponse
8
  from pydantic import BaseModel
9
  from transformers import (
10
  AutoConfig,
11
  AutoModelForCausalLM,
12
  AutoTokenizer,
 
13
  GenerationConfig
14
  )
15
  import boto3
 
 
 
16
  import torch
17
  import uvicorn
18
  from tqdm import tqdm
19
 
20
+ # Configuraci贸n de logging
21
  logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
22
 
23
+ # Variables de entorno
24
  AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
25
  AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
26
  AWS_REGION = os.getenv("AWS_REGION")
27
  S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
28
  HUGGINGFACE_HUB_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
29
 
30
+ # Clase para la petici贸n de generaci贸n
31
  class GenerateRequest(BaseModel):
32
  model_name: str
33
  input_text: str
 
41
  num_return_sequences: int = 1
42
  do_sample: bool = True
43
 
44
+ class Config:
45
+ protected_namespaces = ()
46
+
47
+ # Clase para cargar modelos desde S3
48
  class S3ModelLoader:
49
  def __init__(self, bucket_name, s3_client):
50
  self.bucket_name = bucket_name
 
74
  async def download_and_save_model_from_huggingface(self, model_name):
75
  try:
76
  with tqdm(unit="B", unit_scale=True, desc=f"Downloading {model_name}") as t:
77
+ model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=HUGGINGFACE_HUB_TOKEN, _tqdm=t)
78
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=HUGGINGFACE_HUB_TOKEN)
79
  self.upload_model_to_s3(model_name, model, tokenizer)
80
  return model, tokenizer
81
  except Exception as e:
 
89
  except Exception as e:
90
  raise HTTPException(status_code=500, detail=f"Error saving model to S3: {e}")
91
 
92
+ # Crear la instancia de FastAPI
93
  app = FastAPI()
94
 
95
+ # Funci贸n de generaci贸n asincr贸nica
 
 
96
  @app.post("/generate")
97
+ async def generate(body: GenerateRequest):
98
  try:
99
  model, tokenizer = await model_loader.load_model_and_tokenizer(body.model_name)
100
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
171
  except Exception as e:
172
  raise HTTPException(status_code=500, detail=str(e))
173
 
174
+ # Descargar todos los modelos en segundo plano
175
+ async def download_all_models_in_background():
176
  models_url = "https://huggingface.co/api/models"
177
  try:
178
  response = requests.get(models_url)
 
182
  models = response.json()
183
  for model in models:
184
  model_name = model["id"]
185
+ await model_loader.download_and_save_model_from_huggingface(model_name)
186
  except Exception as e:
187
  raise HTTPException(status_code=500, detail="Error al descargar modelos en segundo plano.")
188
 
189
+ # Funci贸n que corre en segundo plano para descargar modelos
190
  def run_in_background():
191
  threading.Thread(target=download_all_models_in_background, daemon=True).start()
192
 
193
+ # Si este archivo se ejecuta directamente, inicia el servidor
 
 
 
194
  if __name__ == "__main__":
195
  uvicorn.run(app, host="0.0.0.0", port=7860)