Spaces:
Sleeping
Sleeping
Added fetch message from newsweb tool
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -8,15 +9,44 @@ from tools.final_answer import FinalAnswerTool
|
|
| 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
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -55,7 +85,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer],
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
+
import re
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
+
|
| 13 |
+
def extract_message_id(url):
|
| 14 |
+
"""Extracts the message ID from a given Oslo Børs Newsweb URL."""
|
| 15 |
+
match = re.search(r"message/(\d+)", url)
|
| 16 |
+
return match.group(1) if match else None
|
| 17 |
+
|
| 18 |
@tool
|
| 19 |
+
def fetch_message_from_newsweb(url: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Extracts the message ID from a Newsweb URL and fetches the corresponding message from the API.
|
| 22 |
+
|
| 23 |
Args:
|
| 24 |
+
url (str): The Newsweb URL containing the message ID.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
str: The title and body of the news message.
|
| 28 |
"""
|
| 29 |
+
message_id = extract_message_id(url)
|
| 30 |
+
if not message_id:
|
| 31 |
+
return "Invalid URL: Could not extract message ID."
|
| 32 |
+
|
| 33 |
+
api_url = f"https://api3.oslo.oslobors.no/v1/newsreader/message?messageId={message_id}"
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
response = requests.get(api_url, timeout=10)
|
| 37 |
+
response.raise_for_status() # Check for HTTP errors
|
| 38 |
+
|
| 39 |
+
data = response.json()
|
| 40 |
+
message = data.get("data", {}).get("message", {})
|
| 41 |
+
|
| 42 |
+
title = message.get("title", "No title found")
|
| 43 |
+
body = message.get("body", "No message body found")
|
| 44 |
+
|
| 45 |
+
return f"**{title}**\n\n{body}"
|
| 46 |
+
|
| 47 |
+
except requests.RequestException as e:
|
| 48 |
+
return f"Error fetching data from API: {e}"
|
| 49 |
+
|
| 50 |
|
| 51 |
@tool
|
| 52 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 85 |
|
| 86 |
agent = CodeAgent(
|
| 87 |
model=model,
|
| 88 |
+
tools=[final_answer], fetch_message_from_newsweb
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|