Spaces:
Running
Running
| import re | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| MODEL = "facebook/bart-large-cnn" | |
| client = InferenceClient() | |
| def split_sentences(text): | |
| return [p.strip() for p in re.split(r"(?<=[.!?])\s+", text.strip()) if p.strip()] | |
| def chunk_text(text, max_words=600): | |
| chunks, cur, n = [], [], 0 | |
| for s in split_sentences(text): | |
| w = len(s.split()) | |
| if cur and n + w > max_words: | |
| chunks.append(" ".join(cur)); cur, n = [], 0 | |
| cur.append(s); n += w | |
| if cur: | |
| chunks.append(" ".join(cur)) | |
| return chunks | |
| def summarize_one(t): | |
| out = client.summarization(t, model=MODEL) | |
| return (getattr(out, "summary_text", None) or out["summary_text"]).strip() | |
| def run(text): | |
| text = (text or "").strip() | |
| if len(text.split()) < 30: | |
| return "Please paste a longer text (at least ~30 words)." | |
| try: | |
| parts = [summarize_one(c) for c in chunk_text(text)] | |
| combined = " ".join(parts) | |
| if len(parts) > 1 and len(combined.split()) > 130: | |
| combined = summarize_one(combined) | |
| return combined | |
| except Exception as e: | |
| return f"The summarisation service is busy or unavailable right now. Please try again in a moment.\n\n({e})" | |
| demo = gr.Interface( | |
| fn=run, | |
| inputs=gr.Textbox(lines=12, label="Paste a long text (article, report, notes)"), | |
| outputs=gr.Textbox(lines=6, label="Summary"), | |
| title="Text Summarizer", | |
| description="Abstractive summarisation (BART) via the Hugging Face Inference API. Long inputs are chunked, so it handles full articles.", | |
| article="Code: https://github.com/delcenjo/text-summarizer", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(ssr_mode=False) | |