# app.py — Gradio App for Text Summarization import gradio as gr from transformers import AutoTokenizer, AutoModelForSeq2SeqLM import torch MODEL_ID = "samandar1105/text-summarizer" PREFIX = "summarize: " MAX_INPUT_LENGTH = 512 # ... (paste the rest of the app.py code exactly as shown in Phase 5) ... # app.py — Gradio App for Text Summarization import gradio as gr from transformers import AutoTokenizer, AutoModelForSeq2SeqLM import torch # ============================================================ # CONFIGURATION — Update this to your model! # ============================================================ MODEL_ID = "samandar1105/text-summarizer" # ← CHANGE THIS PREFIX = "summarize: " # T5 needs this prefix MAX_INPUT_LENGTH = 512 EXAMPLES = [ ["""NASA's Perseverance rover has collected its most compelling sample yet in the search for ancient life on Mars, scientists announced Wednesday. The rock sample, nicknamed "Cheyava Falls," contains chemical signatures and structures that could be consistent with microbial life billions of years ago, though researchers caution that non-biological explanations have not been ruled out. The sample will eventually be returned to Earth for detailed laboratory analysis as part of the Mars Sample Return mission, currently planned for the early 2030s."""], ["""The Federal Reserve held interest rates steady at its policy meeting on Wednesday, extending a pause that has lasted several months as officials continue to monitor inflation data. In a statement, the central bank said recent indicators suggest the economy is expanding at a solid pace, while inflation has eased over the past year but remains somewhat elevated relative to the 2% target. Markets had widely expected the decision, and futures pricing suggests investors are looking to the next meeting for signs of a potential rate cut."""], ] # We load the tokenizer/model directly and call .generate() ourselves instead of # using pipeline("summarization", ...). Some environments have a broken or # mismatched pipeline task registry that raises "KeyError: Unknown task # summarization" even when transformers is otherwise installed correctly — this # approach sidesteps that entirely and also gives us direct control over beam # search and repetition settings. DEVICE = "cuda" if torch.cuda.is_available() else "cpu" print(f"Loading model: {MODEL_ID} on {DEVICE}") tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID).to(DEVICE) print("Model loaded successfully!") def summarize_text(text: str, max_len: int, min_len: int): if not text or text.strip() == "": return "⚠️ Please paste some text to summarize." if len(text.strip().split()) < 15: return "⚠️ Please enter a longer passage (at least ~15 words) for a meaningful summary." inputs = tokenizer( PREFIX + text.strip(), return_tensors="pt", truncation=True, max_length=MAX_INPUT_LENGTH, ).to(DEVICE) output_ids = model.generate( **inputs, max_length=int(max_len), min_length=int(min_len), num_beams=4, no_repeat_ngram_size=3, ) return tokenizer.decode(output_ids[0], skip_special_tokens=True) with gr.Blocks(title="📝 AI Text Summarizer", theme=gr.themes.Soft()) as demo: gr.Markdown(""" # 📝 AI Text Summarizer Paste any article, report, or long passage below and get a concise AI-generated summary. Built with `t5-small` fine-tuned on the CNN/DailyMail dataset. """) with gr.Row(): with gr.Column(scale=2): input_text = gr.Textbox( label="📄 Text to summarize", placeholder="Paste your article or long text here...", lines=12, ) with gr.Row(): max_len_slider = gr.Slider(30, 200, value=120, step=10, label="Max summary length") min_len_slider = gr.Slider(5, 60, value=20, step=5, label="Min summary length") with gr.Row(): submit_btn = gr.Button("✨ Summarize", variant="primary", scale=2) clear_btn = gr.ClearButton([input_text], scale=1) with gr.Column(scale=2): output_text = gr.Textbox(label="📌 Summary", lines=8) gr.Examples(examples=EXAMPLES, inputs=input_text, label="📌 Try an example") gr.Markdown("---\n**Model:** `t5-small` fine-tuned on CNN/DailyMail") submit_btn.click( fn=summarize_text, inputs=[input_text, max_len_slider, min_len_slider], outputs=[output_text], ) input_text.submit( fn=summarize_text, inputs=[input_text, max_len_slider, min_len_slider], outputs=[output_text], ) if __name__ == "__main__": demo.launch(server_port=7860)