Spaces:
Sleeping
Sleeping
Hjgugugjhuhjggg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,9 @@ import json
|
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
6 |
-
from huggingface_hub import hf_hub_download
|
7 |
import boto3
|
8 |
import logging
|
9 |
-
import
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
logger.setLevel(logging.INFO)
|
@@ -21,8 +20,6 @@ AWS_REGION = os.getenv("AWS_REGION")
|
|
21 |
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
|
22 |
HUGGINGFACE_HUB_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
23 |
|
24 |
-
MAX_TOKENS = 1024
|
25 |
-
|
26 |
s3_client = boto3.client(
|
27 |
's3',
|
28 |
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
@@ -47,31 +44,33 @@ class S3DirectStream:
|
|
47 |
)
|
48 |
self.bucket_name = bucket_name
|
49 |
|
50 |
-
async def
|
51 |
-
loop = asyncio.get_event_loop()
|
52 |
-
return await loop.run_in_executor(None, self._stream_from_s3, key)
|
53 |
-
|
54 |
-
def _stream_from_s3(self, key):
|
55 |
try:
|
56 |
-
|
57 |
-
return response['Body'].read()
|
58 |
-
except self.s3_client.exceptions.NoSuchKey:
|
59 |
-
raise HTTPException(status_code=404, detail=f"El archivo {key} no existe en el bucket S3.")
|
60 |
-
except Exception as e:
|
61 |
-
raise HTTPException(status_code=500, detail=f"Error al descargar {key} desde S3: {str(e)}")
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
def _get_model_file_parts(self, model_name):
|
68 |
-
try:
|
69 |
-
model_name = model_name.replace("/", "-").lower()
|
70 |
-
files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=model_name)
|
71 |
-
model_files = [obj['Key'] for obj in files.get('Contents', []) if model_name in obj['Key']]
|
72 |
-
return model_files
|
73 |
except Exception as e:
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
async def load_model_from_s3(self, model_name):
|
77 |
try:
|
@@ -81,97 +80,53 @@ class S3DirectStream:
|
|
81 |
if not model_files:
|
82 |
await self.download_and_upload_to_s3(model_name)
|
83 |
|
|
|
84 |
config_data = await self.stream_from_s3(f"{model_name}/config.json")
|
85 |
-
if not config_data:
|
86 |
-
raise HTTPException(status_code=500, detail=f"El archivo de configuraci贸n {model_name}/config.json est谩 vac铆o o no se pudo leer.")
|
87 |
-
|
88 |
if isinstance(config_data, bytes):
|
89 |
config_data = config_data.decode("utf-8")
|
90 |
|
91 |
config_json = json.loads(config_data)
|
92 |
|
93 |
-
model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_name}", config=config_json
|
94 |
return model
|
95 |
|
96 |
except HTTPException as e:
|
97 |
raise e
|
98 |
except Exception as e:
|
99 |
-
|
|
|
100 |
|
101 |
async def load_tokenizer_from_s3(self, model_name):
|
102 |
try:
|
103 |
model_name = model_name.replace("/", "-").lower()
|
104 |
tokenizer_data = await self.stream_from_s3(f"{model_name}/tokenizer.json")
|
105 |
-
|
106 |
if isinstance(tokenizer_data, bytes):
|
107 |
tokenizer_data = tokenizer_data.decode("utf-8")
|
108 |
|
109 |
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
|
110 |
return tokenizer
|
111 |
except Exception as e:
|
112 |
-
|
|
|
113 |
|
114 |
-
async def
|
115 |
try:
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
logger.info(f"Creando carpeta en S3: {folder_key}")
|
121 |
-
self.s3_client.put_object(Bucket=self.bucket_name, Key=folder_key, Body='')
|
122 |
-
|
123 |
except Exception as e:
|
124 |
-
raise HTTPException(status_code=500, detail=f"Error al
|
125 |
-
|
126 |
-
async def file_exists_in_s3(self, s3_key):
|
127 |
-
try:
|
128 |
-
self.s3_client.head_object(Bucket=self.bucket_name, Key=s3_key)
|
129 |
-
return True
|
130 |
-
except self.s3_client.exceptions.ClientError:
|
131 |
-
return False
|
132 |
|
133 |
-
async def
|
134 |
try:
|
135 |
-
if force_download:
|
136 |
-
logger.info(f"Forzando la descarga del modelo {model_name} y la carga a S3.")
|
137 |
-
|
138 |
model_name = model_name.replace("/", "-").lower()
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
tokenizer_file = hf_hub_download(repo_id=model_name, filename="tokenizer.json", token=HUGGINGFACE_HUB_TOKEN, force_download=force_download)
|
143 |
-
|
144 |
-
await self.create_s3_folders(f"{model_name}/")
|
145 |
-
|
146 |
-
if not await self.file_exists_in_s3(f"{model_name}/config.json"):
|
147 |
-
with open(config_file, "rb") as file:
|
148 |
-
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/config.json", Body=file)
|
149 |
-
|
150 |
-
if not await self.file_exists_in_s3(f"{model_name}/tokenizer.json"):
|
151 |
-
with open(tokenizer_file, "rb") as file:
|
152 |
-
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/tokenizer.json", Body=file)
|
153 |
-
else:
|
154 |
-
logger.info(f"Los archivos del modelo {model_name} ya existen en S3. No es necesario descargarlos de nuevo.")
|
155 |
-
|
156 |
-
except Exception as e:
|
157 |
-
raise HTTPException(status_code=500, detail=f"Error al descargar o cargar archivos desde Hugging Face a S3: {e}")
|
158 |
-
|
159 |
-
async def resume_download(self, model_name):
|
160 |
-
try:
|
161 |
-
logger.info(f"Reanudando la descarga del modelo {model_name} desde Hugging Face.")
|
162 |
-
config_file = hf_hub_download(repo_id=model_name, filename="config.json", token=HUGGINGFACE_HUB_TOKEN, resume_download=True)
|
163 |
-
tokenizer_file = hf_hub_download(repo_id=model_name, filename="tokenizer.json", token=HUGGINGFACE_HUB_TOKEN, resume_download=True)
|
164 |
-
|
165 |
-
if not await self.file_exists_in_s3(f"{model_name}/config.json"):
|
166 |
-
with open(config_file, "rb") as file:
|
167 |
-
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/config.json", Body=file)
|
168 |
-
|
169 |
-
if not await self.file_exists_in_s3(f"{model_name}/tokenizer.json"):
|
170 |
-
with open(tokenizer_file, "rb") as file:
|
171 |
-
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/tokenizer.json", Body=file)
|
172 |
-
|
173 |
except Exception as e:
|
174 |
-
raise HTTPException(status_code=500, detail=f"Error al
|
175 |
|
176 |
@app.post("/generate")
|
177 |
async def generate(request: GenerateRequest):
|
@@ -187,7 +142,7 @@ async def generate(request: GenerateRequest):
|
|
187 |
|
188 |
if task_type == "text-to-text":
|
189 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
190 |
-
result = generator(input_text, max_length=
|
191 |
return {"result": result[0]["generated_text"]}
|
192 |
|
193 |
elif task_type == "text-to-image":
|
|
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
6 |
import boto3
|
7 |
import logging
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
logger = logging.getLogger(__name__)
|
11 |
logger.setLevel(logging.INFO)
|
|
|
20 |
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
|
21 |
HUGGINGFACE_HUB_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
22 |
|
|
|
|
|
23 |
s3_client = boto3.client(
|
24 |
's3',
|
25 |
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
|
|
44 |
)
|
45 |
self.bucket_name = bucket_name
|
46 |
|
47 |
+
async def download_and_upload_to_s3(self, model_name):
|
|
|
|
|
|
|
|
|
48 |
try:
|
49 |
+
model_name = model_name.replace("/", "-").lower()
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Descarga de los archivos desde Hugging Face
|
52 |
+
config_file = hf_hub_download(repo_id=model_name, filename="config.json", token=HUGGINGFACE_HUB_TOKEN)
|
53 |
+
tokenizer_file = hf_hub_download(repo_id=model_name, filename="tokenizer.json", token=HUGGINGFACE_HUB_TOKEN)
|
54 |
+
|
55 |
+
# Verifica si ya existen en S3
|
56 |
+
if not await self.file_exists_in_s3(f"{model_name}/config.json"):
|
57 |
+
with open(config_file, "rb") as file:
|
58 |
+
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/config.json", Body=file)
|
59 |
+
|
60 |
+
if not await self.file_exists_in_s3(f"{model_name}/tokenizer.json"):
|
61 |
+
with open(tokenizer_file, "rb") as file:
|
62 |
+
self.s3_client.put_object(Bucket=self.bucket_name, Key=f"{model_name}/tokenizer.json", Body=file)
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
except Exception as e:
|
65 |
+
logger.error(f"Error al cargar el modelo desde Hugging Face a S3: {e}")
|
66 |
+
raise HTTPException(status_code=500, detail=f"Error al cargar el modelo: {str(e)}")
|
67 |
+
|
68 |
+
async def file_exists_in_s3(self, s3_key):
|
69 |
+
try:
|
70 |
+
self.s3_client.head_object(Bucket=self.bucket_name, Key=s3_key)
|
71 |
+
return True
|
72 |
+
except self.s3_client.exceptions.ClientError:
|
73 |
+
return False
|
74 |
|
75 |
async def load_model_from_s3(self, model_name):
|
76 |
try:
|
|
|
80 |
if not model_files:
|
81 |
await self.download_and_upload_to_s3(model_name)
|
82 |
|
83 |
+
# Cargar configuraci贸n del modelo
|
84 |
config_data = await self.stream_from_s3(f"{model_name}/config.json")
|
|
|
|
|
|
|
85 |
if isinstance(config_data, bytes):
|
86 |
config_data = config_data.decode("utf-8")
|
87 |
|
88 |
config_json = json.loads(config_data)
|
89 |
|
90 |
+
model = AutoModelForCausalLM.from_pretrained(f"s3://{self.bucket_name}/{model_name}", config=config_json)
|
91 |
return model
|
92 |
|
93 |
except HTTPException as e:
|
94 |
raise e
|
95 |
except Exception as e:
|
96 |
+
logger.error(f"Error al cargar el modelo desde S3: {e}")
|
97 |
+
raise HTTPException(status_code=500, detail=f"Error al cargar el modelo desde S3: {str(e)}")
|
98 |
|
99 |
async def load_tokenizer_from_s3(self, model_name):
|
100 |
try:
|
101 |
model_name = model_name.replace("/", "-").lower()
|
102 |
tokenizer_data = await self.stream_from_s3(f"{model_name}/tokenizer.json")
|
103 |
+
|
104 |
if isinstance(tokenizer_data, bytes):
|
105 |
tokenizer_data = tokenizer_data.decode("utf-8")
|
106 |
|
107 |
tokenizer = AutoTokenizer.from_pretrained(f"s3://{self.bucket_name}/{model_name}")
|
108 |
return tokenizer
|
109 |
except Exception as e:
|
110 |
+
logger.error(f"Error al cargar el tokenizer desde S3: {e}")
|
111 |
+
raise HTTPException(status_code=500, detail=f"Error al cargar el tokenizer desde S3: {str(e)}")
|
112 |
|
113 |
+
async def stream_from_s3(self, key):
|
114 |
try:
|
115 |
+
response = self.s3_client.get_object(Bucket=self.bucket_name, Key=key)
|
116 |
+
return response['Body'].read()
|
117 |
+
except self.s3_client.exceptions.NoSuchKey:
|
118 |
+
raise HTTPException(status_code=404, detail=f"El archivo {key} no existe en el bucket S3.")
|
|
|
|
|
|
|
119 |
except Exception as e:
|
120 |
+
raise HTTPException(status_code=500, detail=f"Error al descargar {key} desde S3: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
async def get_model_file_parts(self, model_name):
|
123 |
try:
|
|
|
|
|
|
|
124 |
model_name = model_name.replace("/", "-").lower()
|
125 |
+
files = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=model_name)
|
126 |
+
model_files = [obj['Key'] for obj in files.get('Contents', []) if model_name in obj['Key']]
|
127 |
+
return model_files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
except Exception as e:
|
129 |
+
raise HTTPException(status_code=500, detail=f"Error al obtener archivos del modelo {model_name} desde S3: {str(e)}")
|
130 |
|
131 |
@app.post("/generate")
|
132 |
async def generate(request: GenerateRequest):
|
|
|
142 |
|
143 |
if task_type == "text-to-text":
|
144 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
145 |
+
result = generator(input_text, max_length=1024, num_return_sequences=1)
|
146 |
return {"result": result[0]["generated_text"]}
|
147 |
|
148 |
elif task_type == "text-to-image":
|