Spaces:
Runtime error
Runtime error
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() | |