fix agent context
Browse files- .gitignore +93 -5
- .python-version +1 -0
- Gradio_UI.py +7 -64
- README.md +3 -1
- pyproject.toml +14 -0
- requirements.txt +60 -7
- uv.lock +0 -0
.gitignore
CHANGED
|
@@ -1,9 +1,97 @@
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
-
*.
|
| 3 |
-
|
| 4 |
-
*.
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
.venv/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
.gradio/
|
|
|
|
| 1 |
+
# Python
|
| 2 |
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
wheels/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
MANIFEST
|
| 23 |
|
| 24 |
+
# Poetry
|
| 25 |
.venv/
|
| 26 |
+
|
| 27 |
+
# Unit test / coverage reports
|
| 28 |
+
htmlcov/
|
| 29 |
+
.tox/
|
| 30 |
+
.nox/
|
| 31 |
+
.coverage
|
| 32 |
+
.coverage.*
|
| 33 |
+
.cache
|
| 34 |
+
nosetests.xml
|
| 35 |
+
coverage.xml
|
| 36 |
+
*.cover
|
| 37 |
+
*.py,cover
|
| 38 |
+
.hypothesis/
|
| 39 |
+
.pytest_cache/
|
| 40 |
+
pytest-report.xml
|
| 41 |
+
pytest.xml
|
| 42 |
+
|
| 43 |
+
# Jupyter Notebook
|
| 44 |
+
.ipynb_checkpoints
|
| 45 |
+
|
| 46 |
+
# Environments
|
| 47 |
+
.env
|
| 48 |
+
.venv
|
| 49 |
+
env/
|
| 50 |
+
venv/
|
| 51 |
+
ENV/
|
| 52 |
+
env.bak/
|
| 53 |
+
venv.bak/
|
| 54 |
+
|
| 55 |
+
# IDE
|
| 56 |
+
.idea/
|
| 57 |
+
.vscode/
|
| 58 |
+
*.swp
|
| 59 |
+
*.swo
|
| 60 |
+
.DS_Store
|
| 61 |
+
.history/
|
| 62 |
+
|
| 63 |
+
# Logs
|
| 64 |
+
logs/
|
| 65 |
+
*.log
|
| 66 |
+
log.txt
|
| 67 |
+
|
| 68 |
+
# Database
|
| 69 |
+
*.db
|
| 70 |
+
*.sqlite
|
| 71 |
+
*.sqlite3
|
| 72 |
+
|
| 73 |
+
# Distribution / packaging
|
| 74 |
+
.Python
|
| 75 |
+
dist/
|
| 76 |
+
build/
|
| 77 |
+
*.egg-info/
|
| 78 |
+
|
| 79 |
+
# mypy
|
| 80 |
+
.mypy_cache/
|
| 81 |
+
.dmypy.json
|
| 82 |
+
dmypy.json
|
| 83 |
+
|
| 84 |
+
# Pyre type checker
|
| 85 |
+
.pyre/
|
| 86 |
+
|
| 87 |
+
# Local development settings
|
| 88 |
+
local_settings.py
|
| 89 |
+
|
| 90 |
+
# Data files
|
| 91 |
+
*.csv
|
| 92 |
+
*.xls
|
| 93 |
+
*.xlsx
|
| 94 |
+
**/data/*
|
| 95 |
+
!**/data/.gitkeep
|
| 96 |
+
|
| 97 |
.gradio/
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.11
|
Gradio_UI.py
CHANGED
|
@@ -13,12 +13,11 @@
|
|
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
# See the License for the specific language governing permissions and
|
| 15 |
# limitations under the License.
|
| 16 |
-
import mimetypes
|
| 17 |
import os
|
| 18 |
import re
|
| 19 |
-
import shutil
|
| 20 |
from typing import Optional
|
| 21 |
|
|
|
|
| 22 |
from smolagents.agent_types import (
|
| 23 |
AgentAudio,
|
| 24 |
AgentImage,
|
|
@@ -32,7 +31,6 @@ from smolagents.utils import _is_package_available
|
|
| 32 |
|
| 33 |
def pull_messages_from_step(step_log: MemoryStep):
|
| 34 |
"""Extract ChatMessage objects from agent steps with proper nesting"""
|
| 35 |
-
import gradio as gr
|
| 36 |
|
| 37 |
if isinstance(step_log, ActionStep):
|
| 38 |
# Output the step number
|
|
@@ -165,7 +163,6 @@ def stream_to_gradio(
|
|
| 165 |
raise ModuleNotFoundError(
|
| 166 |
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
|
| 167 |
)
|
| 168 |
-
import gradio as gr
|
| 169 |
|
| 170 |
total_input_tokens = 0
|
| 171 |
total_output_tokens = 0
|
|
@@ -225,66 +222,14 @@ class GradioUI:
|
|
| 225 |
os.mkdir(file_upload_folder)
|
| 226 |
|
| 227 |
def interact_with_agent(self, prompt, messages):
|
| 228 |
-
import gradio as gr
|
| 229 |
-
|
| 230 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
messages.append(msg)
|
| 234 |
yield messages
|
| 235 |
-
yield messages
|
| 236 |
-
|
| 237 |
-
def upload_file(
|
| 238 |
-
self,
|
| 239 |
-
file,
|
| 240 |
-
file_uploads_log,
|
| 241 |
-
allowed_file_types=[
|
| 242 |
-
"application/pdf",
|
| 243 |
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 244 |
-
"text/plain",
|
| 245 |
-
],
|
| 246 |
-
):
|
| 247 |
-
"""
|
| 248 |
-
Handle file uploads, default allowed types are .pdf, .docx, and .txt
|
| 249 |
-
"""
|
| 250 |
-
import gradio as gr
|
| 251 |
-
|
| 252 |
-
if file is None:
|
| 253 |
-
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
| 254 |
-
|
| 255 |
-
try:
|
| 256 |
-
mime_type, _ = mimetypes.guess_type(file.name)
|
| 257 |
-
except Exception as e:
|
| 258 |
-
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
| 259 |
-
|
| 260 |
-
if mime_type not in allowed_file_types:
|
| 261 |
-
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
| 262 |
-
|
| 263 |
-
# Sanitize file name
|
| 264 |
-
original_name = os.path.basename(file.name)
|
| 265 |
-
sanitized_name = re.sub(
|
| 266 |
-
r"[^\w\-.]", "_", original_name
|
| 267 |
-
) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
|
| 268 |
-
|
| 269 |
-
type_to_ext = {}
|
| 270 |
-
for ext, t in mimetypes.types_map.items():
|
| 271 |
-
if t not in type_to_ext:
|
| 272 |
-
type_to_ext[t] = ext
|
| 273 |
-
|
| 274 |
-
# Ensure the extension correlates to the mime type
|
| 275 |
-
sanitized_name = sanitized_name.split(".")[:-1]
|
| 276 |
-
sanitized_name.append("" + type_to_ext[mime_type])
|
| 277 |
-
sanitized_name = "".join(sanitized_name)
|
| 278 |
-
|
| 279 |
-
# Save the uploaded file to the specified folder
|
| 280 |
-
file_path = os.path.join(
|
| 281 |
-
self.file_upload_folder, os.path.basename(sanitized_name)
|
| 282 |
-
)
|
| 283 |
-
shutil.copy(file.name, file_path)
|
| 284 |
-
|
| 285 |
-
return gr.Textbox(
|
| 286 |
-
f"File uploaded: {file_path}", visible=True
|
| 287 |
-
), file_uploads_log + [file_path]
|
| 288 |
|
| 289 |
def log_user_message(self, text_input, file_uploads_log):
|
| 290 |
return (
|
|
@@ -298,8 +243,6 @@ class GradioUI:
|
|
| 298 |
)
|
| 299 |
|
| 300 |
def launch(self, **kwargs):
|
| 301 |
-
import gradio as gr
|
| 302 |
-
|
| 303 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 304 |
gr.Markdown("""
|
| 305 |
# 🤏 📰 SmolNews: News and Time AI Agent
|
|
@@ -357,7 +300,7 @@ class GradioUI:
|
|
| 357 |
[stored_messages, text_input],
|
| 358 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 359 |
|
| 360 |
-
demo.launch(debug=True, share=
|
| 361 |
|
| 362 |
|
| 363 |
__all__ = ["stream_to_gradio", "GradioUI"]
|
|
|
|
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
# See the License for the specific language governing permissions and
|
| 15 |
# limitations under the License.
|
|
|
|
| 16 |
import os
|
| 17 |
import re
|
|
|
|
| 18 |
from typing import Optional
|
| 19 |
|
| 20 |
+
import gradio as gr
|
| 21 |
from smolagents.agent_types import (
|
| 22 |
AgentAudio,
|
| 23 |
AgentImage,
|
|
|
|
| 31 |
|
| 32 |
def pull_messages_from_step(step_log: MemoryStep):
|
| 33 |
"""Extract ChatMessage objects from agent steps with proper nesting"""
|
|
|
|
| 34 |
|
| 35 |
if isinstance(step_log, ActionStep):
|
| 36 |
# Output the step number
|
|
|
|
| 163 |
raise ModuleNotFoundError(
|
| 164 |
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
|
| 165 |
)
|
|
|
|
| 166 |
|
| 167 |
total_input_tokens = 0
|
| 168 |
total_output_tokens = 0
|
|
|
|
| 222 |
os.mkdir(file_upload_folder)
|
| 223 |
|
| 224 |
def interact_with_agent(self, prompt, messages):
|
|
|
|
|
|
|
| 225 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 226 |
+
for msg in stream_to_gradio(
|
| 227 |
+
self.agent,
|
| 228 |
+
task=prompt,
|
| 229 |
+
reset_agent_memory=True,
|
| 230 |
+
):
|
| 231 |
messages.append(msg)
|
| 232 |
yield messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
def log_user_message(self, text_input, file_uploads_log):
|
| 235 |
return (
|
|
|
|
| 243 |
)
|
| 244 |
|
| 245 |
def launch(self, **kwargs):
|
|
|
|
|
|
|
| 246 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 247 |
gr.Markdown("""
|
| 248 |
# 🤏 📰 SmolNews: News and Time AI Agent
|
|
|
|
| 300 |
[stored_messages, text_input],
|
| 301 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 302 |
|
| 303 |
+
demo.launch(debug=True, share=False, **kwargs)
|
| 304 |
|
| 305 |
|
| 306 |
__all__ = ["stream_to_gradio", "GradioUI"]
|
README.md
CHANGED
|
@@ -15,7 +15,9 @@ tags:
|
|
| 15 |
- agent-course
|
| 16 |
---
|
| 17 |
|
| 18 |
-
# News and Time Agent
|
|
|
|
|
|
|
| 19 |
|
| 20 |
A Gradio-based web application that combines news fetching and timezone capabilities using the smolagents framework.
|
| 21 |
|
|
|
|
| 15 |
- agent-course
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# SmolNews: News and Time Agent
|
| 19 |
+
|
| 20 |
+
* Top 5 headlines, smol news but with a twist.
|
| 21 |
|
| 22 |
A Gradio-based web application that combines news fetching and timezone capabilities using the smolagents framework.
|
| 23 |
|
pyproject.toml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "smolnews"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.11"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"markdownify>=1.0.0",
|
| 9 |
+
"pandas>=2.2.3",
|
| 10 |
+
"python-dateutil>=2.9.0.post0",
|
| 11 |
+
"pytz>=2025.1",
|
| 12 |
+
"requests>=2.32.3",
|
| 13 |
+
"smolagents[gradio]>=1.9.2",
|
| 14 |
+
]
|
requirements.txt
CHANGED
|
@@ -1,7 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.8.0
|
| 4 |
+
beautifulsoup4==4.13.3
|
| 5 |
+
certifi==2025.1.31
|
| 6 |
+
charset-normalizer==3.4.1
|
| 7 |
+
click==8.1.8
|
| 8 |
+
duckduckgo-search==7.5.0
|
| 9 |
+
fastapi==0.115.11
|
| 10 |
+
ffmpy==0.5.0
|
| 11 |
+
filelock==3.17.0
|
| 12 |
+
fsspec==2025.2.0
|
| 13 |
+
gradio==5.20.0
|
| 14 |
+
gradio-client==1.7.2
|
| 15 |
+
groovy==0.1.2
|
| 16 |
+
h11==0.14.0
|
| 17 |
+
httpcore==1.0.7
|
| 18 |
+
httpx==0.28.1
|
| 19 |
+
huggingface-hub==0.29.1
|
| 20 |
+
idna==3.10
|
| 21 |
+
jinja2==3.1.5
|
| 22 |
+
lxml==5.3.1
|
| 23 |
+
markdown-it-py==3.0.0
|
| 24 |
+
markdownify==1.0.0
|
| 25 |
+
markupsafe==2.1.5
|
| 26 |
+
mdurl==0.1.2
|
| 27 |
+
numpy==2.2.3
|
| 28 |
+
orjson==3.10.15
|
| 29 |
+
packaging==24.2
|
| 30 |
+
pandas==2.2.3
|
| 31 |
+
pillow==11.1.0
|
| 32 |
+
primp==0.14.0
|
| 33 |
+
pydantic==2.10.6
|
| 34 |
+
pydantic-core==2.27.2
|
| 35 |
+
pydub==0.25.1
|
| 36 |
+
pygments==2.19.1
|
| 37 |
+
python-dateutil==2.9.0.post0
|
| 38 |
+
python-dotenv==1.0.1
|
| 39 |
+
python-multipart==0.0.20
|
| 40 |
+
pytz==2025.1
|
| 41 |
+
pyyaml==6.0.2
|
| 42 |
+
requests==2.32.3
|
| 43 |
+
rich==13.9.4
|
| 44 |
+
ruff==0.9.9
|
| 45 |
+
safehttpx==0.1.6
|
| 46 |
+
semantic-version==2.10.0
|
| 47 |
+
shellingham==1.5.4
|
| 48 |
+
six==1.17.0
|
| 49 |
+
smolagents==1.9.2
|
| 50 |
+
sniffio==1.3.1
|
| 51 |
+
soupsieve==2.6
|
| 52 |
+
starlette==0.46.0
|
| 53 |
+
tomlkit==0.13.2
|
| 54 |
+
tqdm==4.67.1
|
| 55 |
+
typer==0.15.2
|
| 56 |
+
typing-extensions==4.12.2
|
| 57 |
+
tzdata==2025.1
|
| 58 |
+
urllib3==2.3.0
|
| 59 |
+
uvicorn==0.34.0
|
| 60 |
+
websockets==15.0
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|