profplate commited on
Commit
a88fe4c
·
verified ·
1 Parent(s): d5f151a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ========================================
2
+ # TEXT SUMMARIZER WITH CONTROLS
3
+ # ========================================
4
+ # This Space summarizes long text into a short
5
+ # version. Unlike the text generator, this model
6
+ # CONDENSES — it reads everything and picks out
7
+ # the key points. Both are generative models:
8
+ # one creates from scratch, this one rewrites shorter.
9
+ #
10
+ # Hyperparameters (the controls):
11
+ # - Max length: how long the summary can be
12
+ # - Min length: how short it's allowed to be
13
+ # ========================================
14
+
15
+ import gradio as gr
16
+ from transformers import pipeline
17
+
18
+ # Distilled summarization model — works on free CPU
19
+ summarizer = pipeline(
20
+ "summarization",
21
+ model="sshleifer/distilbart-cnn-6-6",
22
+ )
23
+
24
+
25
+ def summarize(text, max_length, min_length):
26
+ if not text or not text.strip():
27
+ return "Paste some text above to summarize!"
28
+
29
+ word_count = len(text.split())
30
+ if word_count < 30:
31
+ return "The text is too short to summarize — try pasting something longer (at least a paragraph)."
32
+
33
+ # Make sure min doesn't exceed max
34
+ min_length = min(min_length, max_length - 10)
35
+ if min_length < 10:
36
+ min_length = 10
37
+
38
+ result = summarizer(
39
+ text[:1024],
40
+ max_length=max_length,
41
+ min_length=min_length,
42
+ do_sample=False,
43
+ )
44
+
45
+ summary = result[0]["summary_text"]
46
+ summary_words = len(summary.split())
47
+
48
+ return (
49
+ f"{summary}\n\n"
50
+ f"---\n"
51
+ f"Original: {word_count} words → Summary: {summary_words} words "
52
+ f"({summary_words / word_count:.0%} of original)"
53
+ )
54
+
55
+
56
+ demo = gr.Interface(
57
+ fn=summarize,
58
+ inputs=[
59
+ gr.Textbox(
60
+ lines=10,
61
+ placeholder="Paste an article, essay, or long text here...",
62
+ label="Text to Summarize",
63
+ ),
64
+ gr.Slider(
65
+ minimum=30,
66
+ maximum=200,
67
+ value=100,
68
+ step=10,
69
+ label="Max Summary Length (tokens)",
70
+ ),
71
+ gr.Slider(
72
+ minimum=10,
73
+ maximum=100,
74
+ value=25,
75
+ step=5,
76
+ label="Min Summary Length (tokens)",
77
+ ),
78
+ ],
79
+ outputs=gr.Textbox(label="Summary", lines=6),
80
+ title="Quick Summarizer",
81
+ description=(
82
+ "Paste a long article or essay and get a short summary. "
83
+ "Use the sliders to control how long or short the summary is. "
84
+ "The AI reads the whole thing and picks out the key points."
85
+ ),
86
+ examples=[
87
+ [
88
+ "Artificial intelligence has transformed many industries over the past decade. "
89
+ "In healthcare, AI systems can now detect diseases from medical images with "
90
+ "accuracy rivaling human doctors. In finance, algorithmic trading powered by "
91
+ "machine learning processes millions of transactions per second. Education is "
92
+ "also being reshaped, with AI tutors providing personalized learning experiences "
93
+ "for students around the world. However, these advances come with significant "
94
+ "challenges. Privacy concerns arise when AI systems require vast amounts of "
95
+ "personal data. Job displacement remains a worry as automation replaces routine "
96
+ "tasks. And bias in AI systems can perpetuate or even amplify existing social "
97
+ "inequalities. Addressing these challenges while harnessing AI's potential will "
98
+ "be one of the defining tasks of our generation.",
99
+ 100,
100
+ 25,
101
+ ],
102
+ [
103
+ "The patient presented to the emergency department with acute onset of "
104
+ "substernal chest pain radiating to the left arm, accompanied by diaphoresis "
105
+ "and shortness of breath. Initial ECG showed ST-segment elevation in leads "
106
+ "II, III, and aVF, consistent with inferior myocardial infarction. Troponin "
107
+ "levels were elevated at 2.4 ng/mL. The patient was started on dual "
108
+ "antiplatelet therapy and heparin infusion, and cardiology was consulted for "
109
+ "emergent cardiac catheterization. Past medical history is significant for "
110
+ "hypertension, type 2 diabetes mellitus, and hyperlipidemia. The patient "
111
+ "reports a 30-pack-year smoking history.",
112
+ 60,
113
+ 20,
114
+ ],
115
+ [
116
+ "The Legend of Zelda series has captivated gamers for nearly four decades "
117
+ "with its unique blend of exploration, puzzle-solving, and combat. Starting "
118
+ "with the original 1986 NES title, the franchise established a template for "
119
+ "action-adventure games that continues to influence game design today. Each "
120
+ "entry reimagines the core formula while maintaining the series' identity: "
121
+ "a young hero named Link must rescue Princess Zelda and defeat the villain "
122
+ "Ganondorf across a sprawling fantasy world. The 2017 release of Breath of "
123
+ "the Wild revolutionized open-world game design by giving players complete "
124
+ "freedom to explore Hyrule in any order, solving puzzles with emergent physics "
125
+ "systems rather than predetermined solutions. Its 2023 sequel, Tears of the "
126
+ "Kingdom, expanded on this foundation by adding the ability to build and "
127
+ "combine objects, creating an unprecedented sandbox within a narrative-driven "
128
+ "adventure game.",
129
+ 100,
130
+ 25,
131
+ ],
132
+ ],
133
+ )
134
+
135
+ demo.launch()