Spaces:
Build error
Build error
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import anthropic
|
6 |
+
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT
|
7 |
+
from io import StringIO
|
8 |
+
import time
|
9 |
+
from io import BytesIO
|
10 |
+
|
11 |
+
|
12 |
+
anthropic = Anthropic(
|
13 |
+
api_key="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",
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
headers = {
|
18 |
+
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
|
19 |
+
}
|
20 |
+
|
21 |
+
proxies = {"http": os.getenv("HTTP_PROXY")}
|
22 |
+
|
23 |
+
|
24 |
+
@st.cache_data(ttl=3600)
|
25 |
+
def search_legal_cases(query, num_results=10):
|
26 |
+
url = "https://scholar.google.com/scholar?hl=en&as_sdt=6"
|
27 |
+
headers = {
|
28 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.3"
|
29 |
+
}
|
30 |
+
|
31 |
+
params = {
|
32 |
+
"q": query,
|
33 |
+
"hl": "en",
|
34 |
+
"num": num_results,
|
35 |
+
"as_sdt": "4", # This parameter filters the search results to legal cases
|
36 |
+
}
|
37 |
+
|
38 |
+
response = requests.get(url, proxies=proxies, headers=headers, params=params)
|
39 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
40 |
+
|
41 |
+
results = []
|
42 |
+
for result in soup.find_all("div", class_="gs_ri"):
|
43 |
+
title = result.find("h3", class_="gs_rt").text
|
44 |
+
base_url = "https://scholar.google.com"
|
45 |
+
link = base_url + result.find("a")["href"]
|
46 |
+
citation = result.find("div", class_="gs_a").text.replace(" - Google Scholar", "")
|
47 |
+
results.append((title, link, citation))
|
48 |
+
|
49 |
+
return results
|
50 |
+
|
51 |
+
@st.cache_data(ttl=3600)
|
52 |
+
def extract_text_from_link(url):
|
53 |
+
headers = {
|
54 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.3"
|
55 |
+
}
|
56 |
+
|
57 |
+
response = requests.get(url, headers=headers, proxies=proxies)
|
58 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
59 |
+
|
60 |
+
text = soup.get_text(separator="\n")
|
61 |
+
return text
|
62 |
+
|
63 |
+
|
64 |
+
@st.cache_data(ttl=3600)
|
65 |
+
def get_summary(text):
|
66 |
+
prompt = f'''{HUMAN_PROMPT}
|
67 |
+
You are a law professor specialized in legal writing and legal research.
|
68 |
+
Summarize the case in {text} in json format according to the following requirements:
|
69 |
+
Facts (name of the case and its parties, what happened factually).
|
70 |
+
Procedural history (what happened in the past procedurally, what were prior judgements).
|
71 |
+
Issues (what is in dispute).
|
72 |
+
Holding (the applied rule of law).
|
73 |
+
Rationale (reasons for the holding).
|
74 |
+
Decision (what did the court decide, e.g. affirmed, overruled).
|
75 |
+
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
|
76 |
+
Cases cited (which cases the court cited and how it treated them):
|
77 |
+
- 'red flag' means some negative treatment — such as a cited case is being overruled, superseded, or not followed by this court for some reason.
|
78 |
+
- 'yellow flag' means the case has some negative treatment by this court but has not been reversed or overruled. For example, the reasoning of the decision was criticized or its holding was limited to a specific set of facts.
|
79 |
+
- 'blue flag' means the case has been appealed to the U.S. Court of Appeals or the U.S. Supreme Court (excluding appeals originating from agencies).
|
80 |
+
|
81 |
+
Present the summary in json format.
|
82 |
+
{AI_PROMPT}'''
|
83 |
+
completion = anthropic.completions.create(
|
84 |
+
#model="claude-2",
|
85 |
+
model = "claude-instant-1.2",
|
86 |
+
max_tokens_to_sample=2000,
|
87 |
+
prompt=prompt,
|
88 |
+
)
|
89 |
+
response = completion.completion
|
90 |
+
return response
|
91 |
+
|
92 |
+
search_query = st.text_input("case name, e.g. brown v board supreme, 372 US 335, google v oracle appeal")
|
93 |
+
|
94 |
+
if search_query:
|
95 |
+
with st.spinner("Searching for cases..."):
|
96 |
+
results = search_legal_cases(search_query)
|
97 |
+
if results:
|
98 |
+
title, link, citation = results[0]
|
99 |
+
st.write("Title:\n", title)
|
100 |
+
#st.write("Link:\n", link)
|
101 |
+
st.write("Citation:\n", citation)
|
102 |
+
with st.spinner("Extracting text from case..."):
|
103 |
+
text = extract_text_from_link(link)
|
104 |
+
#st.write(text)
|
105 |
+
else:
|
106 |
+
st.write("No results found.")
|
107 |
+
|
108 |
+
|
109 |
+
if search_query:
|
110 |
+
with st.spinner("Generating summary..."):
|
111 |
+
summary = get_summary(text)
|
112 |
+
st.write(summary)
|
113 |
+
|
114 |
+
|
115 |
+
# Convert the response to a file-like object for download
|
116 |
+
download_bytes = BytesIO(summary.encode())
|
117 |
+
st.download_button("Download the results", download_bytes, "summary.txt", "text/plain")
|