Spaces:
Build error
Build error
legaltextai
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,135 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import requests
|
3 |
import os
|
|
|
4 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
@st.cache_data(ttl=3600)
|
7 |
-
def
|
8 |
-
url =
|
9 |
headers = {
|
10 |
-
"
|
11 |
}
|
12 |
-
|
13 |
params = {
|
14 |
-
"
|
15 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
}
|
17 |
|
18 |
-
response = requests.get(url,
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
@st.cache_data(ttl=3600)
|
26 |
def get_summary(text):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
|
43 |
-
Cases cited (which cases the court cited and how it treated them).
|
44 |
-
Present in bullet point format'''},
|
45 |
-
{"role": "user", "content": f"Please summarize this case: {text}. "}
|
46 |
-
]
|
47 |
-
)
|
48 |
-
|
49 |
-
return completion.choices[0].message.content
|
50 |
|
51 |
-
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
st.
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
import os
|
5 |
+
import time
|
6 |
from openai import OpenAI
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
genai.configure(api_key='AIzaSyBE9XAwJiAs6xY2UukvGYsy0ghtxA1F2q8')
|
10 |
+
|
11 |
+
generation_config = {
|
12 |
+
"temperature": 0,
|
13 |
+
"top_p": 0.95,
|
14 |
+
"top_k": 64,
|
15 |
+
"max_output_tokens": 8192,
|
16 |
+
"response_mime_type": "text/plain",
|
17 |
+
}
|
18 |
+
|
19 |
+
headers = {
|
20 |
+
"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"
|
21 |
+
}
|
22 |
+
|
23 |
+
proxies = {"http": os.getenv("HTTP_PROXY")}
|
24 |
+
|
25 |
+
|
26 |
|
27 |
@st.cache_data(ttl=3600)
|
28 |
+
def search_legal_cases(query, num_results=10):
|
29 |
+
url = "https://scholar.google.com/scholar?hl=en&as_sdt=6"
|
30 |
headers = {
|
31 |
+
"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"
|
32 |
}
|
33 |
+
|
34 |
params = {
|
35 |
+
"q": query,
|
36 |
+
"hl": "en",
|
37 |
+
"num": num_results,
|
38 |
+
"as_sdt": "4", # This parameter filters the search results to legal cases
|
39 |
+
}
|
40 |
+
|
41 |
+
response = requests.get(url, proxies=proxies, headers=headers, params=params)
|
42 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
43 |
+
|
44 |
+
results = []
|
45 |
+
for result in soup.find_all("div", class_="gs_ri"):
|
46 |
+
title = result.find("h3", class_="gs_rt").text
|
47 |
+
base_url = "https://scholar.google.com"
|
48 |
+
link = base_url + result.find("a")["href"]
|
49 |
+
citation = result.find("div", class_="gs_a").text.replace(" - Google Scholar", "")
|
50 |
+
results.append((title, link, citation))
|
51 |
+
|
52 |
+
return results
|
53 |
+
|
54 |
+
@st.cache_data(ttl=3600)
|
55 |
+
def extract_text_from_link(url):
|
56 |
+
headers = {
|
57 |
+
"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"
|
58 |
}
|
59 |
|
60 |
+
response = requests.get(url, headers=headers, proxies=proxies)
|
61 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
62 |
|
63 |
+
text = soup.get_text(separator="\n")
|
64 |
+
return text
|
65 |
+
|
66 |
+
|
67 |
+
# @st.cache_data(ttl=3600)
|
68 |
+
# def get_summary(text):
|
69 |
+
# client = OpenAI(api_key='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')
|
70 |
+
|
71 |
+
# completion = client.chat.completions.create(
|
72 |
+
# model="gpt-4o",
|
73 |
+
# messages=[
|
74 |
+
# {"role": "system", "content": f'''You are a law professor specialized in legal writing and legal research.
|
75 |
+
# When presented with a case by a user please summarize it according to the following requirements:
|
76 |
+
# Name of the court.
|
77 |
+
# Facts (name of the parties, what happened factually).
|
78 |
+
# Procedural history (what happened in the past procedurally, what were prior judgements).
|
79 |
+
# Issues (what is in dispute).
|
80 |
+
# Holding (the applied rule of law).
|
81 |
+
# Rationale (reasons for the holding).
|
82 |
+
# Decision (what did the court decide, e.g. affirmed, overruled).
|
83 |
+
# Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
|
84 |
+
# Cases cited (which cases the court cited and how it treated them).'''},
|
85 |
+
# {"role": "user", "content": f"Please summarize this case according to the instructions: {text}. "}
|
86 |
+
# ]
|
87 |
+
# )
|
88 |
+
|
89 |
+
# return completion.choices[0].message.content
|
90 |
+
|
91 |
|
|
|
92 |
def get_summary(text):
|
93 |
+
model = genai.GenerativeModel('gemini-1.5-flash', generation_config=generation_config)
|
94 |
+
response = model.generate_content(f'''You are a law professor specialized in legal writing and legal research.
|
95 |
+
When presented with a case by a user please summarize it according to the following requirements:
|
96 |
+
Name of the court.
|
97 |
+
Facts (name of the parties, what happened factually).
|
98 |
+
Procedural history (what happened in the past procedurally, what were prior judgements).
|
99 |
+
Issues (what is in dispute).
|
100 |
+
Holding (the applied rule of law).
|
101 |
+
Rationale (reasons for the holding).
|
102 |
+
Decision (what did the court decide, e.g. affirmed, overruled).
|
103 |
+
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
|
104 |
+
Cases cited (which cases the court cited and how it treated them).
|
105 |
+
Here is the text of the court decision: {text}''',
|
106 |
+
stream=False)
|
107 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
|
110 |
+
st.write("\n")
|
111 |
+
st.write("\n")
|
112 |
+
search_query = st.text_input("case name, e.g. brown v board supreme, 372 US 335, google v oracle appeal")
|
113 |
|
114 |
+
if search_query:
|
115 |
+
with st.spinner("Searching for cases..."):
|
116 |
+
results = search_legal_cases(search_query)
|
117 |
+
if results:
|
118 |
+
title, link, citation = results[0]
|
119 |
+
st.write("Title:\n", title)
|
120 |
+
#st.write("Link:\n", link)
|
121 |
+
st.write("Citation:\n", citation)
|
122 |
+
#with st.spinner("Extracting text from case / Generating summary"):
|
123 |
+
text = extract_text_from_link(link)
|
124 |
+
#st.write(text) # Optionally display the extracted text
|
125 |
|
126 |
+
summary = get_summary(text)
|
127 |
+
#st.write(response)
|
128 |
+
st.write(summary.text)
|
129 |
+
#for chunk in summary:
|
130 |
+
#st.write(chunk.text)
|
131 |
+
#st.write("_"*80)
|
132 |
+
|
133 |
+
|
134 |
+
else:
|
135 |
+
st.write("No results found.")
|