File size: 2,912 Bytes
9b4a2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import gradio as gr
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain_community.llms import HuggingFaceEndpoint

# Load Hugging Face API token
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")

# Load LLM with token
llm = HuggingFaceEndpoint(
    repo_id="google/flan-t5-base",
    huggingfacehub_api_token=hf_token,
    model_kwargs={"temperature": 0.7, "max_length": 512}
)

summary_cache = ""
glossary_cache = ""
retriever_chain = None

def extract_text_and_summary(file):
    global retriever_chain, summary_cache, glossary_cache

    loader = PyPDFLoader(file.name)
    docs = loader.load()

    splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    splits = splitter.split_documents(docs)
    full_text = "\n".join([doc.page_content for doc in splits])

    embeddings = HuggingFaceEmbeddings()
    db = FAISS.from_documents(splits, embeddings)
    retriever = db.as_retriever()
    retriever_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)

    summary_prompt = f"Summarize this legal document:\n{full_text[:1500]}"
    glossary_prompt = f"Extract and define legal terms from the document:\n{full_text[:1500]}"

    summary_cache = llm(summary_prompt)
    glossary_cache = llm(glossary_prompt)

    filename = "summary_output.txt"
    with open(filename, "w", encoding="utf-8") as f:
        f.write("=== Summary ===\n")
        f.write(summary_cache + "\n\n")
        f.write("=== Glossary ===\n")
        f.write(glossary_cache + "\n")

    return full_text, summary_cache, glossary_cache, filename

def answer_custom_question(question):
    if retriever_chain:
        return retriever_chain.run(question)
    return "Please upload and process a document first."

with gr.Blocks() as demo:
    gr.Markdown("## 🧾 Legal Document Summarizer Using LangChain")

    with gr.Row():
        file = gr.File(label="πŸ“ Upload Legal PDF", file_types=[".pdf"])
        process_btn = gr.Button("πŸ” Extract & Summarize")

    extracted_text = gr.Textbox(label="πŸ“„ Extracted Text", lines=10)
    summary_output = gr.Textbox(label="πŸ“ Summary", lines=5)
    glossary_output = gr.Textbox(label="πŸ“˜ Glossary", lines=5)
    download_link = gr.File(label="⬇️ Download Summary")

    with gr.Row():
        user_question = gr.Textbox(label="❓ Ask a Custom Question")
        custom_answer = gr.Textbox(label="πŸ€– AI Answer")
        ask_btn = gr.Button("🧠 Get Answer")

    process_btn.click(fn=extract_text_and_summary, inputs=file, outputs=[
        extracted_text, summary_output, glossary_output, download_link
    ])
    ask_btn.click(fn=answer_custom_question, inputs=user_question, outputs=custom_answer)

demo.launch()