vangru commited on
Commit
8ccb87b
·
verified ·
1 Parent(s): f103160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -35
app.py CHANGED
@@ -1,43 +1,33 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
 
5
- model_name = "facebook/bart-large-cnn"
6
-
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
-
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
- model = model.to(device)
12
 
13
  def summarize_text(text):
14
  if not text.strip():
15
- return "Please enter some text."
16
-
17
- inputs = tokenizer(
18
  text,
19
- return_tensors="pt",
20
- truncation=True,
21
- max_length=1024
22
- ).to(device)
23
-
24
- summary_ids = model.generate(
25
- inputs["input_ids"],
26
  max_length=150,
27
- min_length=50,
28
- num_beams=4,
29
- early_stopping=True
30
  )
31
-
32
- return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
33
-
34
- with gr.Blocks() as demo:
35
- gr.Markdown("# 📄 BART Summarizer")
36
-
37
- text_input = gr.Textbox(lines=10, label="Enter Text")
38
- btn = gr.Button("Generate Summary")
39
- output = gr.Textbox(label="Summary", lines=6)
40
-
41
- btn.click(summarize_text, inputs=text_input, outputs=output)
42
-
43
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Load summarization pipeline
5
+ summarizer = pipeline(
6
+ "summarization",
7
+ model="facebook/bart-large-cnn"
8
+ )
 
 
9
 
10
  def summarize_text(text):
11
  if not text.strip():
12
+ return "Please enter some text to summarize."
13
+
14
+ summary = summarizer(
15
  text,
 
 
 
 
 
 
 
16
  max_length=150,
17
+ min_length=40,
18
+ do_sample=False
 
19
  )
20
+
21
+ return summary[0]['summary_text']
22
+
23
+ # Gradio Interface
24
+ demo = gr.Interface(
25
+ fn=summarize_text,
26
+ inputs=gr.Textbox(lines=15, placeholder="Paste your text here..."),
27
+ outputs="text",
28
+ title="Advanced BART Text Summarizer",
29
+ description="Summarize long text using facebook/bart-large-cnn"
30
+ )
31
+
32
+ if __name__ == "__main__":
33
+ demo.launch()