h8hawk commited on
Commit
8e92f09
·
1 Parent(s): ae7a494

add gitignore, add fuzzy search for matching places

Browse files
Files changed (4) hide show
  1. .gitignore +191 -0
  2. app.py +33 -53
  3. requirements.txt +1 -0
  4. tools/time_tools.py +83 -0
.gitignore ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # PyPI configuration file
171
+ .pypirc
172
+
173
+ # Credentials
174
+ cred.json
175
+ credentials/
176
+ *_credentials.*
177
+ service-account*.json
178
+
179
+ # Database
180
+ *.db
181
+ *.sqlite3
182
+
183
+
184
+ # IDE
185
+ .idea/
186
+ .vscode/
187
+ *.swo
188
+
189
+ # OS
190
+ .DS_Store
191
+ Thumbs.db
app.py CHANGED
@@ -1,69 +1,49 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
- import pytz
5
  import yaml
6
  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
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
 
21
- @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
- Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
- """
27
- try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
- except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
- final_answer = FinalAnswerTool()
 
38
 
39
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
42
- model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
- )
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
-
53
- with open("prompts.yaml", 'r') as stream:
54
- prompt_templates = yaml.safe_load(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,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
- prompt_templates=prompt_templates
66
- )
67
 
68
 
69
- GradioUI(agent).launch()
 
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
 
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
6
+ from tools.time_tools import get_current_time_in_timezone
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+ with open("prompts.yaml", "r") as stream:
11
+ prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
 
 
 
12
 
13
+ # Import tool from Hub
14
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
+ def main():
18
+ final_answer = FinalAnswerTool()
19
 
20
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
21
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
22
 
23
+ model = HfApiModel(
24
+ max_tokens=2096,
25
+ temperature=0.5,
26
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # it is possible that this model may be overloaded
27
+ custom_role_conversions=None,
28
+ )
29
 
30
+ agent = CodeAgent(
31
+ model=model,
32
+ tools=[
33
+ get_current_time_in_timezone,
34
+ final_answer,
35
+ ], ## add your tools here (don't remove final answer)
36
+ max_steps=6,
37
+ verbosity_level=1,
38
+ grammar=None,
39
+ planning_interval=None,
40
+ name=None,
41
+ description=None,
42
+ prompt_templates=prompt_templates,
43
+ )
44
 
45
+ GradioUI(agent).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
+ if __name__ == "__main__":
49
+ main()
requirements.txt CHANGED
@@ -3,3 +3,4 @@ smolagents
3
  requests
4
  duckduckgo_search
5
  pandas
 
 
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ thefuzz
tools/time_tools.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import pytz
3
+ from smolagents import tool
4
+ from thefuzz import fuzz
5
+ from typing import Tuple, Optional
6
+
7
+
8
+ def _format_time(dt: datetime.datetime) -> str:
9
+ """Format a datetime object into a string.
10
+
11
+ Args:
12
+ dt: The datetime object to format
13
+
14
+ Returns:
15
+ A formatted time string
16
+ """
17
+ return dt.strftime("%Y-%m-%d %H:%M:%S")
18
+
19
+
20
+ def _find_matching_timezone(
21
+ timezone: str, min_score: int = 70
22
+ ) -> Optional[Tuple[str, int]]:
23
+ """Find the best matching timezone using fuzzy matching.
24
+
25
+ Args:
26
+ timezone: The timezone string to match
27
+ min_score: Minimum fuzzy matching score to consider a valid match
28
+
29
+ Returns:
30
+ A tuple of (matched_timezone, score) or None if no match found
31
+ """
32
+ best_match = None
33
+ best_score = 0
34
+
35
+ for tz_name in pytz.all_timezones:
36
+ score = fuzz.partial_token_sort_ratio(timezone.lower(), tz_name.lower())
37
+ if score > best_score:
38
+ best_score = score
39
+ best_match = tz_name
40
+
41
+ if best_score >= min_score and best_match is not None:
42
+ return (best_match, best_score)
43
+ return None
44
+
45
+
46
+ def _get_time_in_timezone(timezone_name: str) -> datetime.datetime:
47
+ """Get the current time in the specified timezone.
48
+
49
+ Args:
50
+ timezone_name: A valid timezone name
51
+
52
+ Returns:
53
+ A datetime object with the current time in the specified timezone
54
+ """
55
+ tz = pytz.timezone(timezone_name)
56
+ return datetime.datetime.now(tz)
57
+
58
+
59
+ @tool
60
+ def get_current_time_in_timezone(timezone: str) -> str:
61
+ """A tool that fetches the current local time in a specified timezone.
62
+
63
+ Args:
64
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
65
+ Fuzzy matching is applied to find the closest timezone if exact match fails.
66
+
67
+ Returns:
68
+ A string with the current time in the specified timezone or an error message.
69
+ """
70
+ # First try exact match
71
+ try:
72
+ local_time = _format_time(_get_time_in_timezone(timezone))
73
+ return f"The current local time in {timezone} is: {local_time}"
74
+ except pytz.exceptions.UnknownTimeZoneError:
75
+ # If exact match fails, try fuzzy matching
76
+ match_result = _find_matching_timezone(timezone)
77
+
78
+ if match_result:
79
+ best_match, _ = match_result
80
+ local_time = _format_time(_get_time_in_timezone(best_match))
81
+ return f"The current local time in {best_match} (matched from '{timezone}') is: {local_time}"
82
+ else:
83
+ return f"Could not find a close match for timezone '{timezone}'. Please try a valid timezone name."