Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_index.core import Settings
|
| 3 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
|
| 4 |
+
from llama_index.embeddings.gemini import GeminiEmbedding
|
| 5 |
+
from llama_index.llms.gemini import Gemini
|
| 6 |
+
from llama_index.core import Document
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
#import streamlit_analytics2 as streamlit_analytics
|
| 9 |
+
|
| 10 |
+
# Set up Google API key
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
# Configure Google Gemini
|
| 14 |
+
|
| 15 |
+
# Load and index the legal document data
|
| 16 |
+
def load_data(uploaded_files):
|
| 17 |
+
documents = [Document(text=t) for t in uploaded_files]
|
| 18 |
+
#documents = SimpleDirectoryReader(input_files=[uploaded_files]).load_data()
|
| 19 |
+
Settings.embed_model = GeminiEmbedding(api_key=os.getenv("GOOGLE_API_KEY"), model_name="models/embedding-001")
|
| 20 |
+
Settings.llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.8, model_name="models/gemini-pro")
|
| 21 |
+
llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.8, model_name="models/gemini-pro")
|
| 22 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 23 |
+
return index
|
| 24 |
+
|
| 25 |
+
# Generate legal document summary
|
| 26 |
+
def generate_summary(index, document_text):
|
| 27 |
+
query_engine = index.as_query_engine()
|
| 28 |
+
response = query_engine.query(f"""
|
| 29 |
+
You are a skilled legal analyst. Your task is to provide a comprehensive summary of the given legal document.
|
| 30 |
+
Analyze the following legal document and summarize it:
|
| 31 |
+
{document_text}
|
| 32 |
+
|
| 33 |
+
Please cover the following aspects:
|
| 34 |
+
1. Document type and purpose
|
| 35 |
+
2. Key parties involved
|
| 36 |
+
3. Main clauses and provisions
|
| 37 |
+
4. Important dates and deadlines
|
| 38 |
+
5. Potential legal implications
|
| 39 |
+
6. Any notable or unusual elements
|
| 40 |
+
|
| 41 |
+
Provide a clear, concise, and professional summary that would be useful for legal professionals or clients.
|
| 42 |
+
""")
|
| 43 |
+
return response.response
|
| 44 |
+
|
| 45 |
+
# Streamlit app
|
| 46 |
+
def main():
|
| 47 |
+
st.title("Legal Document Summarizer")
|
| 48 |
+
st.write("Upload a legal document, and let our AI summarize it!")
|
| 49 |
+
|
| 50 |
+
# File uploader
|
| 51 |
+
uploaded_file = st.file_uploader("Choose a legal document file", type=["txt", "pdf"])
|
| 52 |
+
|
| 53 |
+
if uploaded_file is not None:
|
| 54 |
+
# Read file contents
|
| 55 |
+
if uploaded_file.type == "application/pdf":
|
| 56 |
+
# You'll need to install PyPDF2 for this
|
| 57 |
+
import PyPDF2
|
| 58 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 59 |
+
document_text = ""
|
| 60 |
+
l = []
|
| 61 |
+
for page in pdf_reader.pages:
|
| 62 |
+
document_text += page.extract_text()
|
| 63 |
+
l.append(page.extract_text())
|
| 64 |
+
else:
|
| 65 |
+
document_text = uploaded_file.getvalue().decode("utf-8")
|
| 66 |
+
|
| 67 |
+
st.write("Analyzing legal document...")
|
| 68 |
+
|
| 69 |
+
# Load data and generate summary
|
| 70 |
+
index = load_data(l)
|
| 71 |
+
summary = generate_summary(index, document_text)
|
| 72 |
+
|
| 73 |
+
st.write("## Legal Document Summary")
|
| 74 |
+
st.write(summary)
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
main()
|