Download app.py from abersbail/text-summarizer-cpu: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/abersbail/text-summarizer-cpu/resolve/d4b7b198bd04e386d121e50fbf020b85c3465ea9/app.py
- Command line
-
hf download hf://spaces/abersbail/text-summarizer-cpu@d4b7b198bd04e386d121e50fbf020b85c3465ea9/app.py
-
curl -L -o app.py https://huggingface.co/spaces/abersbail/text-summarizer-cpu/resolve/d4b7b198bd04e386d121e50fbf020b85c3465ea9/app.py
5.14 kB
| import gradio as gr | |
| import re | |
| from transformers import pipeline | |
| # Load lightweight, fast summarization pipeline | |
| try: | |
| summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
| except Exception as e: | |
| summarizer = None | |
| print(f"Pipeline loading error: {e}") | |
| def extract_key_points(text, num_points=3): | |
| """Extract key sentence points based on sentence length & structure.""" | |
| sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 10] | |
| if not sentences: | |
| return "β’ No key sentences identified." | |
| # Sort sentences by length/informativeness as simple heuristic | |
| key_sentences = sorted(sentences, key=lambda s: len(s), reverse=True)[:num_points] | |
| return "\n".join([f"β’ {s}" for s in key_sentences]) | |
| def analyze_and_summarize(text, max_len, min_len): | |
| if not text or len(text.strip()) < 30: | |
| return ( | |
| "β οΈ Please enter a longer text (at least 30 characters) to summarize.", | |
| "N/A", | |
| "N/A", | |
| "N/A" | |
| ) | |
| words_input = len(text.split()) | |
| try: | |
| if summarizer: | |
| summary_res = summarizer( | |
| text, | |
| max_length=int(max_len), | |
| min_length=int(min_len), | |
| do_sample=False | |
| ) | |
| summary_text = summary_res[0]['summary_text'] | |
| else: | |
| # Fallback heuristic summary if pipeline fails to load | |
| sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5] | |
| summary_text = " ".join(sentences[:max(1, len(sentences)//2)]) | |
| except Exception as err: | |
| # Fallback if text is shorter than min_length or another edge case occurs | |
| sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5] | |
| summary_text = " ".join(sentences[:2]) if sentences else text | |
| words_summary = len(summary_text.split()) | |
| reduction = max(0, round((1 - (words_summary / words_input)) * 100, 1)) if words_input > 0 else 0 | |
| read_time_saved = max(0, round((words_input - words_summary) / 200, 1)) | |
| key_bullets = extract_key_points(text) | |
| stats_md = f""" | |
| ### π Summary Analytics | |
| - **Original Word Count**: `{words_input}` words | |
| - **Summary Word Count**: `{words_summary}` words | |
| - **Text Reduction**: `{reduction}%` smaller | |
| - **Est. Reading Time Saved**: `{read_time_saved} minutes` | |
| """ | |
| return summary_text, key_bullets, stats_md | |
| example_1 = """Artificial intelligence (AI) is transforming industries worldwide, from healthcare and finance to education and transport. Deep learning models, powered by neural networks with millions or billions of parameters, have achieved unprecedented capabilities in natural language understanding, computer vision, and autonomous decision making. As these technologies evolve, researchers emphasize the importance of AI safety, ethics, and transparency to ensure AI systems remain aligned with human values and societal benefit.""" | |
| example_2 = """Machine learning algorithms build a mathematical model based on sample data, known as training data, to make predictions or decisions without being explicitly programmed to do so. Supervised learning algorithms build a mathematical model of a set of data that contains both the inputs and the desired outputs. Unsupervised learning algorithms take a set of data that contains only inputs, and find structure in the data, like grouping or clustering of data points.""" | |
| demo = gr.Blocks(theme=gr.themes.Soft()) | |
| with demo: | |
| gr.Markdown( | |
| """ | |
| # π Smart AI Text Summarizer & Insight Extractor | |
| *Transform lengthy articles, essays, and documents into concise, highly readable summaries powered by Deep Learning.* | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| input_text = gr.Textbox( | |
| label="Input Document / Article Text", | |
| placeholder="Paste your paragraph or article here...", | |
| lines=10 | |
| ) | |
| with gr.Row(): | |
| max_slider = gr.Slider(minimum=30, maximum=300, value=130, step=10, label="Max Summary Length") | |
| min_slider = gr.Slider(minimum=10, maximum=100, value=30, step=5, label="Min Summary Length") | |
| submit_btn = gr.Button("β‘ Summarize & Extract Insights", variant="primary") | |
| gr.Examples( | |
| examples=[[example_1, 130, 30], [example_2, 100, 25]], | |
| inputs=[input_text, max_slider, min_slider] | |
| ) | |
| with gr.Column(scale=1): | |
| output_summary = gr.Textbox(label="β¨ AI Abstractive Summary", lines=5) | |
| output_bullets = gr.Textbox(label="π Key Highlight Points", lines=4) | |
| output_stats = gr.Markdown(label="Analytics") | |
| submit_btn.click( | |
| fn=analyze_and_summarize, | |
| inputs=[input_text, max_slider, min_slider], | |
| outputs=[output_summary, output_bullets, output_stats] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |