Spaces:
Build error
Build error
import streamlit as st | |
from bs4 import BeautifulSoup | |
import requests | |
import os | |
import time | |
from openai import OpenAI | |
import google.generativeai as genai | |
genai.configure(api_key='AIzaSyBE9XAwJiAs6xY2UukvGYsy0ghtxA1F2q8') | |
generation_config = { | |
"temperature": 0, | |
"top_p": 0.95, | |
"top_k": 64, | |
"max_output_tokens": 8192, | |
"response_mime_type": "text/plain", | |
} | |
headers = { | |
"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" | |
} | |
proxies = {"http": os.getenv("HTTP_PROXY")} | |
def search_legal_cases(query, num_results=10): | |
url = "https://scholar.google.com/scholar?hl=en&as_sdt=6" | |
headers = { | |
"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" | |
} | |
params = { | |
"q": query, | |
"hl": "en", | |
"num": num_results, | |
"as_sdt": "4", # This parameter filters the search results to legal cases | |
} | |
response = requests.get(url, proxies=proxies, headers=headers, params=params) | |
soup = BeautifulSoup(response.text, "html.parser") | |
results = [] | |
for result in soup.find_all("div", class_="gs_ri"): | |
title = result.find("h3", class_="gs_rt").text | |
base_url = "https://scholar.google.com" | |
link = base_url + result.find("a")["href"] | |
citation = result.find("div", class_="gs_a").text.replace(" - Google Scholar", "") | |
results.append((title, link, citation)) | |
return results | |
def extract_text_from_link(url): | |
headers = { | |
"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" | |
} | |
response = requests.get(url, headers=headers, proxies=proxies) | |
soup = BeautifulSoup(response.content, "html.parser") | |
text = soup.get_text(separator="\n") | |
return text | |
# @st.cache_data(ttl=3600) | |
# def get_summary(text): | |
# client = OpenAI(api_key='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') | |
# completion = client.chat.completions.create( | |
# model="gpt-4o", | |
# messages=[ | |
# {"role": "system", "content": f'''You are a law professor specialized in legal writing and legal research. | |
# When presented with a case by a user please summarize it according to the following requirements: | |
# Name of the court. | |
# Facts (name of the parties, what happened factually). | |
# Procedural history (what happened in the past procedurally, what were prior judgements). | |
# Issues (what is in dispute). | |
# Holding (the applied rule of law). | |
# Rationale (reasons for the holding). | |
# Decision (what did the court decide, e.g. affirmed, overruled). | |
# Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion). | |
# Cases cited (which cases the court cited and how it treated them).'''}, | |
# {"role": "user", "content": f"Please summarize this case according to the instructions: {text}. "} | |
# ] | |
# ) | |
# return completion.choices[0].message.content | |
def get_summary(text): | |
model = genai.GenerativeModel('gemini-1.5-flash', generation_config=generation_config) | |
response = model.generate_content(f'''You are a law professor specialized in legal writing and legal research. | |
When presented with a case by a user please summarize it according to the following requirements: | |
Name of the court. | |
Facts (name of the parties, what happened factually). | |
Procedural history (what happened in the past procedurally, what were prior judgements). | |
Issues (what is in dispute). | |
Holding (the applied rule of law). | |
Rationale (reasons for the holding). | |
Decision (what did the court decide, e.g. affirmed, overruled). | |
Other opinions (if there are any dissenting or concurring opinions, summarize majority opinion, dissenting opinion and concurring opinion). | |
Cases cited (which cases the court cited and how it treated them). | |
Here is the text of the court decision: {text}''', | |
stream=False) | |
return response | |
st.write("\n") | |
st.write("\n") | |
search_query = st.text_input("case name, e.g. brown v board supreme, 372 US 335, google v oracle appeal") | |
if search_query: | |
with st.spinner("Searching for cases..."): | |
results = search_legal_cases(search_query) | |
if results: | |
title, link, citation = results[0] | |
st.write("Title:\n", title) | |
#st.write("Link:\n", link) | |
st.write("Citation:\n", citation) | |
#with st.spinner("Extracting text from case / Generating summary"): | |
text = extract_text_from_link(link) | |
#st.write(text) # Optionally display the extracted text | |
summary = get_summary(text) | |
#st.write(response) | |
st.write(summary.text) | |
#for chunk in summary: | |
#st.write(chunk.text) | |
#st.write("_"*80) | |
else: | |
st.write("No results found.") | |