Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from transformers import T5ForConditionalGeneration, T5Tokenizer | |
| MODEL_NAME = "t5-small" | |
| print("Loading model...") | |
| tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME, legacy=False) | |
| model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME) | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model.to(device) | |
| model.eval() | |
| print(f"Model loaded on {device}!") | |
| def simplify_legal_text(legal_text, max_length=512, num_beams=4): | |
| if not legal_text or not legal_text.strip(): | |
| return "Please enter some legal text to simplify." | |
| if len(legal_text) > 5000: | |
| return "Text too long! Please keep input under 5,000 characters." | |
| try: | |
| input_text = f"summarize: {legal_text}" | |
| # FIXED: Use tokenizer as callable | |
| encoded = tokenizer( | |
| input_text, | |
| max_length=1024, | |
| truncation=True, | |
| return_tensors="pt" | |
| ) | |
| inputs = encoded.input_ids.to(device) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| inputs, | |
| max_length=max_length, | |
| num_beams=num_beams, | |
| early_stopping=True, | |
| do_sample=False, | |
| repetition_penalty=2.5, | |
| length_penalty=1.0, | |
| no_repeat_ngram_size=3 | |
| ) | |
| simplified_text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return simplified_text | |
| except Exception as e: | |
| return f"Error: {str(e)}. Please try again with shorter text." | |
| # Create Gradio interface | |
| with gr.Blocks(title="Legal Text Simplifier", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| """ | |
| # ⚖️ Legal Text Simplifier | |
| Transform complex legal language into simple, easy-to-understand text. | |
| **How to use:** | |
| 1. Paste your legal text in the input box | |
| 2. Adjust settings if needed (optional) | |
| 3. Click "Simplify" to get your simplified version | |
| **Tips:** | |
| - Works best with paragraphs or short documents | |
| - For very long texts, break them into smaller sections | |
| - The model uses AI to preserve meaning while simplifying language | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| legal_input = gr.Textbox( | |
| label="📝 Legal Text (Paste your complex legal text here)", | |
| placeholder="Enter legal text to simplify...", | |
| lines=10, | |
| value="The party of the first part hereby agrees to indemnify and hold harmless the party of the second part from any and all claims, damages, losses, costs, and expenses..." | |
| ) | |
| with gr.Row(): | |
| simplify_btn = gr.Button("✨ Simplify Text", variant="primary", size="lg") | |
| clear_btn = gr.Button("🗑️ Clear", size="lg") | |
| with gr.Column(scale=1): | |
| gr.Markdown("### ⚙️ Advanced Settings") | |
| max_length = gr.Slider( | |
| minimum=100, | |
| maximum=1000, | |
| value=512, | |
| step=50, | |
| label="Max Output Length", | |
| info="Longer = more detailed, but may be slower" | |
| ) | |
| num_beams = gr.Slider( | |
| minimum=1, | |
| maximum=8, | |
| value=4, | |
| step=1, | |
| label="Quality (Beam Search)", | |
| info="Higher = better quality, slower generation" | |
| ) | |
| simplified_output = gr.Textbox( | |
| label="✨ Simplified Text", | |
| lines=10, | |
| interactive=False, | |
| placeholder="Your simplified text will appear here..." | |
| ) | |
| # Connect the function to the interface | |
| simplify_btn.click( | |
| fn=simplify_legal_text, | |
| inputs=[legal_input, max_length, num_beams], | |
| outputs=simplified_output | |
| ) | |
| clear_btn.click( | |
| fn=lambda: ("", ""), | |
| outputs=[legal_input, simplified_output] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |