Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("google/pegasus-xsum")
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-xsum")
|
| 7 |
+
|
| 8 |
+
def summarize(text):
|
| 9 |
+
# Tokenize input text
|
| 10 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
| 11 |
+
|
| 12 |
+
# Generate summary
|
| 13 |
+
summary_ids = model.generate(inputs["input_ids"])
|
| 14 |
+
|
| 15 |
+
# Decode and return the summary
|
| 16 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 17 |
+
return summary
|
| 18 |
+
|
| 19 |
+
# Create Gradio interface
|
| 20 |
+
demo = gr.Interface(
|
| 21 |
+
fn=summarize,
|
| 22 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter text to summarize...", label="Input Text"),
|
| 23 |
+
outputs=gr.Textbox(lines=5, label="Summary"),
|
| 24 |
+
title="Pegasus-XSum Text Summarizer",
|
| 25 |
+
description="Enter text and get an abstractive summary using Google's Pegasus-XSum model."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Launch the app
|
| 29 |
+
demo.launch()
|