PRSHNTKUMR commited on
Commit
9163e4e
·
verified ·
1 Parent(s): dc90d68

Create Text_Summary_app.py

Browse files
Files changed (1) hide show
  1. Text_Summary_app.py +30 -0
Text_Summary_app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from transformers import pipeline, AutoTokenizer
4
+
5
+ # Initialize summarizer and tokenizer
6
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer="sshleifer/distilbart-cnn-12-6")
7
+ tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
8
+
9
+ def summarize_text(input_text):
10
+ """Summarizes the given input text."""
11
+ max_length = tokenizer.model_max_length
12
+ inputs = tokenizer(input_text, truncation=True, max_length=max_length, return_tensors="pt")
13
+ summary_ids = summarizer.model.generate(inputs.input_ids, max_length=50, min_length=10, do_sample=False)
14
+ summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
15
+ return {"summary": summary_text}
16
+
17
+ def generate_summary(input):
18
+ output = summarize_text(input)
19
+ return output["summary"] # Return the summary directly
20
+
21
+ gr.close_all()
22
+ demo = gr.Interface(
23
+ fn=generate_summary,
24
+ inputs=[gr.Textbox(label="Text to summarize", lines=6)],
25
+ outputs=[gr.Textbox(label="Summary", lines=3)],
26
+ title="Text Summarization",
27
+ description="Summarize text using the 'shleifer/distilbart-cnn-12-6' language model.",
28
+ )
29
+
30
+ demo.launch()