Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import BartForConditionalGeneration, BartTokenizer
|
3 |
+
|
4 |
+
# Load the model
|
5 |
+
model_name = "philschmid/bart-large-cnn-samsum"
|
6 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
7 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Summary length options
|
10 |
+
length_options = {
|
11 |
+
"Short": (30, 50),
|
12 |
+
"Medium": (50, 100),
|
13 |
+
"Long": (100, 150)
|
14 |
+
}
|
15 |
+
|
16 |
+
# Function to process summarization
|
17 |
+
def summarize_text(text, summary_length):
|
18 |
+
min_len, max_len = length_options.get(summary_length, (50, 100)) # Default to Medium
|
19 |
+
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
21 |
+
summary_ids = model.generate(
|
22 |
+
inputs["input_ids"],
|
23 |
+
max_length=max_len,
|
24 |
+
min_length=min_len,
|
25 |
+
length_penalty=1.0,
|
26 |
+
num_beams=6,
|
27 |
+
repetition_penalty=1.2
|
28 |
+
)
|
29 |
+
|
30 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
31 |
+
|
32 |
+
# Gradio Interface
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown("# AI-Powered Summarizer ✨")
|
35 |
+
|
36 |
+
with gr.Row():
|
37 |
+
text_input = gr.Textbox(label="Enter Text to Summarize", lines=8, placeholder="Paste your text here...")
|
38 |
+
|
39 |
+
summary_length = gr.Radio(["Short", "Medium", "Long"], value="Medium", label="Summary Length")
|
40 |
+
|
41 |
+
summarize_button = gr.Button("Summarize")
|
42 |
+
|
43 |
+
output_text = gr.Textbox(label="Summary", lines=6)
|
44 |
+
|
45 |
+
summarize_button.click(summarize
|