Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import T5ForConditionalGeneration, T5TokenizerFast
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# ============================================================
|
| 6 |
+
# CONFIG
|
| 7 |
+
# ============================================================
|
| 8 |
+
MODEL_NAME = "t5-small"
|
| 9 |
+
WEIGHTS_PATH = "t5_weights.pt" # your uploaded weights file
|
| 10 |
+
|
| 11 |
+
# ============================================================
|
| 12 |
+
# LOAD MODEL & TOKENIZER
|
| 13 |
+
# ============================================================
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
tokenizer = T5TokenizerFast.from_pretrained(MODEL_NAME)
|
| 17 |
+
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 18 |
+
model.load_state_dict(torch.load(WEIGHTS_PATH, map_location=device))
|
| 19 |
+
model.to(device)
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
# ============================================================
|
| 23 |
+
# SUMMARIZATION FUNCTION
|
| 24 |
+
# ============================================================
|
| 25 |
+
def summarize(text):
|
| 26 |
+
if not text.strip():
|
| 27 |
+
return "⚠️ Please enter some text."
|
| 28 |
+
inputs = tokenizer(
|
| 29 |
+
"summarize: " + text,
|
| 30 |
+
return_tensors="pt",
|
| 31 |
+
truncation=True,
|
| 32 |
+
max_length=512,
|
| 33 |
+
padding="max_length"
|
| 34 |
+
).to(device)
|
| 35 |
+
|
| 36 |
+
summary_ids = model.generate(
|
| 37 |
+
**inputs,
|
| 38 |
+
max_new_tokens=150,
|
| 39 |
+
num_beams=4,
|
| 40 |
+
early_stopping=True,
|
| 41 |
+
no_repeat_ngram_size=3
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 45 |
+
|
| 46 |
+
# ============================================================
|
| 47 |
+
# GRADIO UI
|
| 48 |
+
# ============================================================
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=summarize,
|
| 51 |
+
inputs=gr.Textbox(lines=10, label="Enter News Article"),
|
| 52 |
+
outputs=gr.Textbox(lines=8, label="Generated Summary"),
|
| 53 |
+
title="📰 T5 News Summarizer",
|
| 54 |
+
description="Upload your own fine-tuned T5 model weights and summarize news articles easily!",
|
| 55 |
+
examples=[
|
| 56 |
+
["The US economy grew at an annual rate of 2.4% last quarter, surprising analysts..."],
|
| 57 |
+
["Scientists have discovered a new species of bird in the Amazon rainforest..."]
|
| 58 |
+
]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# ============================================================
|
| 62 |
+
# RUN APP
|
| 63 |
+
# ============================================================
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|