Leonardo Kamigauti commited on
Commit
c4f01da
·
1 Parent(s): b661c94

Try make api secrets via user prompt

Browse files
Files changed (1) hide show
  1. app.py +43 -72
app.py CHANGED
@@ -4,56 +4,53 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- # from typing import Optional
8
 
9
  from kaggle.api.kaggle_api_extended import KaggleApi
10
  import os
11
 
12
  from Gradio_UI import GradioUI
13
 
14
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
- @tool
16
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
17
- #Keep this format for the description / args / args description but feel free to modify the tool
18
- """A tool that does nothing yet
19
- Args:
20
- arg1: the first argument
21
- arg2: the second argument
22
- """
23
- return "What magic will you build ?"
24
 
25
  @tool
26
- def search_kaggle_datasets(search_term:str, kaggle_username:str = None, kaggle_key:str = None, max_results:int = 10)-> list[dict[str]]:
27
- """Search for datasets on Kaggle based on a search term.
28
  Args:
29
- search_term: The term to search for.
30
- kaggle_username: Your Kaggle username.
31
- kaggle_key: Your Kaggle API key.
32
- max_results: Maximum number of results to return.
33
  """
34
- # Initialize the Kaggle API
 
35
  api = KaggleApi()
36
-
37
- # Authenticate using provided credentials
38
- if kaggle_username and kaggle_key:
39
- # Create a temporary kaggle.json file
40
- kaggle_json_content = f'{{"username":"{kaggle_username}","key":"{kaggle_key}"}}'
41
- kaggle_json_path = os.path.expanduser("~/.kaggle/kaggle.json")
42
- os.makedirs(os.path.dirname(kaggle_json_path), exist_ok=True)
43
- with open(kaggle_json_path, "w") as f:
44
- f.write(kaggle_json_content)
45
- os.chmod(kaggle_json_path, 0o600) # Set permissions to read/write for the owner only
46
- else:
47
- # Use the default kaggle.json file if no credentials are provided
48
- return 'Error in searching Kaggle datasets: No username or key provided.'
49
-
50
  try:
51
  api.authenticate()
52
  except Exception as e:
53
  return f"Error authenticating with Kaggle: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  # Search for datasets
56
- datasets = api.dataset_list(search=search_term)
57
 
58
  # Limit the number of results
59
  datasets = datasets[:max_results]
@@ -61,7 +58,7 @@ def search_kaggle_datasets(search_term:str, kaggle_username:str = None, kaggle_k
61
  # Extract relevant information
62
  results = []
63
  for dataset in datasets:
64
- dataset_info = api.dataset_view(dataset)
65
  results.append({
66
  'title': dataset_info['title'],
67
  'url': f"https://www.kaggle.com/{dataset_info['ref']}",
@@ -69,59 +66,27 @@ def search_kaggle_datasets(search_term:str, kaggle_username:str = None, kaggle_k
69
  'files': dataset_info['files'],
70
  'last_updated': dataset_info['lastUpdated']
71
  })
72
-
73
- # Clean up the temporary kaggle.json file if it was created
74
- if kaggle_username and kaggle_key:
75
- os.remove(kaggle_json_path)
76
-
77
  return results
78
 
79
  @tool
80
  def download_kaggle_dataset(
 
81
  dataset_ref: str,
82
  download_path: str,
83
- kaggle_username: str = None,
84
- kaggle_key: str = None,
85
  unzip: bool = True
86
  ) -> str:
87
  """Download a dataset from Kaggle.
88
  Args:
 
89
  dataset_ref: The reference of the dataset (e.g., "username/dataset-name").
90
  download_path: The directory where the dataset will be downloaded.
91
- kaggle_username: Your Kaggle username (from kaggle.json).
92
- kaggle_key: Your Kaggle API key (from kaggle.json).
93
  unzip: Whether to unzip the dataset after downloading. Default is True.
94
  """
95
- # Initialize the Kaggle API
96
- api = KaggleApi()
97
-
98
- # Authenticate using provided credentials
99
- if kaggle_username and kaggle_key:
100
- # Create a temporary kaggle.json file
101
- kaggle_json_content = f'{{"username":"{kaggle_username}","key":"{kaggle_key}"}}'
102
- kaggle_json_path = os.path.expanduser("~/.kaggle/kaggle.json")
103
- os.makedirs(os.path.dirname(kaggle_json_path), exist_ok=True)
104
- with open(kaggle_json_path, "w") as f:
105
- f.write(kaggle_json_content)
106
- os.chmod(kaggle_json_path, 0o600) # Set permissions to read/write for the owner only
107
- else:
108
- # Use the default kaggle.json file if no credentials are provided
109
- pass
110
-
111
- try:
112
- api.authenticate()
113
- except Exception as e:
114
- return f"Error authenticating with Kaggle: {str(e)}"
115
-
116
  # Ensure the download path exists
117
  os.makedirs(download_path, exist_ok=True)
118
 
119
  # Download the dataset
120
- api.dataset_download_files(dataset_ref, path=download_path, unzip=unzip)
121
-
122
- # Clean up the temporary kaggle.json file if it was created
123
- if kaggle_username and kaggle_key:
124
- os.remove(kaggle_json_path)
125
 
126
  return f"Dataset '{dataset_ref}' downloaded to '{download_path}'."
127
 
@@ -137,7 +102,6 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
137
  custom_role_conversions=None,
138
  )
139
 
140
-
141
  # Import tool from Hub
142
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
143
 
@@ -146,7 +110,12 @@ with open("prompts.yaml", 'r') as stream:
146
 
147
  agent = CodeAgent(
148
  model=model,
149
- tools=[final_answer, search_kaggle_datasets, download_kaggle_dataset], ## add your tools here (don't remove final answer)
 
 
 
 
 
150
  max_steps=6,
151
  verbosity_level=1,
152
  grammar=None,
@@ -154,7 +123,9 @@ agent = CodeAgent(
154
  name=None,
155
  description=None,
156
  prompt_templates=prompt_templates,
157
- additional_authorized_imports=['pandas', 'matplotlib', 'seaborn'],
 
 
158
  )
159
 
160
 
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
 
8
  from kaggle.api.kaggle_api_extended import KaggleApi
9
  import os
10
 
11
  from Gradio_UI import GradioUI
12
 
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 (str): Username for Kaggle.
21
+ key (str): 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()
28
  except Exception as e:
29
  return f"Error authenticating with Kaggle: {str(e)}"
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)
54
 
55
  # Limit the number of results
56
  datasets = datasets[:max_results]
 
58
  # Extract relevant information
59
  results = []
60
  for dataset in datasets:
61
+ dataset_info = kaggle_api.dataset_view(dataset)
62
  results.append({
63
  'title': dataset_info['title'],
64
  'url': f"https://www.kaggle.com/{dataset_info['ref']}",
 
66
  'files': dataset_info['files'],
67
  'last_updated': dataset_info['lastUpdated']
68
  })
 
 
 
 
 
69
  return results
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.
84
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
91
  return f"Dataset '{dataset_ref}' downloaded to '{download_path}'."
92
 
 
102
  custom_role_conversions=None,
103
  )
104
 
 
105
  # Import tool from Hub
106
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
107
 
 
110
 
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],
119
  max_steps=6,
120
  verbosity_level=1,
121
  grammar=None,
 
123
  name=None,
124
  description=None,
125
  prompt_templates=prompt_templates,
126
+ additional_authorized_imports=['pandas',
127
+ 'matplotlib',
128
+ 'seaborn'],
129
  )
130
 
131