anasmkh commited on
Commit
5ed8c5e
·
verified ·
1 Parent(s): 5a47af8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
+ import gradio as gr
3
+
4
+ # Load model and tokenizer
5
+ tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
7
+
8
+ def summarize_text(text):
9
+ # Tokenize the input text
10
+ inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
11
+
12
+ # Generate summary
13
+ summary_ids = model.generate(
14
+ inputs["input_ids"],
15
+ num_beams=4,
16
+ max_length=200,
17
+ early_stopping=True
18
+ )
19
+
20
+ # Decode and return the summary
21
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
22
+ return summary
23
+
24
+ # Create Gradio interface
25
+ interface = gr.Interface(
26
+ fn=summarize_text,
27
+ inputs=gr.Textbox(lines=10, placeholder="Paste your text here...", label="Input Text"),
28
+ outputs=gr.Textbox(lines=5, label="Summary"),
29
+ title="Text Summarizer with BART-large-CNN",
30
+ description="Paste any long text and get a concise summary using Facebook's BART-large-CNN model.",
31
+ examples=[
32
+ ["The Apollo program, also known as Project Apollo, was the third United States human spaceflight program carried out by the National Aeronautics and Space Administration (NASA), which succeeded in preparing and landing the first humans on the Moon from 1968 to 1972. It was first conceived during Dwight D. Eisenhower's administration as a three-person spacecraft to follow the one-person Project Mercury, which put the first Americans in space. Apollo was later dedicated to President John F. Kennedy's national goal of landing a man on the Moon and returning him safely to the Earth by the end of the 1960s, which he proposed in a May 25, 1961, address to Congress."]
33
+ ]
34
+ )
35
+
36
+ # Launch the app
37
+ interface.launch()