Spaces:
Build error
Build error
File size: 4,525 Bytes
cd6b461 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 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()
|