Hjgugugjhuhjggg commited on
Commit
3e20aa7
1 Parent(s): c8d4027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -57,6 +57,8 @@ def download_model_from_huggingface(model_name):
57
  ]
58
  for i in range(1, 100):
59
  file_patterns.extend([f"pytorch_model-{i:05}-of-00001", f"model-{i:05}"])
 
 
60
  for filename in file_patterns:
61
  url = f"https://huggingface.co/{model_name}/resolve/main/{filename}"
62
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
@@ -65,6 +67,8 @@ def download_model_from_huggingface(model_name):
65
  if response.status_code == 200:
66
  blob_name = f"{model_name}/{filename}"
67
  bucket.blob(blob_name).upload_from_file(BytesIO(response.content))
 
 
68
  except Exception as e:
69
  raise HTTPException(status_code=500, detail=f"Error downloading {filename} from Hugging Face: {e}")
70
 
@@ -81,18 +85,38 @@ async def predict(request: DownloadModelRequest):
81
  ]
82
  for i in range(1, 100):
83
  model_files.extend([f"pytorch_model-{i:05}-of-00001", f"model-{i:05}"])
84
- if not any(gcs_handler.file_exists(f"{model_prefix}/{file}") for file in model_files):
 
 
 
 
 
85
  download_model_from_huggingface(model_prefix)
 
 
86
  model_files_streams = {file: gcs_handler.download_file(f"{model_prefix}/{file}") for file in model_files if gcs_handler.file_exists(f"{model_prefix}/{file}")}
 
 
87
  config_stream = model_files_streams.get("config.json")
88
  tokenizer_stream = model_files_streams.get("tokenizer.json")
 
89
  if not config_stream or not tokenizer_stream:
90
  raise HTTPException(status_code=500, detail="Required model files missing.")
 
 
91
  model = AutoModelForCausalLM.from_pretrained(config_stream)
92
  tokenizer = AutoTokenizer.from_pretrained(tokenizer_stream)
 
 
93
  pipeline_ = pipeline(request.pipeline_task, model=model, tokenizer=tokenizer)
 
 
94
  result = pipeline_(request.input_text)
 
95
  return {"response": result}
 
 
 
96
  except Exception as e:
97
  raise HTTPException(status_code=500, detail=f"Error: {e}")
98
 
 
57
  ]
58
  for i in range(1, 100):
59
  file_patterns.extend([f"pytorch_model-{i:05}-of-00001", f"model-{i:05}"])
60
+
61
+ # Descargar los archivos del modelo
62
  for filename in file_patterns:
63
  url = f"https://huggingface.co/{model_name}/resolve/main/{filename}"
64
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
 
67
  if response.status_code == 200:
68
  blob_name = f"{model_name}/{filename}"
69
  bucket.blob(blob_name).upload_from_file(BytesIO(response.content))
70
+ else:
71
+ raise HTTPException(status_code=404, detail=f"File {filename} not found on Hugging Face.")
72
  except Exception as e:
73
  raise HTTPException(status_code=500, detail=f"Error downloading {filename} from Hugging Face: {e}")
74
 
 
85
  ]
86
  for i in range(1, 100):
87
  model_files.extend([f"pytorch_model-{i:05}-of-00001", f"model-{i:05}"])
88
+
89
+ # Verificar si los archivos del modelo están en GCS
90
+ model_files_exist = all(gcs_handler.file_exists(f"{model_prefix}/{file}") for file in model_files)
91
+
92
+ if not model_files_exist:
93
+ # Descargar el modelo si no existe
94
  download_model_from_huggingface(model_prefix)
95
+
96
+ # Descargar los archivos necesarios
97
  model_files_streams = {file: gcs_handler.download_file(f"{model_prefix}/{file}") for file in model_files if gcs_handler.file_exists(f"{model_prefix}/{file}")}
98
+
99
+ # Asegurar que los archivos esenciales estén presentes
100
  config_stream = model_files_streams.get("config.json")
101
  tokenizer_stream = model_files_streams.get("tokenizer.json")
102
+
103
  if not config_stream or not tokenizer_stream:
104
  raise HTTPException(status_code=500, detail="Required model files missing.")
105
+
106
+ # Cargar el modelo y el tokenizador
107
  model = AutoModelForCausalLM.from_pretrained(config_stream)
108
  tokenizer = AutoTokenizer.from_pretrained(tokenizer_stream)
109
+
110
+ # Crear un pipeline para la tarea deseada
111
  pipeline_ = pipeline(request.pipeline_task, model=model, tokenizer=tokenizer)
112
+
113
+ # Realizar la predicción
114
  result = pipeline_(request.input_text)
115
+
116
  return {"response": result}
117
+
118
+ except HTTPException as e:
119
+ raise e
120
  except Exception as e:
121
  raise HTTPException(status_code=500, detail=f"Error: {e}")
122