Hjgugugjhuhjggg commited on
Commit
09dbcd2
1 Parent(s): 0b278a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -20,12 +20,19 @@ GCS_BUCKET_NAME = os.getenv("GCS_BUCKET_NAME")
20
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
21
  HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Token de Hugging Face
22
 
 
 
 
 
 
 
23
  # Inicialización del cliente de GCS
24
  try:
 
25
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
26
  storage_client = storage.Client.from_service_account_info(credentials_info)
27
  bucket = storage_client.bucket(GCS_BUCKET_NAME)
28
- except (exceptions.DefaultCredentialsError, json.JSONDecodeError, KeyError) as e:
29
  print(f"Error al cargar credenciales o bucket: {e}")
30
  exit(1)
31
 
@@ -162,4 +169,4 @@ async def predict(request: DownloadModelRequest):
162
 
163
 
164
  if __name__ == "__main__":
165
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
20
  GOOGLE_APPLICATION_CREDENTIALS_JSON = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
21
  HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Token de Hugging Face
22
 
23
+ # Validar nombre del bucket
24
+ def validate_bucket_name(bucket_name):
25
+ if not re.match(r"^[a-z0-9][a-z0-9\-\.]*[a-z0-9]$", bucket_name):
26
+ raise ValueError(f"El nombre del bucket '{bucket_name}' no es válido. Debe comenzar y terminar con una letra o número.")
27
+ return bucket_name
28
+
29
  # Inicialización del cliente de GCS
30
  try:
31
+ GCS_BUCKET_NAME = validate_bucket_name(GCS_BUCKET_NAME) # Validar el nombre del bucket
32
  credentials_info = json.loads(GOOGLE_APPLICATION_CREDENTIALS_JSON)
33
  storage_client = storage.Client.from_service_account_info(credentials_info)
34
  bucket = storage_client.bucket(GCS_BUCKET_NAME)
35
+ except (exceptions.DefaultCredentialsError, json.JSONDecodeError, KeyError, ValueError) as e:
36
  print(f"Error al cargar credenciales o bucket: {e}")
37
  exit(1)
38
 
 
169
 
170
 
171
  if __name__ == "__main__":
172
+ uvicorn.run(app, host="0.0.0.0", port=8000)