Hjgugugjhuhjggg
commited on
Commit
•
823bbba
1
Parent(s):
db17ba5
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
9 |
from io import BytesIO
|
10 |
from dotenv import load_dotenv
|
11 |
import uvicorn
|
|
|
12 |
|
13 |
load_dotenv()
|
14 |
|
@@ -52,17 +53,21 @@ def download_model_from_huggingface(model_name):
|
|
52 |
url = f"https://huggingface.co/{model_name}/tree/main"
|
53 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
54 |
|
55 |
-
# Intentar obtener el árbol de archivos
|
56 |
try:
|
57 |
response = requests.get(url, headers=headers)
|
58 |
if response.status_code == 200:
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
else:
|
67 |
raise HTTPException(status_code=404, detail="Error al acceder al árbol de archivos de Hugging Face.")
|
68 |
except Exception as e:
|
@@ -93,19 +98,33 @@ async def predict(request: DownloadModelRequest):
|
|
93 |
# Asegurar que los archivos esenciales estén presentes
|
94 |
config_stream = model_files_streams.get("config.json")
|
95 |
tokenizer_stream = model_files_streams.get("tokenizer.json")
|
|
|
96 |
|
97 |
-
if not config_stream or not tokenizer_stream:
|
98 |
raise HTTPException(status_code=500, detail="Required model files missing.")
|
99 |
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
return {"response": result}
|
111 |
|
|
|
9 |
from io import BytesIO
|
10 |
from dotenv import load_dotenv
|
11 |
import uvicorn
|
12 |
+
import tempfile
|
13 |
|
14 |
load_dotenv()
|
15 |
|
|
|
53 |
url = f"https://huggingface.co/{model_name}/tree/main"
|
54 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
55 |
|
|
|
56 |
try:
|
57 |
response = requests.get(url, headers=headers)
|
58 |
if response.status_code == 200:
|
59 |
+
# Enlace a los archivos del modelo
|
60 |
+
model_files = [
|
61 |
+
"pytorch_model.bin",
|
62 |
+
"config.json",
|
63 |
+
"tokenizer.json",
|
64 |
+
"model.safetensors",
|
65 |
+
]
|
66 |
+
for file_name in model_files:
|
67 |
+
file_url = f"https://huggingface.co/{model_name}/resolve/main/{file_name}"
|
68 |
+
file_content = requests.get(file_url).content
|
69 |
+
blob_name = f"{model_name}/{file_name}"
|
70 |
+
bucket.blob(blob_name).upload_from_file(BytesIO(file_content))
|
71 |
else:
|
72 |
raise HTTPException(status_code=404, detail="Error al acceder al árbol de archivos de Hugging Face.")
|
73 |
except Exception as e:
|
|
|
98 |
# Asegurar que los archivos esenciales estén presentes
|
99 |
config_stream = model_files_streams.get("config.json")
|
100 |
tokenizer_stream = model_files_streams.get("tokenizer.json")
|
101 |
+
model_stream = model_files_streams.get("pytorch_model.bin")
|
102 |
|
103 |
+
if not config_stream or not tokenizer_stream or not model_stream:
|
104 |
raise HTTPException(status_code=500, detail="Required model files missing.")
|
105 |
|
106 |
+
# Guardar los archivos en directorios temporales
|
107 |
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
108 |
+
config_path = os.path.join(tmp_dir, "config.json")
|
109 |
+
tokenizer_path = os.path.join(tmp_dir, "tokenizer.json")
|
110 |
+
model_path = os.path.join(tmp_dir, "pytorch_model.bin")
|
111 |
+
|
112 |
+
with open(config_path, 'wb') as f:
|
113 |
+
f.write(config_stream.read())
|
114 |
+
with open(tokenizer_path, 'wb') as f:
|
115 |
+
f.write(tokenizer_stream.read())
|
116 |
+
with open(model_path, 'wb') as f:
|
117 |
+
f.write(model_stream.read())
|
118 |
+
|
119 |
+
# Cargar el modelo y el tokenizador desde los archivos temporales
|
120 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
121 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
|
122 |
+
|
123 |
+
# Crear un pipeline para la tarea deseada
|
124 |
+
pipeline_ = pipeline(request.pipeline_task, model=model, tokenizer=tokenizer)
|
125 |
+
|
126 |
+
# Realizar la predicción
|
127 |
+
result = pipeline_(request.input_text)
|
128 |
|
129 |
return {"response": result}
|
130 |
|