Leonardo Kamigauti commited on
Commit
05d46d9
·
1 Parent(s): 9cd249f

Remove Kaggle auth credentials from functions to use hf secrets instead

Browse files
Files changed (1) hide show
  1. app.py +7 -23
app.py CHANGED
@@ -13,15 +13,9 @@ from Gradio_UI import GradioUI
13
  os.environ['KAGGLE_USERNAME'] = ''
14
  os.environ['KAGGLE_KEY'] = ''
15
 
16
- @tool
17
- def auth_kaggle(username:str, key:str) -> KaggleApi:
18
- """Authenticate with Kaggle using the provided username and key.
19
- Args:
20
- username: Username for Kaggle.
21
- key: API key for Kaggle.
22
  """
23
- os.environ['KAGGLE_USERNAME'] = username
24
- os.environ['KAGGLE_KEY'] = key
25
  api = KaggleApi()
26
  try:
27
  api.authenticate()
@@ -30,24 +24,16 @@ def auth_kaggle(username:str, key:str) -> KaggleApi:
30
  return api
31
 
32
  @tool
33
- def clean_kaggle_auth() -> str:
34
- """Remove Kaggle credentials from the environment variables for security purposes.
35
- """
36
- os.environ['KAGGLE_USERNAME'] = ''
37
- os.environ['KAGGLE_KEY'] = ''
38
- return 'Kaggle credentials removed'
39
-
40
- @tool
41
- def search_kaggle_datasets(kaggle_api:KaggleApi,
42
- search_term:str,
43
  max_results:int = 10
44
  ) -> list[dict[str]]:
45
  """Search for datasets on Kaggle based on a search term and return list of datasets metadata.
46
  Args:
47
- kaggle_api: Kaggle API object with authentication.
48
  search_term: The term to search for.
49
  max_results: Maximum number of results to return.
50
  """
 
 
51
 
52
  # Search for datasets
53
  datasets = kaggle_api.dataset_list(search=search_term)
@@ -70,14 +56,12 @@ def search_kaggle_datasets(kaggle_api:KaggleApi,
70
 
71
  @tool
72
  def download_kaggle_dataset(
73
- kaggle_api: KaggleApi,
74
  dataset_ref: str,
75
  download_path: str,
76
  unzip: bool = True
77
  ) -> str:
78
  """Download a dataset from Kaggle.
79
  Args:
80
- kaggle_api: Kaggle API object with authentication.
81
  dataset_ref: The reference of the dataset (e.g., "username/dataset-name").
82
  download_path: The directory where the dataset will be downloaded.
83
  unzip: Whether to unzip the dataset after downloading. Default is True.
@@ -85,6 +69,8 @@ def download_kaggle_dataset(
85
  # Ensure the download path exists
86
  os.makedirs(download_path, exist_ok=True)
87
 
 
 
88
  # Download the dataset
89
  kaggle_api.dataset_download_files(dataset_ref, path=download_path, unzip=unzip)
90
 
@@ -111,8 +97,6 @@ with open("prompts.yaml", 'r') as stream:
111
  agent = CodeAgent(
112
  model=model,
113
  tools=[final_answer,
114
- auth_kaggle,
115
- clean_kaggle_auth,
116
  search_kaggle_datasets,
117
  download_kaggle_dataset,
118
  image_generation_tool],
 
13
  os.environ['KAGGLE_USERNAME'] = ''
14
  os.environ['KAGGLE_KEY'] = ''
15
 
16
+ def auth_kaggle() -> KaggleApi:
17
+ """Authenticate Kaggle and return the API object.
 
 
 
 
18
  """
 
 
19
  api = KaggleApi()
20
  try:
21
  api.authenticate()
 
24
  return api
25
 
26
  @tool
27
+ def search_kaggle_datasets(search_term:str,
 
 
 
 
 
 
 
 
 
28
  max_results:int = 10
29
  ) -> list[dict[str]]:
30
  """Search for datasets on Kaggle based on a search term and return list of datasets metadata.
31
  Args:
 
32
  search_term: The term to search for.
33
  max_results: Maximum number of results to return.
34
  """
35
+
36
+ kaggle_api = auth_kaggle()
37
 
38
  # Search for datasets
39
  datasets = kaggle_api.dataset_list(search=search_term)
 
56
 
57
  @tool
58
  def download_kaggle_dataset(
 
59
  dataset_ref: str,
60
  download_path: str,
61
  unzip: bool = True
62
  ) -> str:
63
  """Download a dataset from Kaggle.
64
  Args:
 
65
  dataset_ref: The reference of the dataset (e.g., "username/dataset-name").
66
  download_path: The directory where the dataset will be downloaded.
67
  unzip: Whether to unzip the dataset after downloading. Default is True.
 
69
  # Ensure the download path exists
70
  os.makedirs(download_path, exist_ok=True)
71
 
72
+ kaggle_api = auth_kaggle()
73
+
74
  # Download the dataset
75
  kaggle_api.dataset_download_files(dataset_ref, path=download_path, unzip=unzip)
76
 
 
97
  agent = CodeAgent(
98
  model=model,
99
  tools=[final_answer,
 
 
100
  search_kaggle_datasets,
101
  download_kaggle_dataset,
102
  image_generation_tool],