dobleuber commited on
Commit
7ff0139
·
1 Parent(s): ae7a494

Add random tools

Browse files
Files changed (4) hide show
  1. .env.example +1 -0
  2. .gitignore +176 -0
  3. app.py +118 -1
  4. requirements.txt +1 -0
.env.example ADDED
@@ -0,0 +1 @@
 
 
1
+ HF_TOKEN=
.gitignore ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
175
+
176
+ .gradio/
app.py CHANGED
@@ -7,6 +7,10 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
 
 
 
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
@@ -33,6 +37,109 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -55,7 +162,17 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+ from dotenv import load_dotenv
11
+
12
+ load_dotenv()
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
 
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
+ @tool
41
+ def get_joke() -> str:
42
+ """
43
+ Fetches a random joke from the JokeAPI.
44
+ This function sends a GET request to the JokeAPI to retrieve a random joke.
45
+ It handles both single jokes and two-part jokes (setup and delivery).
46
+ If the request fails or the response does not contain a joke, an error message is returned.
47
+ Returns:
48
+ str: The joke as a string, or an error message if the joke could not be fetched.
49
+ """
50
+ url = "https://v2.jokeapi.dev/joke/Any?type=single"
51
+
52
+ try:
53
+ response = requests.get(url)
54
+ response.raise_for_status()
55
+
56
+ data = response.json()
57
+
58
+ if "joke" in data:
59
+ return data["joke"]
60
+ elif "setup" in data and "delivery" in data:
61
+ return f"{data['setup']} - {data['delivery']}"
62
+ else:
63
+ return "Error: Unable to fetch joke."
64
+
65
+ except requests.exceptions.RequestException as e:
66
+ return f"Error fetching joke: {str(e)}"
67
+
68
+
69
+ @tool
70
+ def get_time_in_timezone(location: str) -> str:
71
+ """
72
+ Fetches the current time for a given location using the World Time API.
73
+ Args:
74
+ location: The location for which to fetch the current time, formatted as 'Region/City'.
75
+ Returns:
76
+ str: A string indicating the current time in the specified location, or an error message if the request fails.
77
+ Raises:
78
+ requests.exceptions.RequestException: If there is an issue with the HTTP request.
79
+ """
80
+ url = f"http://worldtimeapi.org/api/timezone/{location}.json"
81
+
82
+ try:
83
+ response = requests.get(url)
84
+ response.raise_for_status()
85
+
86
+ data = response.json()
87
+ current_time = data["datetime"]
88
+
89
+ return f"The current time in {location} is {current_time}."
90
+
91
+ except requests.exceptions.RequestException as e:
92
+ return f"Error fetching time data: {str(e)}"
93
+
94
+
95
+ @tool
96
+ def get_random_fact() -> str:
97
+ """
98
+ Fetches a random fact from the "uselessfacts.jsph.pl" API.
99
+ Returns:
100
+ str: A string containing the random fact or an error message if the request fails.
101
+ """
102
+ url = "https://uselessfacts.jsph.pl/random.json?language=en"
103
+
104
+ try:
105
+ response = requests.get(url)
106
+ response.raise_for_status()
107
+
108
+ data = response.json()
109
+
110
+ return f"Random Fact: {data['text']}"
111
+
112
+ except requests.exceptions.RequestException as e:
113
+ return f"Error fetching random fact: {str(e)}"
114
+
115
+
116
+ @tool
117
+ def search_wikipedia(query: str) -> str:
118
+ """
119
+ Fetches a summary of a Wikipedia page for a given query.
120
+ Args:
121
+ query: The search term to look up on Wikipedia.
122
+ Returns:
123
+ str: A summary of the Wikipedia page if successful, or an error message if the request fails.
124
+ Raises:
125
+ requests.exceptions.RequestException: If there is an issue with the HTTP request.
126
+ """
127
+ url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
128
+
129
+ try:
130
+ response = requests.get(url)
131
+ response.raise_for_status()
132
+
133
+ data = response.json()
134
+ title = data["title"]
135
+ extract = data["extract"]
136
+
137
+ return f"Summary for {title}: {extract}"
138
+
139
+ except requests.exceptions.RequestException as e:
140
+ return f"Error fetching Wikipedia data: {str(e)}"
141
+
142
+ search_tool = DuckDuckGoSearchTool()
143
 
144
  final_answer = FinalAnswerTool()
145
 
 
162
 
163
  agent = CodeAgent(
164
  model=model,
165
+ tools=[
166
+ final_answer,
167
+ my_custom_tool,
168
+ get_current_time_in_timezone,
169
+ image_generation_tool,
170
+ search_tool,
171
+ get_joke,
172
+ get_time_in_timezone,
173
+ get_random_fact,
174
+ search_wikipedia
175
+ ], ## add your tools here (don't remove final answer)
176
  max_steps=6,
177
  verbosity_level=1,
178
  grammar=None,
requirements.txt CHANGED
@@ -3,3 +3,4 @@ smolagents
3
  requests
4
  duckduckgo_search
5
  pandas
 
 
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ python-dotenv