Upload folder using huggingface_hub
Browse files- README.md +13 -5
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +95 -28
- requirements.txt +3 -5
README.md
CHANGED
|
@@ -1,13 +1,21 @@
|
|
| 1 |
---
|
| 2 |
-
title: Text Summarizer
|
|
|
|
| 3 |
colorFrom: blue
|
| 4 |
-
colorTo:
|
| 5 |
sdk: gradio
|
|
|
|
| 6 |
app_file: app.py
|
| 7 |
pinned: false
|
| 8 |
-
license:
|
| 9 |
---
|
| 10 |
|
| 11 |
-
# Text Summarizer
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AI Smart Text Summarizer
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Smart AI Text Summarizer & Key Insights
|
| 14 |
|
| 15 |
+
An intelligent NLP application built with Gradio and Hugging Face Transformers (`sshleifer/distilbart-cnn-12-6`) to summarize long documents and extract key insights instantly.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
- Abstractive AI summarization
|
| 19 |
+
- Adjustable length controls
|
| 20 |
+
- Automated key highlights extraction
|
| 21 |
+
- Real-time word count & reading time reduction analytics
|
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (7.06 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,44 +1,111 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
return service.summarize(text, style, int(max_words))
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
) as demo:
|
| 17 |
gr.Markdown(
|
| 18 |
"""
|
| 19 |
-
# Text Summarizer
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
if __name__ == "__main__":
|
| 44 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load lightweight, fast summarization pipeline
|
| 6 |
+
try:
|
| 7 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 8 |
+
except Exception as e:
|
| 9 |
+
summarizer = None
|
| 10 |
+
print(f"Pipeline loading error: {e}")
|
| 11 |
|
| 12 |
+
def extract_key_points(text, num_points=3):
|
| 13 |
+
"""Extract key sentence points based on sentence length & structure."""
|
| 14 |
+
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 10]
|
| 15 |
+
if not sentences:
|
| 16 |
+
return "β’ No key sentences identified."
|
| 17 |
+
# Sort sentences by length/informativeness as simple heuristic
|
| 18 |
+
key_sentences = sorted(sentences, key=lambda s: len(s), reverse=True)[:num_points]
|
| 19 |
+
return "\n".join([f"β’ {s}" for s in key_sentences])
|
| 20 |
|
| 21 |
+
def analyze_and_summarize(text, max_len, min_len):
|
| 22 |
+
if not text or len(text.strip()) < 30:
|
| 23 |
+
return (
|
| 24 |
+
"β οΈ Please enter a longer text (at least 30 characters) to summarize.",
|
| 25 |
+
"N/A",
|
| 26 |
+
"N/A",
|
| 27 |
+
"N/A"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
words_input = len(text.split())
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
if summarizer:
|
| 34 |
+
summary_res = summarizer(
|
| 35 |
+
text,
|
| 36 |
+
max_length=int(max_len),
|
| 37 |
+
min_length=int(min_len),
|
| 38 |
+
do_sample=False
|
| 39 |
+
)
|
| 40 |
+
summary_text = summary_res[0]['summary_text']
|
| 41 |
+
else:
|
| 42 |
+
# Fallback heuristic summary if pipeline fails to load
|
| 43 |
+
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5]
|
| 44 |
+
summary_text = " ".join(sentences[:max(1, len(sentences)//2)])
|
| 45 |
+
except Exception as err:
|
| 46 |
+
# Fallback if text is shorter than min_length or another edge case occurs
|
| 47 |
+
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5]
|
| 48 |
+
summary_text = " ".join(sentences[:2]) if sentences else text
|
| 49 |
|
| 50 |
+
words_summary = len(summary_text.split())
|
| 51 |
+
reduction = max(0, round((1 - (words_summary / words_input)) * 100, 1)) if words_input > 0 else 0
|
| 52 |
+
read_time_saved = max(0, round((words_input - words_summary) / 200, 1))
|
| 53 |
+
|
| 54 |
+
key_bullets = extract_key_points(text)
|
| 55 |
+
|
| 56 |
+
stats_md = f"""
|
| 57 |
+
### π Summary Analytics
|
| 58 |
+
- **Original Word Count**: `{words_input}` words
|
| 59 |
+
- **Summary Word Count**: `{words_summary}` words
|
| 60 |
+
- **Text Reduction**: `{reduction}%` smaller
|
| 61 |
+
- **Est. Reading Time Saved**: `{read_time_saved} minutes`
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
return summary_text, key_bullets, stats_md
|
| 65 |
|
| 66 |
+
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."""
|
|
|
|
| 67 |
|
| 68 |
+
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."""
|
| 69 |
|
| 70 |
+
demo = gr.Blocks(theme=gr.themes.Soft())
|
| 71 |
+
|
| 72 |
+
with demo:
|
|
|
|
| 73 |
gr.Markdown(
|
| 74 |
"""
|
| 75 |
+
# π Smart AI Text Summarizer & Insight Extractor
|
| 76 |
+
*Transform lengthy articles, essays, and documents into concise, highly readable summaries powered by Deep Learning.*
|
| 77 |
"""
|
| 78 |
)
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column(scale=1):
|
| 82 |
+
input_text = gr.Textbox(
|
| 83 |
+
label="Input Document / Article Text",
|
| 84 |
+
placeholder="Paste your paragraph or article here...",
|
| 85 |
+
lines=10
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
max_slider = gr.Slider(minimum=30, maximum=300, value=130, step=10, label="Max Summary Length")
|
| 90 |
+
min_slider = gr.Slider(minimum=10, maximum=100, value=30, step=5, label="Min Summary Length")
|
| 91 |
+
|
| 92 |
+
submit_btn = gr.Button("β‘ Summarize & Extract Insights", variant="primary")
|
| 93 |
+
|
| 94 |
+
gr.Examples(
|
| 95 |
+
examples=[[example_1, 130, 30], [example_2, 100, 25]],
|
| 96 |
+
inputs=[input_text, max_slider, min_slider]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
output_summary = gr.Textbox(label="β¨ AI Abstractive Summary", lines=5)
|
| 101 |
+
output_bullets = gr.Textbox(label="π Key Highlight Points", lines=4)
|
| 102 |
+
output_stats = gr.Markdown(label="Analytics")
|
| 103 |
+
|
| 104 |
+
submit_btn.click(
|
| 105 |
+
fn=analyze_and_summarize,
|
| 106 |
+
inputs=[input_text, max_slider, min_slider],
|
| 107 |
+
outputs=[output_summary, output_bullets, output_stats]
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
torch>=2.3.0
|
| 5 |
-
transformers>=4.49.0
|
|
|
|
| 1 |
+
transformers>=4.30.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
gradio>=4.0.0
|
|
|
|
|
|