Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,334 +1,69 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
from Gradio_UI import GradioUI
|
| 8 |
-
from typing import List, Dict
|
| 9 |
-
from bs4 import BeautifulSoup
|
| 10 |
-
|
| 11 |
-
COURTLISTENER_API_KEY = "INSERT_API_KEY"
|
| 12 |
-
API_BASE = "https://www.courtlistener.com/api/rest/v4/"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@tool
|
| 16 |
-
def search_terms_of_service(company_name: str) -> Dict:
|
| 17 |
-
"""Search for a company's Terms of Service link
|
| 18 |
-
|
| 19 |
-
Args:
|
| 20 |
-
company_name: Name of the organization/company to search for
|
| 21 |
-
|
| 22 |
-
Returns:
|
| 23 |
-
Dictionary containing company name and link to their Terms of Service
|
| 24 |
-
"""
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
# API URL
|
| 28 |
-
api_base = "https://05bf-176-76-227-247.ngrok-free.app" # lokalno ga runnamo
|
| 29 |
-
|
| 30 |
-
# API request naredimo
|
| 31 |
-
url = f"{api_base}/company/{company_name}"
|
| 32 |
-
|
| 33 |
-
print(f"Request URL: {url}")
|
| 34 |
-
|
| 35 |
-
response = requests.get(url)
|
| 36 |
-
|
| 37 |
-
print(f"Response status: {response.status_code}")
|
| 38 |
-
print(f"Response body preview: {response.text[:200]}...")
|
| 39 |
-
|
| 40 |
-
if response.status_code == 404: #HTTP error!
|
| 41 |
-
return {"error": f"Company '{company_name}' not found in the database"}
|
| 42 |
-
|
| 43 |
-
response.raise_for_status()
|
| 44 |
-
|
| 45 |
-
data = response.json()
|
| 46 |
-
|
| 47 |
-
# ali obstaja več linkov?
|
| 48 |
-
if not data.get('secondary_links') or len(data['secondary_links']) == 0:
|
| 49 |
-
return {"error": f"No Terms of Service links found for {company_name}"}
|
| 50 |
-
|
| 51 |
-
# returnamo ime in link, lahko jih več
|
| 52 |
-
result = {
|
| 53 |
-
"company_name": data["name"],
|
| 54 |
-
"tos_link": data["secondary_links"][0], # prvi link
|
| 55 |
-
"all_links": data["secondary_links"] # več linkov
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
return result
|
| 59 |
-
|
| 60 |
-
except requests.exceptions.RequestException as e:
|
| 61 |
-
error_msg = f"API Error: {str(e)}"
|
| 62 |
-
if hasattr(e, 'response') and e.response is not None:
|
| 63 |
-
error_msg += f" (Status code: {e.response.status_code})"
|
| 64 |
-
if hasattr(e.response, 'text'):
|
| 65 |
-
error_msg += f" - {e.response.text[:200]}"
|
| 66 |
-
print(error_msg) # debug, da prizna, če se zmoti
|
| 67 |
-
return {"error": error_msg}
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
error_msg = f"Unexpected error: {str(e)}"
|
| 71 |
-
print(error_msg) # debug, da AI prizna, če se zmoti
|
| 72 |
-
return {"error": error_msg}
|
| 73 |
-
|
| 74 |
-
@tool
|
| 75 |
-
def extract_tos_content(company_name: str) -> Dict:
|
| 76 |
-
"""
|
| 77 |
-
Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository.
|
| 78 |
-
|
| 79 |
-
Args:
|
| 80 |
-
company_name: Name of the organization/company to search for
|
| 81 |
-
|
| 82 |
-
Returns:
|
| 83 |
-
Dictionary containing company name, link, and extracted content
|
| 84 |
-
"""
|
| 85 |
-
|
| 86 |
-
tos_info = search_terms_of_service(company_name)
|
| 87 |
-
|
| 88 |
-
# error
|
| 89 |
-
if "error" in tos_info:
|
| 90 |
-
return tos_info
|
| 91 |
-
|
| 92 |
-
try:
|
| 93 |
-
# link od zgoraj
|
| 94 |
-
tos_link = tos_info["tos_link"]
|
| 95 |
-
|
| 96 |
-
print(f"Accessing TOS link: {tos_link}")
|
| 97 |
-
|
| 98 |
-
# Gitlab repo
|
| 99 |
-
if "code.europa.eu" in tos_link and "/-/blob/" in tos_link:
|
| 100 |
-
# spremeni '/-/blob/' z '/-/raw/'
|
| 101 |
-
raw_url = tos_link.replace("/-/blob/", "/-/raw/")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if "?" in raw_url:
|
| 105 |
-
raw_url = raw_url.split("?")[0]
|
| 106 |
-
|
| 107 |
-
print(f"Accessing raw content URL: {raw_url}")
|
| 108 |
-
|
| 109 |
-
# request za raw data iz GItlabaa
|
| 110 |
-
response = requests.get(raw_url)
|
| 111 |
-
response.raise_for_status()
|
| 112 |
-
|
| 113 |
-
return {
|
| 114 |
-
"company_name": tos_info["company_name"],
|
| 115 |
-
"tos_link": tos_link,
|
| 116 |
-
"raw_url": raw_url,
|
| 117 |
-
"content": response.text, # cel raw data fajl
|
| 118 |
-
"content_type": "markdown" if raw_url.endswith(".md") else "text"
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
response = requests.get(tos_link)
|
| 122 |
-
response.raise_for_status()
|
| 123 |
-
|
| 124 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
pre_element = soup.find('pre')
|
| 129 |
-
if pre_element:
|
| 130 |
-
content = pre_element.get_text()
|
| 131 |
-
else:
|
| 132 |
-
main_content = soup.find('main') or soup.find('article') or soup.find('body')
|
| 133 |
-
if main_content:
|
| 134 |
-
content = main_content.get_text(strip=True)
|
| 135 |
-
|
| 136 |
-
return {
|
| 137 |
-
"company_name": tos_info["company_name"],
|
| 138 |
-
"tos_link": tos_link,
|
| 139 |
-
"content": content,
|
| 140 |
-
"note": "Content extracted from HTML page"
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
except requests.exceptions.RequestException as e:
|
| 144 |
-
error_msg = f"Error accessing TOS link: {str(e)}"
|
| 145 |
-
if hasattr(e, 'response') and e.response is not None:
|
| 146 |
-
error_msg += f" (Status code: {e.response.status_code})"
|
| 147 |
-
if hasattr(e.response, 'text'):
|
| 148 |
-
error_msg += f" - {e.response.text[:200]}"
|
| 149 |
-
print(error_msg)
|
| 150 |
-
return {"error": error_msg}
|
| 151 |
-
|
| 152 |
-
except Exception as e:
|
| 153 |
-
error_msg = f"Unexpected error while processing content: {str(e)}"
|
| 154 |
-
print(error_msg)
|
| 155 |
-
return {"error": error_msg}
|
| 156 |
|
|
|
|
| 157 |
@tool
|
| 158 |
-
def
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
"""Search court cases by keyword and jurisdiction
|
| 162 |
Args:
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
date_range: Tuple of (start_date, end_date) in UTC
|
| 166 |
"""
|
| 167 |
-
|
| 168 |
-
try:
|
| 169 |
-
# API
|
| 170 |
-
api_base = "https://www.courtlistener.com/api/rest/v4/"
|
| 171 |
-
|
| 172 |
-
# zgradimo query
|
| 173 |
-
params = {
|
| 174 |
-
"q": keyword,
|
| 175 |
-
"type": "o", # 'o' opinion oz. mnenje
|
| 176 |
-
"page_size": 5
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
if jurisdiction:
|
| 181 |
-
if jurisdiction.lower() == "scotus":
|
| 182 |
-
params["court"] = "scotus"
|
| 183 |
-
else:
|
| 184 |
-
params["court"] = jurisdiction
|
| 185 |
-
|
| 186 |
-
# Datum in format
|
| 187 |
-
if date_range:
|
| 188 |
-
params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
|
| 189 |
-
params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
|
| 190 |
-
|
| 191 |
-
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 192 |
-
|
| 193 |
-
print(f"Request URL: {api_base}search/")
|
| 194 |
-
print(f"Params: {params}")
|
| 195 |
-
|
| 196 |
-
response = requests.get(f"{api_base}search/", params=params, headers=headers)
|
| 197 |
-
|
| 198 |
-
print(f"Response status: {response.status_code}")
|
| 199 |
-
print(f"Response body preview: {response.text[:200]}...")
|
| 200 |
-
|
| 201 |
-
# Handle HTTP errors
|
| 202 |
-
if response.status_code == 403:
|
| 203 |
-
return "Authentication failed: Please check your API key"
|
| 204 |
-
|
| 205 |
-
response.raise_for_status()
|
| 206 |
-
|
| 207 |
-
data = response.json()
|
| 208 |
-
|
| 209 |
-
if 'results' not in data or len(data['results']) == 0:
|
| 210 |
-
return "No matching cases found in the database"
|
| 211 |
-
|
| 212 |
-
cases = []
|
| 213 |
-
for result in data['results']:
|
| 214 |
-
case_info = {
|
| 215 |
-
"case_name": result.get('caseName', result.get('case_name', 'Unknown')),
|
| 216 |
-
"citation": result.get('citation', 'No citation available'),
|
| 217 |
-
"court": result.get('court_full_name', result.get('court', 'Unknown')),
|
| 218 |
-
"date_filed": result.get('date_filed', 'Unknown date'),
|
| 219 |
-
"summary": result.get('text', result.get('snippet', ''))[:200] + '...' if result.get('text') or result.get('snippet') else "No summary available"
|
| 220 |
-
}
|
| 221 |
-
cases.append(case_info)
|
| 222 |
-
|
| 223 |
-
return cases
|
| 224 |
-
|
| 225 |
-
except requests.exceptions.RequestException as e:
|
| 226 |
-
error_msg = f"API Error: {str(e)}"
|
| 227 |
-
if hasattr(e, 'response') and e.response is not None:
|
| 228 |
-
error_msg += f" (Status code: {e.response.status_code})"
|
| 229 |
-
if hasattr(e.response, 'text'):
|
| 230 |
-
error_msg += f" - {e.response.text[:200]}"
|
| 231 |
-
print(error_msg) # debug
|
| 232 |
-
return error_msg
|
| 233 |
-
|
| 234 |
-
except Exception as e:
|
| 235 |
-
error_msg = f"Unexpected error: {str(e)}"
|
| 236 |
-
print(error_msg) # debug
|
| 237 |
-
return error_msg
|
| 238 |
-
|
| 239 |
|
| 240 |
@tool
|
| 241 |
-
def
|
| 242 |
-
"""
|
| 243 |
Args:
|
| 244 |
-
|
| 245 |
"""
|
| 246 |
try:
|
| 247 |
-
#
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
citation_id = citation_id.split("(")[0].strip()
|
| 253 |
-
|
| 254 |
-
print(f"Looking up citation: {citation_id}")
|
| 255 |
-
|
| 256 |
-
# API
|
| 257 |
-
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 258 |
-
|
| 259 |
-
params = {
|
| 260 |
-
"cite": citation_id,
|
| 261 |
-
"court": "scotus"
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
-
print(f"Request URL: {api_base}opinions/")
|
| 265 |
-
print(f"Params: {params}")
|
| 266 |
-
|
| 267 |
-
# API request, ki ga pošiljamo COurtListnereju
|
| 268 |
-
response = requests.get(f"{api_base}opinions/", params=params, headers=headers)
|
| 269 |
-
|
| 270 |
-
# debugiranje
|
| 271 |
-
print(f"Response status: {response.status_code}")
|
| 272 |
-
print(f"Response body preview: {response.text[:200]}...")
|
| 273 |
-
|
| 274 |
-
response.raise_for_status()
|
| 275 |
-
|
| 276 |
-
# JSON
|
| 277 |
-
data = response.json()
|
| 278 |
-
|
| 279 |
-
if 'results' in data and len(data['results']) > 0:
|
| 280 |
-
result = data['results'][0]
|
| 281 |
-
return {
|
| 282 |
-
"status": result.get('status', 'Unknown'),
|
| 283 |
-
"citation": result.get('citation', citation_id),
|
| 284 |
-
"court": result.get('court_full_name', 'Unknown'),
|
| 285 |
-
"precedential_status": result.get('precedential_status', 'Unknown'),
|
| 286 |
-
"date_filed": result.get('date_filed', 'Unknown'),
|
| 287 |
-
"case_name": result.get('case_name', 'Unknown')
|
| 288 |
-
}
|
| 289 |
-
else:
|
| 290 |
-
return {"error": f"No citation found for {citation_id}"}
|
| 291 |
-
|
| 292 |
-
except requests.exceptions.RequestException as e:
|
| 293 |
-
error_msg = f"API Error: {str(e)}"
|
| 294 |
-
if hasattr(e, 'response') and e.response is not None:
|
| 295 |
-
error_msg += f" (Status code: {e.response.status_code})"
|
| 296 |
-
if hasattr(e.response, 'text'):
|
| 297 |
-
error_msg += f" - {e.response.text[:200]}"
|
| 298 |
-
print(error_msg) # debugging
|
| 299 |
-
return {"error": error_msg}
|
| 300 |
-
|
| 301 |
except Exception as e:
|
| 302 |
-
|
| 303 |
-
print(error_msg) # debugging
|
| 304 |
-
return {"error": error_msg}
|
| 305 |
|
| 306 |
|
| 307 |
final_answer = FinalAnswerTool()
|
| 308 |
|
| 309 |
-
#
|
|
|
|
|
|
|
| 310 |
model = HfApiModel(
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
)
|
| 316 |
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
| 318 |
with open("prompts.yaml", 'r') as stream:
|
| 319 |
prompt_templates = yaml.safe_load(stream)
|
| 320 |
-
|
| 321 |
-
#param za agenta
|
| 322 |
agent = CodeAgent(
|
| 323 |
model=model,
|
| 324 |
-
tools=[final_answer
|
| 325 |
max_steps=6,
|
| 326 |
verbosity_level=1,
|
| 327 |
grammar=None,
|
| 328 |
planning_interval=None,
|
| 329 |
-
name=
|
| 330 |
-
description=
|
| 331 |
prompt_templates=prompt_templates
|
| 332 |
)
|
| 333 |
|
| 334 |
-
|
|
|
|
|
|
| 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()
|