Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
+
model_name = "t5-small"
|
| 6 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 7 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Define the summarization function
|
| 10 |
+
def summarize_text(text):
|
| 11 |
+
input_text = "summarize: " + text.strip()
|
| 12 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 13 |
+
summary_ids = model.generate(input_ids, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 14 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 15 |
+
return summary
|
| 16 |
+
|
| 17 |
+
# Gradio interface
|
| 18 |
+
iface = gr.Interface(fn=summarize_text,
|
| 19 |
+
inputs=gr.Textbox(lines=15, placeholder="Paste your text here..."),
|
| 20 |
+
outputs=gr.Textbox(label="Summary"),
|
| 21 |
+
title="T5 Text Summarizer",
|
| 22 |
+
description="Enter any long English text to get a summarized version using the T5 model.")
|
| 23 |
+
|
| 24 |
+
# Launch
|
| 25 |
+
iface.launch()
|