Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,67 @@
|
|
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 |
|
8 |
-
|
9 |
headers = {
|
10 |
"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"
|
11 |
}
|
12 |
|
13 |
proxies = {"http": os.getenv("HTTP_PROXY")}
|
14 |
|
15 |
-
|
16 |
-
|
17 |
@st.cache_data(ttl=3600)
|
18 |
-
def
|
19 |
-
url = "https://
|
20 |
headers = {
|
21 |
"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"
|
22 |
}
|
23 |
-
|
24 |
params = {
|
25 |
-
"
|
26 |
-
"hl": "en",
|
27 |
-
"num": num_results,
|
28 |
-
"as_sdt": "4", # This parameter filters the search results to legal cases
|
29 |
-
}
|
30 |
-
|
31 |
-
response = requests.get(url, proxies=proxies, headers=headers, params=params)
|
32 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
33 |
-
|
34 |
-
results = []
|
35 |
-
for result in soup.find_all("div", class_="gs_ri"):
|
36 |
-
title = result.find("h3", class_="gs_rt").text
|
37 |
-
base_url = "https://scholar.google.com"
|
38 |
-
link = base_url + result.find("a")["href"]
|
39 |
-
citation = result.find("div", class_="gs_a").text.replace(" - Google Scholar", "")
|
40 |
-
results.append((title, link, citation))
|
41 |
-
|
42 |
-
return results
|
43 |
-
|
44 |
-
@st.cache_data(ttl=3600)
|
45 |
-
def extract_text_from_link(url):
|
46 |
-
headers = {
|
47 |
-
"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"
|
48 |
}
|
49 |
|
50 |
-
response = requests.get(url, headers=headers, proxies=proxies)
|
51 |
-
soup = BeautifulSoup(response.content, "html.parser")
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
@st.cache_data(ttl=3600)
|
58 |
def get_summary(text):
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
|
82 |
st.write("\n")
|
83 |
st.write("\n")
|
84 |
-
search_query = st.text_input("case
|
85 |
|
86 |
if search_query:
|
87 |
-
with st.spinner("
|
88 |
-
|
89 |
-
if
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
st.write("Citation:\n", citation)
|
94 |
-
#with st.spinner("Extracting text from case / Generating summary"):
|
95 |
-
text = extract_text_from_link(link)
|
96 |
-
#st.write(text) # Optionally display the extracted text
|
97 |
-
|
98 |
-
summary = get_summary(text)
|
99 |
-
st.write(summary)
|
100 |
-
|
101 |
else:
|
102 |
-
st.write("
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import requests
|
3 |
import os
|
|
|
4 |
from openai import OpenAI
|
5 |
|
|
|
6 |
headers = {
|
7 |
"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"
|
8 |
}
|
9 |
|
10 |
proxies = {"http": os.getenv("HTTP_PROXY")}
|
11 |
|
|
|
|
|
12 |
@st.cache_data(ttl=3600)
|
13 |
+
def extract_text_from_api(query):
|
14 |
+
url = "https://fastapi-example-uqyy.onrender.com/extract_text"
|
15 |
headers = {
|
16 |
"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"
|
17 |
}
|
18 |
+
|
19 |
params = {
|
20 |
+
"query": query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
|
23 |
+
response = requests.get(url, headers=headers, proxies=proxies, params=params)
|
|
|
24 |
|
25 |
+
if response.status_code == 200:
|
26 |
+
return response.json().get("extracted_text", "")
|
27 |
+
else:
|
28 |
+
return ""
|
29 |
|
30 |
@st.cache_data(ttl=3600)
|
31 |
def get_summary(text):
|
32 |
+
client = OpenAI(api_key='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')
|
33 |
+
|
34 |
+
completion = client.chat.completions.create(
|
35 |
+
model="gpt-4o",
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": '''You are a law professor specialized in legal writing and legal research.
|
38 |
+
When presented with a case by a user please summarize it according to the following requirements:
|
39 |
+
Name of the court.
|
40 |
+
Facts (name of the parties, what happened factually).
|
41 |
+
Procedural history (what happened in the past procedurally, what were prior judgements).
|
42 |
+
Issues (what is in dispute).
|
43 |
+
Holding (the applied rule of law).
|
44 |
+
Rationale (reasons for the holding).
|
45 |
+
Decision (what did the court decide, e.g. affirmed, overruled).
|
46 |
+
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion).
|
47 |
+
Cases cited (which cases the court cited and how it treated them).'''},
|
48 |
+
{"role": "user", "content": f"Please summarize this case according to the instructions: {text}. "}
|
49 |
+
]
|
50 |
+
)
|
51 |
+
|
52 |
+
return completion.choices[0].message.content
|
53 |
|
54 |
|
55 |
st.write("\n")
|
56 |
st.write("\n")
|
57 |
+
search_query = st.text_input("Enter the case query, e.g., 'brown v board supreme'")
|
58 |
|
59 |
if search_query:
|
60 |
+
with st.spinner("Extracting text from case..."):
|
61 |
+
text = extract_text_from_api(search_query)
|
62 |
+
if text:
|
63 |
+
with st.spinner("Generating summary..."):
|
64 |
+
summary = get_summary(text)
|
65 |
+
st.write(summary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
else:
|
67 |
+
st.write("Failed to extract text from the external API.")
|