mogg7 commited on
Commit
dbd77dc
·
verified ·
1 Parent(s): 1210ed1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +238 -238
app.py CHANGED
@@ -1,265 +1,265 @@
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
-
12
- @tool
13
- def search_terms_of_service(company_name: str) -> Dict:
14
- """Search for a company's Terms of Service link
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- Args:
17
- company_name: Name of the organization/company to search for
18
 
19
- Returns:
20
- Dictionary containing company name and link to their Terms of Service
21
- """
22
 
23
- try:
24
- # API URL
25
- api_base = "https://05bf-176-76-227-247.ngrok-free.app" # lokalno ga runnamo
26
-
27
- # API request naredimo
28
- url = f"{api_base}/company/{company_name}"
29
-
30
- print(f"Request URL: {url}")
31
-
32
- response = requests.get(url)
33
-
34
- print(f"Response status: {response.status_code}")
35
- print(f"Response body preview: {response.text[:200]}...")
36
-
37
- if response.status_code == 404: #HTTP error!
38
- return {"error": f"Company '{company_name}' not found in the database"}
39
-
40
- response.raise_for_status()
41
-
42
- data = response.json()
43
-
44
- # ali obstaja več linkov?
45
- if not data.get('secondary_links') or len(data['secondary_links']) == 0:
46
- return {"error": f"No Terms of Service links found for {company_name}"}
47
-
48
- # returnamo ime in link, lahko jih več
49
- result = {
50
- "company_name": data["name"],
51
- "tos_link": data["secondary_links"][0], # prvi link
52
- "all_links": data["secondary_links"] # več linkov
53
- }
54
-
55
- return result
56
-
57
- except requests.exceptions.RequestException as e:
58
- error_msg = f"API Error: {str(e)}"
59
- if hasattr(e, 'response') and e.response is not None:
60
- error_msg += f" (Status code: {e.response.status_code})"
61
- if hasattr(e.response, 'text'):
62
- error_msg += f" - {e.response.text[:200]}"
63
- print(error_msg) # debug, da prizna, če se zmoti
64
- return {"error": error_msg}
65
-
66
- except Exception as e:
67
- error_msg = f"Unexpected error: {str(e)}"
68
- print(error_msg) # debug, da AI prizna, če se zmoti
69
- return {"error": error_msg}
70
-
71
- @tool
72
- def extract_tos_content(company_name: str) -> Dict:
73
- """
74
- Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository.
75
 
76
- Args:
77
- company_name: Name of the organization/company to search for
78
 
79
- Returns:
80
- Dictionary containing company name, link, and extracted content
81
- """
82
 
83
- tos_info = search_terms_of_service(company_name)
84
 
85
- # error
86
- if "error" in tos_info:
87
- return tos_info
88
 
89
- try:
90
- # link od zgoraj
91
- tos_link = tos_info["tos_link"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- print(f"Accessing TOS link: {tos_link}")
94
 
95
- # Gitlab repo
96
- if "code.europa.eu" in tos_link and "/-/blob/" in tos_link:
97
- # spremeni '/-/blob/' z '/-/raw/'
98
- raw_url = tos_link.replace("/-/blob/", "/-/raw/")
99
-
100
-
101
- if "?" in raw_url:
102
- raw_url = raw_url.split("?")[0]
103
-
104
- print(f"Accessing raw content URL: {raw_url}")
105
 
106
- # request za raw data iz GItlabaa
107
- response = requests.get(raw_url)
108
- response.raise_for_status()
109
-
110
- return {
111
- "company_name": tos_info["company_name"],
112
- "tos_link": tos_link,
113
- "raw_url": raw_url,
114
- "content": response.text, # cel raw data fajl
115
- "content_type": "markdown" if raw_url.endswith(".md") else "text"
116
- }
117
 
118
- response = requests.get(tos_link)
 
119
  response.raise_for_status()
120
 
121
- soup = BeautifulSoup(response.text, 'html.parser')
122
-
123
- content = ""
124
-
125
- pre_element = soup.find('pre')
126
- if pre_element:
127
- content = pre_element.get_text()
128
- else:
129
- main_content = soup.find('main') or soup.find('article') or soup.find('body')
130
- if main_content:
131
- content = main_content.get_text(strip=True)
132
-
133
  return {
134
  "company_name": tos_info["company_name"],
135
  "tos_link": tos_link,
136
- "content": content,
137
- "note": "Content extracted from HTML page"
 
138
  }
139
-
140
- except requests.exceptions.RequestException as e:
141
- error_msg = f"Error accessing TOS link: {str(e)}"
142
- if hasattr(e, 'response') and e.response is not None:
143
- error_msg += f" (Status code: {e.response.status_code})"
144
- if hasattr(e.response, 'text'):
145
- error_msg += f" - {e.response.text[:200]}"
146
- print(error_msg)
147
- return {"error": error_msg}
148
-
149
- except Exception as e:
150
- error_msg = f"Unexpected error while processing content: {str(e)}"
151
- print(error_msg)
152
- return {"error": error_msg}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
- @tool
155
- def search_court_cases(keyword: str, jurisdiction: str = None,
156
- date_range: tuple = None) -> List[Dict]:
157
-
158
- """Search court cases by keyword and jurisdiction
159
- Args:
160
- keyword: Legal concepts or case terms to search
161
- jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
162
- date_range: Tuple of (start_date, end_date) in UTC
163
- """
164
-
165
- try:
166
- # API
167
- api_base = "https://www.courtlistener.com/api/rest/v4/"
168
-
169
- # zgradimo query
170
- params = {
171
- "q": keyword,
172
- "type": "o", # 'o' opinion oz. mnenje
173
- "page_size": 5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  }
175
-
176
-
177
- if jurisdiction:
178
- if jurisdiction.lower() == "scotus":
179
- params["court"] = "scotus"
180
- else:
181
- params["court"] = jurisdiction
182
-
183
- # Datum in format
184
- if date_range:
185
- params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
186
- params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
187
-
188
- headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
189
-
190
- print(f"Request URL: {api_base}search/")
191
- print(f"Params: {params}")
192
-
193
- response = requests.get(f"{api_base}search/", params=params, headers=headers)
194
-
195
- print(f"Response status: {response.status_code}")
196
- print(f"Response body preview: {response.text[:200]}...")
197
-
198
- # Handle HTTP errors
199
- if response.status_code == 403:
200
- return "Authentication failed: Please check your API key"
201
-
202
- response.raise_for_status()
203
-
204
- data = response.json()
205
-
206
- if 'results' not in data or len(data['results']) == 0:
207
- return "No matching cases found in the database"
208
-
209
- cases = []
210
- for result in data['results']:
211
- case_info = {
212
- "case_name": result.get('caseName', result.get('case_name', 'Unknown')),
213
- "citation": result.get('citation', 'No citation available'),
214
- "court": result.get('court_full_name', result.get('court', 'Unknown')),
215
- "date_filed": result.get('date_filed', 'Unknown date'),
216
- "summary": result.get('text', result.get('snippet', ''))[:200] + '...' if result.get('text') or result.get('snippet') else "No summary available"
217
- }
218
- cases.append(case_info)
219
-
220
- return cases
221
-
222
- except requests.exceptions.RequestException as e:
223
- error_msg = f"API Error: {str(e)}"
224
- if hasattr(e, 'response') and e.response is not None:
225
- error_msg += f" (Status code: {e.response.status_code})"
226
- if hasattr(e.response, 'text'):
227
- error_msg += f" - {e.response.text[:200]}"
228
- print(error_msg) # debug
229
- return error_msg
230
-
231
- except Exception as e:
232
- error_msg = f"Unexpected error: {str(e)}"
233
- print(error_msg) # debug
234
- return error_msg
235
-
236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
- final_answer = FinalAnswerTool()
239
 
240
- #kateri model in kje se povežemo!
241
- model = HfApiModel(
242
- max_tokens=2096,
243
- temperature=0.3,
244
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
245
- custom_role_conversions=None,
246
- )
247
 
248
- #prompti za agenta
249
- with open("prompts.yaml", 'r') as stream:
250
- prompt_templates = yaml.safe_load(stream)
251
 
252
- #param za agenta
253
- agent = CodeAgent(
254
- model=model,
255
- tools=[final_answer,search_terms_of_service,extract_tos_content], #search_court_cases, check_citation_status], #orodja
256
- max_steps=6,
257
- verbosity_level=1,
258
- grammar=None,
259
- planning_interval=None,
260
- name="Legal Research Assistant",
261
- description="Specializes in US federal and state court decisions analysis",
262
- prompt_templates=prompt_templates
263
- )
264
 
265
- GradioUI(agent).launch() #launch za UI
 
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
+
12
+ @tool
13
+ def search_terms_of_service(company_name: str) -> Dict:
14
+ """Search for a company's Terms of Service link
15
+
16
+ Args:
17
+ company_name: Name of the organization/company to search for
18
+
19
+ Returns:
20
+ Dictionary containing company name and link to their Terms of Service
21
+ """
22
+
23
+ try:
24
+ # API URL
25
+ api_base = "https://05bf-176-76-227-247.ngrok-free.app" # lokalno ga runnamo
26
 
27
+ # API request naredimo
28
+ url = f"{api_base}/company/{company_name}"
29
 
30
+ print(f"Request URL: {url}")
 
 
31
 
32
+ response = requests.get(url)
33
+
34
+ print(f"Response status: {response.status_code}")
35
+ print(f"Response body preview: {response.text[:200]}...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ if response.status_code == 404: #HTTP error!
38
+ return {"error": f"Company '{company_name}' not found in the database"}
39
 
40
+ response.raise_for_status()
 
 
41
 
42
+ data = response.json()
43
 
44
+ # ali obstaja več linkov?
45
+ if not data.get('secondary_links') or len(data['secondary_links']) == 0:
46
+ return {"error": f"No Terms of Service links found for {company_name}"}
47
 
48
+ # returnamo ime in link, lahko jih več
49
+ result = {
50
+ "company_name": data["name"],
51
+ "tos_link": data["secondary_links"][0], # prvi link
52
+ "all_links": data["secondary_links"] # več linkov
53
+ }
54
+
55
+ return result
56
+
57
+ except requests.exceptions.RequestException as e:
58
+ error_msg = f"API Error: {str(e)}"
59
+ if hasattr(e, 'response') and e.response is not None:
60
+ error_msg += f" (Status code: {e.response.status_code})"
61
+ if hasattr(e.response, 'text'):
62
+ error_msg += f" - {e.response.text[:200]}"
63
+ print(error_msg) # debug, da prizna, če se zmoti
64
+ return {"error": error_msg}
65
+
66
+ except Exception as e:
67
+ error_msg = f"Unexpected error: {str(e)}"
68
+ print(error_msg) # debug, da AI prizna, če se zmoti
69
+ return {"error": error_msg}
70
+
71
+ @tool
72
+ def extract_tos_content(company_name: str) -> Dict:
73
+ """
74
+ Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository.
75
+
76
+ Args:
77
+ company_name: Name of the organization/company to search for
78
+
79
+ Returns:
80
+ Dictionary containing company name, link, and extracted content
81
+ """
82
+
83
+ tos_info = search_terms_of_service(company_name)
84
+
85
+ # error
86
+ if "error" in tos_info:
87
+ return tos_info
88
+
89
+ try:
90
+ # link od zgoraj
91
+ tos_link = tos_info["tos_link"]
92
+
93
+ print(f"Accessing TOS link: {tos_link}")
94
+
95
+ # Gitlab repo
96
+ if "code.europa.eu" in tos_link and "/-/blob/" in tos_link:
97
+ # spremeni '/-/blob/' z '/-/raw/'
98
+ raw_url = tos_link.replace("/-/blob/", "/-/raw/")
99
 
 
100
 
101
+ if "?" in raw_url:
102
+ raw_url = raw_url.split("?")[0]
 
 
 
 
 
 
 
 
103
 
104
+ print(f"Accessing raw content URL: {raw_url}")
 
 
 
 
 
 
 
 
 
 
105
 
106
+ # request za raw data iz GItlabaa
107
+ response = requests.get(raw_url)
108
  response.raise_for_status()
109
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  return {
111
  "company_name": tos_info["company_name"],
112
  "tos_link": tos_link,
113
+ "raw_url": raw_url,
114
+ "content": response.text, # cel raw data fajl
115
+ "content_type": "markdown" if raw_url.endswith(".md") else "text"
116
  }
117
+
118
+ response = requests.get(tos_link)
119
+ response.raise_for_status()
120
+
121
+ soup = BeautifulSoup(response.text, 'html.parser')
122
+
123
+ content = ""
124
+
125
+ pre_element = soup.find('pre')
126
+ if pre_element:
127
+ content = pre_element.get_text()
128
+ else:
129
+ main_content = soup.find('main') or soup.find('article') or soup.find('body')
130
+ if main_content:
131
+ content = main_content.get_text(strip=True)
132
+
133
+ return {
134
+ "company_name": tos_info["company_name"],
135
+ "tos_link": tos_link,
136
+ "content": content,
137
+ "note": "Content extracted from HTML page"
138
+ }
139
+
140
+ except requests.exceptions.RequestException as e:
141
+ error_msg = f"Error accessing TOS link: {str(e)}"
142
+ if hasattr(e, 'response') and e.response is not None:
143
+ error_msg += f" (Status code: {e.response.status_code})"
144
+ if hasattr(e.response, 'text'):
145
+ error_msg += f" - {e.response.text[:200]}"
146
+ print(error_msg)
147
+ return {"error": error_msg}
148
+
149
+ except Exception as e:
150
+ error_msg = f"Unexpected error while processing content: {str(e)}"
151
+ print(error_msg)
152
+ return {"error": error_msg}
153
 
154
+ @tool
155
+ def search_court_cases(keyword: str, jurisdiction: str = None,
156
+ date_range: tuple = None) -> List[Dict]:
157
+
158
+ """Search court cases by keyword and jurisdiction
159
+ Args:
160
+ keyword: Legal concepts or case terms to search
161
+ jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
162
+ date_range: Tuple of (start_date, end_date) in UTC
163
+ """
164
+
165
+ try:
166
+ # API
167
+ api_base = "https://www.courtlistener.com/api/rest/v4/"
168
+
169
+ # zgradimo query
170
+ params = {
171
+ "q": keyword,
172
+ "type": "o", # 'o' opinion oz. mnenje
173
+ "page_size": 5
174
+ }
175
+
176
+
177
+ if jurisdiction:
178
+ if jurisdiction.lower() == "scotus":
179
+ params["court"] = "scotus"
180
+ else:
181
+ params["court"] = jurisdiction
182
+
183
+ # Datum in format
184
+ if date_range:
185
+ params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
186
+ params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
187
+
188
+ headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
189
+
190
+ print(f"Request URL: {api_base}search/")
191
+ print(f"Params: {params}")
192
+
193
+ response = requests.get(f"{api_base}search/", params=params, headers=headers)
194
+
195
+ print(f"Response status: {response.status_code}")
196
+ print(f"Response body preview: {response.text[:200]}...")
197
+
198
+ # Handle HTTP errors
199
+ if response.status_code == 403:
200
+ return "Authentication failed: Please check your API key"
201
+
202
+ response.raise_for_status()
203
+
204
+ data = response.json()
205
+
206
+ if 'results' not in data or len(data['results']) == 0:
207
+ return "No matching cases found in the database"
208
+
209
+ cases = []
210
+ for result in data['results']:
211
+ case_info = {
212
+ "case_name": result.get('caseName', result.get('case_name', 'Unknown')),
213
+ "citation": result.get('citation', 'No citation available'),
214
+ "court": result.get('court_full_name', result.get('court', 'Unknown')),
215
+ "date_filed": result.get('date_filed', 'Unknown date'),
216
+ "summary": result.get('text', result.get('snippet', ''))[:200] + '...' if result.get('text') or result.get('snippet') else "No summary available"
217
  }
218
+ cases.append(case_info)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
+ return cases
221
+
222
+ except requests.exceptions.RequestException as e:
223
+ error_msg = f"API Error: {str(e)}"
224
+ if hasattr(e, 'response') and e.response is not None:
225
+ error_msg += f" (Status code: {e.response.status_code})"
226
+ if hasattr(e.response, 'text'):
227
+ error_msg += f" - {e.response.text[:200]}"
228
+ print(error_msg) # debug
229
+ return error_msg
230
+
231
+ except Exception as e:
232
+ error_msg = f"Unexpected error: {str(e)}"
233
+ print(error_msg) # debug
234
+ return error_msg
235
+
236
+
237
 
238
+ final_answer = FinalAnswerTool()
239
 
240
+ #kateri model in kje se povežemo!
241
+ model = HfApiModel(
242
+ max_tokens=2096,
243
+ temperature=0.3,
244
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
245
+ custom_role_conversions=None,
246
+ )
247
 
248
+ #prompti za agenta
249
+ with open("prompts.yaml", 'r') as stream:
250
+ prompt_templates = yaml.safe_load(stream)
251
 
252
+ #param za agenta
253
+ agent = CodeAgent(
254
+ model=model,
255
+ tools=[final_answer,search_terms_of_service,extract_tos_content], #search_court_cases, check_citation_status], #orodja
256
+ max_steps=6,
257
+ verbosity_level=1,
258
+ grammar=None,
259
+ planning_interval=None,
260
+ name="Legal Research Assistant",
261
+ description="Specializes in US federal and state court decisions analysis",
262
+ prompt_templates=prompt_templates
263
+ )
264
 
265
+ GradioUI(agent).launch() #launch za UI