Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,74 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, 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 |
|
| 10 |
-
COURTLISTENER_API_KEY = "8287a459013c68a65edda52a2f96126138f567e9"
|
| 11 |
API_BASE = "https://www.courtlistener.com/api/rest/v3/"
|
| 12 |
|
| 13 |
@tool
|
| 14 |
-
def search_court_cases(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
Parameters:
|
| 23 |
-
keyword (str): A search term or phrase related to a legal case.
|
| 24 |
-
jurisdiction (Optional[str]): The jurisdiction code for filtering results (e.g., "scotus" for the Supreme Court).
|
| 25 |
-
date_range (Optional[Tuple[str, str]]): A tuple of two date strings ("YYYY-MM-DD", "YYYY-MM-DD") specifying the start and end date.
|
| 26 |
-
|
| 27 |
-
Returns:
|
| 28 |
-
List[Dict]: A list of dictionaries containing case details, including:
|
| 29 |
-
- case_name (str): The name of the case.
|
| 30 |
-
- citation (str): The legal citation of the case.
|
| 31 |
-
- court (str): The court where the case was heard.
|
| 32 |
-
- date_filed (str): The date the case was filed.
|
| 33 |
-
- summary (str): A short snippet from the case text.
|
| 34 |
-
|
| 35 |
-
Raises:
|
| 36 |
-
requests.exceptions.RequestException: If there is an error with the CourtListener API request.
|
| 37 |
"""
|
| 38 |
try:
|
| 39 |
params = {
|
| 40 |
"q": keyword,
|
| 41 |
-
"
|
|
|
|
|
|
|
|
|
|
| 42 |
"page_size": 5
|
| 43 |
}
|
| 44 |
-
|
| 45 |
-
if date_range and isinstance(date_range, tuple) and len(date_range) == 2:
|
| 46 |
-
if date_range[0]:
|
| 47 |
-
params["date_filed__gte"] = date_range[0]
|
| 48 |
-
if date_range[1]:
|
| 49 |
-
params["date_filed__lte"] = date_range[1]
|
| 50 |
-
|
| 51 |
-
if jurisdiction:
|
| 52 |
-
params["court"] = jurisdiction
|
| 53 |
-
|
| 54 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 55 |
-
|
|
|
|
| 56 |
response.raise_for_status()
|
| 57 |
-
|
| 58 |
-
|
| 59 |
cases = []
|
| 60 |
-
for result in
|
| 61 |
case_info = {
|
| 62 |
-
"case_name": result.get(
|
| 63 |
-
"citation": result
|
| 64 |
-
"court": result
|
| 65 |
-
"date_filed": result.get(
|
| 66 |
-
"summary": result.get(
|
| 67 |
}
|
| 68 |
cases.append(case_info)
|
| 69 |
-
|
| 70 |
-
return cases if cases else [
|
| 71 |
|
| 72 |
except requests.exceptions.RequestException as e:
|
| 73 |
-
return
|
| 74 |
|
| 75 |
@tool
|
| 76 |
-
def check_citation_status(
|
| 77 |
-
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
Parameters:
|
| 81 |
-
citation (str): The citation of the legal case.
|
| 82 |
-
|
| 83 |
-
Returns:
|
| 84 |
-
Dict: A dictionary with case details, including:
|
| 85 |
-
- case_name (str): The name of the case.
|
| 86 |
-
- court (str): The court where the case was heard.
|
| 87 |
-
- date_filed (str): The date the case was filed.
|
| 88 |
-
- precedential_status (str): Whether the case is precedential.
|
| 89 |
-
- summary (str): A short snippet from the case.
|
| 90 |
"""
|
| 91 |
try:
|
| 92 |
-
params = {"citation": citation}
|
| 93 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 94 |
-
response = requests.get(f"{API_BASE}
|
| 95 |
response.raise_for_status()
|
| 96 |
-
|
| 97 |
data = response.json()
|
| 98 |
-
if not data["results"]:
|
| 99 |
-
return {"error": "No matching case found for this citation."}
|
| 100 |
-
|
| 101 |
-
case = data["results"][0]
|
| 102 |
return {
|
| 103 |
-
"
|
| 104 |
-
"
|
| 105 |
-
"
|
| 106 |
-
"precedential_status":
|
| 107 |
-
"summary": case.get("plain_text", "No summary available.")[:200] + "..."
|
| 108 |
}
|
| 109 |
-
|
| 110 |
except requests.exceptions.RequestException as e:
|
| 111 |
return {"error": str(e)}
|
| 112 |
|
|
@@ -114,14 +76,14 @@ final_answer = FinalAnswerTool()
|
|
| 114 |
|
| 115 |
model = HfApiModel(
|
| 116 |
max_tokens=2096,
|
| 117 |
-
temperature=0.3,
|
| 118 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
|
| 119 |
custom_role_conversions=None,
|
| 120 |
)
|
| 121 |
|
| 122 |
with open("prompts.yaml", 'r') as stream:
|
| 123 |
prompt_templates = yaml.safe_load(stream)
|
| 124 |
-
|
| 125 |
agent = CodeAgent(
|
| 126 |
model=model,
|
| 127 |
tools=[final_answer, search_court_cases, check_citation_status],
|
|
|
|
| 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 |
|
| 10 |
+
COURTLISTENER_API_KEY = "8287a459013c68a65edda52a2f96126138f567e9"
|
| 11 |
API_BASE = "https://www.courtlistener.com/api/rest/v3/"
|
| 12 |
|
| 13 |
@tool
|
| 14 |
+
def search_court_cases(keyword: str, jurisdiction: str = "federal",
|
| 15 |
+
date_range: tuple = (datetime.datetime.now(pytz.utc) - datetime.timedelta(days=365),
|
| 16 |
+
datetime.datetime.now(pytz.utc))) -> List[Dict]:
|
| 17 |
+
"""Search recent court cases by keyword and jurisdiction
|
| 18 |
+
Args:
|
| 19 |
+
keyword: Legal concepts or case terms to search
|
| 20 |
+
jurisdiction: Court jurisdiction (e.g., 'ca9' for 9th Circuit)
|
| 21 |
+
date_range: Tuple of (start_date, end_date) in UTC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
params = {
|
| 25 |
"q": keyword,
|
| 26 |
+
"jurisdiction": jurisdiction,
|
| 27 |
+
"dateFiled__gte": date_range[0].isoformat(),
|
| 28 |
+
"dateFiled__lte": date_range[1].isoformat(),
|
| 29 |
+
"order_by": "-dateFiled",
|
| 30 |
"page_size": 5
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 33 |
+
|
| 34 |
+
response = requests.get(f"{API_BASE}search/", params=params, headers=headers)
|
| 35 |
response.raise_for_status()
|
| 36 |
+
|
|
|
|
| 37 |
cases = []
|
| 38 |
+
for result in response.json()['results']:
|
| 39 |
case_info = {
|
| 40 |
+
"case_name": result.get('caseName', 'N/A'),
|
| 41 |
+
"citation": result.get('citation', 'N/A'),
|
| 42 |
+
"court": result.get('court', 'N/A'),
|
| 43 |
+
"date_filed": result.get('dateFiled', 'N/A'),
|
| 44 |
+
"summary": result.get('snippet', '')[:200] + '...'
|
| 45 |
}
|
| 46 |
cases.append(case_info)
|
| 47 |
+
|
| 48 |
+
return cases if cases else ["No relevant cases found"]
|
| 49 |
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
+
return f"API Error: {str(e)}"
|
| 52 |
|
| 53 |
@tool
|
| 54 |
+
def check_citation_status(citation_id: str) -> Dict:
|
| 55 |
+
"""Check the current status and treatment of a legal citation
|
| 56 |
+
Args:
|
| 57 |
+
citation_id: The CourtListener citation ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
try:
|
|
|
|
| 60 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 61 |
+
response = requests.get(f"{API_BASE}citations/{citation_id}/", headers=headers)
|
| 62 |
response.raise_for_status()
|
| 63 |
+
|
| 64 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return {
|
| 66 |
+
"status": data.get('status', 'Unknown'),
|
| 67 |
+
"treatment": data.get('treatment', 'Not available'),
|
| 68 |
+
"cited_by_count": data.get('cited_by_count', 0),
|
| 69 |
+
"precedential_status": data.get('precedential_status', 'Unknown')
|
|
|
|
| 70 |
}
|
| 71 |
+
|
| 72 |
except requests.exceptions.RequestException as e:
|
| 73 |
return {"error": str(e)}
|
| 74 |
|
|
|
|
| 76 |
|
| 77 |
model = HfApiModel(
|
| 78 |
max_tokens=2096,
|
| 79 |
+
temperature=0.3, # Lower temperature for legal accuracy
|
| 80 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
|
| 81 |
custom_role_conversions=None,
|
| 82 |
)
|
| 83 |
|
| 84 |
with open("prompts.yaml", 'r') as stream:
|
| 85 |
prompt_templates = yaml.safe_load(stream)
|
| 86 |
+
|
| 87 |
agent = CodeAgent(
|
| 88 |
model=model,
|
| 89 |
tools=[final_answer, search_court_cases, check_citation_status],
|