Frog215 commited on
Commit
9f3d041
·
verified ·
1 Parent(s): 03b8038

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -82
app.py CHANGED
@@ -1,62 +1,62 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool, FinalAnswerTool
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:
@@ -65,7 +65,7 @@ def search_terms_of_service(company_name: str) -> Dict:
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
@@ -75,41 +75,41 @@ def search_terms_of_service(company_name: str) -> Dict:
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,
@@ -117,14 +117,14 @@ def extract_tos_content(company_name: str) -> Dict:
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
  content = ""
127
-
128
  pre_element = soup.find('pre')
129
  if pre_element:
130
  content = pre_element.get_text()
@@ -132,14 +132,14 @@ def extract_tos_content(company_name: str) -> Dict:
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:
@@ -148,67 +148,67 @@ def extract_tos_content(company_name: str) -> Dict:
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 search_court_cases(keyword: str, jurisdiction: str = None,
159
  date_range: tuple = None) -> List[Dict]:
160
-
161
  """Search court cases by keyword and jurisdiction
162
  Args:
163
  keyword: Legal concepts or case terms to search
164
  jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
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 = {
@@ -219,9 +219,9 @@ def search_court_cases(keyword: str, jurisdiction: str = None,
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:
@@ -230,7 +230,7 @@ def search_court_cases(keyword: str, jurisdiction: str = None,
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
@@ -246,36 +246,36 @@ def check_citation_status(citation_id: str) -> Dict:
246
  try:
247
  # Use the correct API base URL with v4
248
  api_base = "https://www.courtlistener.com/api/rest/v4/"
249
-
250
  # API vrne z oklepaji mi rabimo brez!
251
  if "(" in citation_id:
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 {
@@ -288,7 +288,7 @@ def check_citation_status(citation_id: str) -> Dict:
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:
@@ -297,37 +297,38 @@ def check_citation_status(citation_id: str) -> Dict:
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
  error_msg = f"Unexpected error: {str(e)}"
303
  print(error_msg) # debugging
304
- return {"error": error_msg}
305
-
306
 
307
- #kateri model in kje se povežemo!
308
  final_answer = FinalAnswerTool()
309
 
310
- model = InferenceClientModel(
 
311
  max_tokens=2096,
312
- temperature=0.5,
313
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
314
  custom_role_conversions=None,
315
  )
316
 
 
317
  with open("prompts.yaml", 'r') as stream:
318
  prompt_templates = yaml.safe_load(stream)
319
-
320
- # We're creating our CodeAgent
321
  agent = CodeAgent(
322
  model=model,
323
- tools=[final_answer,extract_tos_content,search_terms_of_service], # add your tools here (don't remove final_answer)
324
  max_steps=6,
325
  verbosity_level=1,
326
  grammar=None,
327
  planning_interval=None,
328
- name=None,
329
- description=None,
330
  prompt_templates=prompt_templates
331
  )
332
 
333
- GradioUI(agent).launch()
 
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
  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:
 
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
 
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,
 
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
  content = ""
127
+
128
  pre_element = soup.find('pre')
129
  if pre_element:
130
  content = pre_element.get_text()
 
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:
 
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 search_court_cases(keyword: str, jurisdiction: str = None,
159
  date_range: tuple = None) -> List[Dict]:
160
+
161
  """Search court cases by keyword and jurisdiction
162
  Args:
163
  keyword: Legal concepts or case terms to search
164
  jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
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 = {
 
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:
 
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
 
246
  try:
247
  # Use the correct API base URL with v4
248
  api_base = "https://www.courtlistener.com/api/rest/v4/"
249
+
250
  # API vrne z oklepaji mi rabimo brez!
251
  if "(" in citation_id:
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 {
 
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:
 
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
  error_msg = f"Unexpected error: {str(e)}"
303
  print(error_msg) # debugging
304
+ return {"error": error_msg}
305
+
306
 
 
307
  final_answer = FinalAnswerTool()
308
 
309
+ #kateri model in kje se povežemo!
310
+ model = HfApiModel(
311
  max_tokens=2096,
312
+ temperature=0.3,
313
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
314
  custom_role_conversions=None,
315
  )
316
 
317
+ #prompti za agenta
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,search_terms_of_service,extract_tos_content], #search_court_cases, check_citation_status], #orodja
325
  max_steps=6,
326
  verbosity_level=1,
327
  grammar=None,
328
  planning_interval=None,
329
+ name="Legal Research Assistant",
330
+ description="Specializes in US federal and state court decisions analysis",
331
  prompt_templates=prompt_templates
332
  )
333
 
334
+ GradioUI(agent).launch() #launch za UI