albertoakel commited on
Commit
f38b172
·
1 Parent(s): a832d75

Fix: garante download completo antes da leitura dos dados

Browse files
Files changed (2) hide show
  1. download_data.py +12 -31
  2. load_process.py +0 -1
download_data.py CHANGED
@@ -1,54 +1,35 @@
 
1
  import os
2
  from huggingface_hub import hf_hub_download
3
 
4
- # REPO_ID = "albertoakel/dados_belem"
5
- # SUBDIR = "data/process"
6
-
7
- # FILES = [
8
- # "shape_bairros.gpkg",
9
- # "shape_coleta.gpkg",
10
- # "Pontos_descartes_ML.gpkg",
11
- # "tabela_total_com_DIEs.csv",
12
- # "Bairros_Ncoleta.csv",
13
- # ]
14
-
15
- # def ensure_data():
16
- # paths = {}
17
- # for f in FILES:
18
- # path = hf_hub_download(
19
- # repo_id=REPO_ID,
20
- # filename=f"{SUBDIR}/{f}",
21
- # repo_type="dataset"
22
- # )
23
- # paths[f] = path
24
- # return paths
25
-
26
-
27
-
28
- DATASET_ID = "albertoakel/dados_belem"
29
  SUBDIR = "data/process"
30
 
31
- # diretório temporário (persistente durante o runtime)
32
  BASE_DIR = "/tmp/dados_belem"
33
  os.makedirs(BASE_DIR, exist_ok=True)
34
 
35
-
36
  def get_data_file(filename: str) -> str:
37
  """
38
- Baixa o arquivo do HF Dataset apenas se não existir localmente.
39
- Retorna o caminho local do arquivo.
 
40
  """
41
  local_path = os.path.join(BASE_DIR, filename)
42
 
43
  if not os.path.exists(local_path):
44
  print(f"⬇️ Baixando {filename} do Hugging Face Dataset...")
45
- hf_hub_download(
46
- repo_id=DATASET_ID,
47
  filename=f"{SUBDIR}/{filename}",
48
  repo_type="dataset",
49
  local_dir=BASE_DIR,
50
  local_dir_use_symlinks=False
51
  )
 
 
 
 
 
52
  else:
53
  print(f"✅ Usando cache local: {filename}")
54
 
 
1
+ # download_data.py
2
  import os
3
  from huggingface_hub import hf_hub_download
4
 
5
+ REPO_ID = "albertoakel/dados_belem"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  SUBDIR = "data/process"
7
 
 
8
  BASE_DIR = "/tmp/dados_belem"
9
  os.makedirs(BASE_DIR, exist_ok=True)
10
 
 
11
  def get_data_file(filename: str) -> str:
12
  """
13
+ Garante que o arquivo existe localmente.
14
+ Se não existir, baixa do Hugging Face Dataset.
15
+ Retorna o caminho LOCAL FINAL do arquivo.
16
  """
17
  local_path = os.path.join(BASE_DIR, filename)
18
 
19
  if not os.path.exists(local_path):
20
  print(f"⬇️ Baixando {filename} do Hugging Face Dataset...")
21
+ downloaded_path = hf_hub_download(
22
+ repo_id=REPO_ID,
23
  filename=f"{SUBDIR}/{filename}",
24
  repo_type="dataset",
25
  local_dir=BASE_DIR,
26
  local_dir_use_symlinks=False
27
  )
28
+
29
+ # garante que o caminho retornado é o que vamos usar
30
+ if downloaded_path != local_path:
31
+ os.rename(downloaded_path, local_path)
32
+
33
  else:
34
  print(f"✅ Usando cache local: {filename}")
35
 
load_process.py CHANGED
@@ -4,7 +4,6 @@ import os
4
  import pandas as pd
5
  import geopandas as gpd
6
  from download_data import get_data_file
7
- #modify to freehun
8
 
9
  def load_bairros():
10
  return gpd.read_file(get_data_file("shape_bairros.gpkg"))
 
4
  import pandas as pd
5
  import geopandas as gpd
6
  from download_data import get_data_file
 
7
 
8
  def load_bairros():
9
  return gpd.read_file(get_data_file("shape_bairros.gpkg"))