Spaces:
Build error
Build error
Download app.py from VerdictAI/Legal_Document_Summarization: direct link, hf CLI and curl.
- Browser
- Download file 4.53 kB
-
https://huggingface.co/spaces/VerdictAI/Legal_Document_Summarization/resolve/main/app.py
- Command line
-
hf download hf://spaces/VerdictAI/Legal_Document_Summarization/app.py
-
curl -L -o app.py https://huggingface.co/spaces/VerdictAI/Legal_Document_Summarization/resolve/main/app.py
4.53 kB
| import gradio | |
| print("Gradio version:", gradio.__version__) | |
| import re | |
| import pytesseract | |
| from pdfminer.high_level import extract_text | |
| from PIL import Image | |
| from transformers import pipeline | |
| import gradio as gr | |
| # Load Summarization Model | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
| def clean_text(text): | |
| text = re.sub(r'http[s]?://\S+', '', text) | |
| text = re.sub(r'www\.\S+', '', text) | |
| text = re.sub(r'\n+', ' ', text).strip() | |
| return text | |
| def extract_text_from_pdf(pdf_file): | |
| text = extract_text(pdf_file.name) | |
| return clean_text(text) | |
| def extract_text_from_image(image_file): | |
| image = Image.open(image_file) | |
| text = pytesseract.image_to_string(image) | |
| return clean_text(text) | |
| def chunk_text(text, chunk_size=1000): | |
| return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)] | |
| def summarize_text(text, max_length=200, min_length=50): | |
| chunks = chunk_text(text) | |
| summaries = [] | |
| for chunk in chunks: | |
| try: | |
| summary = summarizer(chunk, max_length=max_length, min_length=min_length, do_sample=False) | |
| summaries.append(summary[0]['summary_text']) | |
| except Exception as e: | |
| print(f"Error summarizing chunk: {e}") | |
| return " ".join(summaries) | |
| def gradio_handle_input(input_type, text_input, image_input, pdf_input): | |
| if input_type == "Text": | |
| text = text_input | |
| elif input_type == "Image": | |
| if image_input is None: | |
| return "", "Please upload an image." | |
| text = extract_text_from_image(image_input) | |
| elif input_type == "Pdf": | |
| if pdf_input is None: | |
| return "", "Please upload a PDF file." | |
| text = extract_text_from_pdf(pdf_input) | |
| else: | |
| return "", "Invalid input type." | |
| text = clean_text(text) | |
| if not text.strip(): | |
| return "", "Error: extracted text is empty." | |
| summary = summarize_text(text) | |
| return text, summary | |
| def toggle_inputs(choice): | |
| return ( | |
| gr.update(visible=choice == "Text", value=""), | |
| gr.update(visible=choice == "Image"), | |
| gr.update(visible=choice == "Pdf"), | |
| gr.update(value=""), # Clear preview if needed | |
| "", "" | |
| ) | |
| def download_summary(summary): | |
| if not summary.strip(): | |
| return gr.update(visible=False), None | |
| file_path = "summary.txt" | |
| with open(file_path, "w", encoding="utf-8") as f: | |
| f.write(summary) | |
| return gr.update(visible=True, value=file_path) | |
| with gr.Blocks(css="footer {visibility: hidden;}", title="Legal Document Summarization") as demo: | |
| gr.Markdown("## π Legal Document Summarization") | |
| gr.Markdown(""" | |
| ### π Steps to Use: | |
| 1. Choose input type (βοΈ Text / π PDF / πΌοΈ Image) | |
| 2. Enter your text or upload a file | |
| 3. Click **π Summarize** | |
| 4. View original input and βοΈ summary below | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| input_type = gr.Radio(choices=["Text", "Pdf", "Image"], label="Select Input Type", value="Text") | |
| text_input = gr.Textbox(label="βοΈ Enter Text", lines=15) | |
| image_input = gr.Image(label="π· Upload Image", visible=False, type="filepath") | |
| pdf_input = gr.File(label="π Upload PDF", file_types=[".pdf"], visible=False) | |
| submit_btn = gr.Button("π Summarize") | |
| with gr.Column(scale=1): | |
| gr.Markdown("β³ *Note: Summarization may take some time depending on file size and system performance.*") | |
| gr.Markdown("π **Summary Output**") | |
| output = gr.Textbox(label="", lines=15, interactive=False) | |
| download_btn = gr.Button("β¬οΈ Download Summary") | |
| file_output = gr.File(label="Download Summary", visible=False) | |
| gr.Markdown(""" | |
| > β οΈ **Disclaimer:** This tool provides automatic summarizations of legal documents **for educational purposes only.** | |
| > It does **not** constitute legal advice or professional consultation. | |
| > Please consult a qualified legal professional for any legal matters. | |
| """) | |
| input_type.change( | |
| fn=toggle_inputs, | |
| inputs=input_type, | |
| outputs=[text_input, image_input, pdf_input, output, text_input, output] | |
| ) | |
| submit_btn.click( | |
| fn=gradio_handle_input, | |
| inputs=[input_type, text_input, image_input, pdf_input], | |
| outputs=[text_input, output] | |
| ) | |
| download_btn.click( | |
| fn=download_summary, | |
| inputs=output, | |
| outputs=file_output | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |