summarizecase / app.py
legaltextai's picture
Update app.py
8266583 verified
import streamlit as st
import requests
import os
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
generation_config = {
"temperature": 0,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
@st.cache_data(ttl=3600)
def extract_text_from_api(query):
api_url = "https://urchin-app-c3zop.ondigitalocean.app/extract_text"
params = {"query": query, "num_results": 1}
try:
response = requests.get(api_url, params=params)
response.raise_for_status()
return response.json().get("text", "")
except requests.exceptions.RequestException as e:
st.error(f"Error fetching case data: {e}")
return ""
@st.cache_data(ttl=3600)
def get_summary(text):
model = genai.GenerativeModel('gemini-1.5-flash', generation_config=generation_config,
safety_settings = {
'HATE': 'BLOCK_NONE',
'HARASSMENT': 'BLOCK_NONE',
'SEXUAL' : 'BLOCK_NONE',
'DANGEROUS' : 'BLOCK_NONE'
})
try:
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 case and its proper citation.
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 case to be summarized: {text}''',
stream=False
)
return response
except Exception as e:
st.error(f"Error generating summary: {e}")
return ""
tab1, tab2, tab3 = st.tabs(["Summarize a case", "Find a case by facts", "Analyze a court brief"])
with tab1:
st.write("\n\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..."):
text = extract_text_from_api(search_query)
if text:
#st.write(text)
summary = get_summary(text)
st.write(summary.candidates[0].safety_ratings)
if summary:
st.write(summary.text)
else:
st.write("No case text found.")