Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,29 +2,29 @@ import gradio as gr
|
|
| 2 |
import re
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
# Load
|
|
|
|
| 6 |
try:
|
| 7 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 8 |
-
except Exception
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 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()) <
|
| 23 |
return (
|
| 24 |
-
"⚠️ Please enter a longer text (at least
|
| 25 |
-
"N/A",
|
| 26 |
"N/A",
|
| 27 |
-
"
|
| 28 |
)
|
| 29 |
|
| 30 |
words_input = len(text.split())
|
|
@@ -37,15 +37,16 @@ def analyze_and_summarize(text, max_len, min_len):
|
|
| 37 |
min_length=int(min_len),
|
| 38 |
do_sample=False
|
| 39 |
)
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
|
@@ -54,12 +55,12 @@ def analyze_and_summarize(text, max_len, min_len):
|
|
| 54 |
key_bullets = extract_key_points(text)
|
| 55 |
|
| 56 |
stats_md = f"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
return summary_text, key_bullets, stats_md
|
| 65 |
|
|
@@ -67,7 +68,7 @@ example_1 = """Artificial intelligence (AI) is transforming industries worldwide
|
|
| 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(
|
| 71 |
|
| 72 |
with demo:
|
| 73 |
gr.Markdown(
|
|
|
|
| 2 |
import re
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load summarization pipeline safely
|
| 6 |
+
summarizer = None
|
| 7 |
try:
|
| 8 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 9 |
+
except Exception:
|
| 10 |
+
try:
|
| 11 |
+
summarizer = pipeline("text2text-generation", model="sshleifer/distilbart-cnn-12-6")
|
| 12 |
+
except Exception as e:
|
| 13 |
+
print(f"Pipeline error: {e}")
|
| 14 |
|
| 15 |
def extract_key_points(text, num_points=3):
|
|
|
|
| 16 |
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 10]
|
| 17 |
if not sentences:
|
| 18 |
return "• No key sentences identified."
|
|
|
|
| 19 |
key_sentences = sorted(sentences, key=lambda s: len(s), reverse=True)[:num_points]
|
| 20 |
return "\n".join([f"• {s}" for s in key_sentences])
|
| 21 |
|
| 22 |
def analyze_and_summarize(text, max_len, min_len):
|
| 23 |
+
if not text or len(text.strip()) < 20:
|
| 24 |
return (
|
| 25 |
+
"⚠️ Please enter a longer text (at least 20 characters) to summarize.",
|
|
|
|
| 26 |
"N/A",
|
| 27 |
+
"### 📊 Summary Analytics\n- Please provide input text."
|
| 28 |
)
|
| 29 |
|
| 30 |
words_input = len(text.split())
|
|
|
|
| 37 |
min_length=int(min_len),
|
| 38 |
do_sample=False
|
| 39 |
)
|
| 40 |
+
if isinstance(summary_res, list) and len(summary_res) > 0:
|
| 41 |
+
summary_text = summary_res[0].get('summary_text') or summary_res[0].get('generated_text') or str(summary_res[0])
|
| 42 |
+
else:
|
| 43 |
+
summary_text = str(summary_res)
|
| 44 |
else:
|
|
|
|
| 45 |
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5]
|
| 46 |
summary_text = " ".join(sentences[:max(1, len(sentences)//2)])
|
| 47 |
+
except Exception:
|
|
|
|
| 48 |
sentences = [s.strip() for s in re.split(r'(?<=[.!?]) +', text) if len(s.strip()) > 5]
|
| 49 |
+
summary_text = " ".join(sentences[:2]) if len(sentences) >= 2 else text
|
| 50 |
|
| 51 |
words_summary = len(summary_text.split())
|
| 52 |
reduction = max(0, round((1 - (words_summary / words_input)) * 100, 1)) if words_input > 0 else 0
|
|
|
|
| 55 |
key_bullets = extract_key_points(text)
|
| 56 |
|
| 57 |
stats_md = f"""
|
| 58 |
+
### 📊 Summary Analytics
|
| 59 |
+
- **Original Word Count**: `{words_input}` words
|
| 60 |
+
- **Summary Word Count**: `{words_summary}` words
|
| 61 |
+
- **Text Reduction**: `{reduction}%` smaller
|
| 62 |
+
- **Est. Reading Time Saved**: `{read_time_saved} minutes`
|
| 63 |
+
"""
|
| 64 |
|
| 65 |
return summary_text, key_bullets, stats_md
|
| 66 |
|
|
|
|
| 68 |
|
| 69 |
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."""
|
| 70 |
|
| 71 |
+
demo = gr.Blocks()
|
| 72 |
|
| 73 |
with demo:
|
| 74 |
gr.Markdown(
|