Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,38 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
model =
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
|
| 9 |
-
# Create
|
| 10 |
-
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
|
| 11 |
|
| 12 |
-
# Define
|
| 13 |
def summarize_text(text):
|
| 14 |
-
if
|
| 15 |
return "Please enter some text."
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Gradio
|
| 20 |
app = gr.Interface(
|
| 21 |
fn=summarize_text,
|
| 22 |
inputs=gr.Textbox(lines=15, placeholder="Paste your text here...", label="Input Text"),
|
| 23 |
outputs=gr.Textbox(label="Summary"),
|
| 24 |
-
title="
|
| 25 |
-
description="Summarize long
|
| 26 |
)
|
| 27 |
|
| 28 |
# Launch the app
|
|
|
|
| 1 |
+
# Required: pip install gradio transformers accelerate optimum onnxruntime onnx
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
from optimum.onnxruntime import ORTModelForSeq2SeqLM
|
| 6 |
+
from optimum.pipelines import pipeline
|
| 7 |
|
| 8 |
+
# Load ONNX model and tokenizer
|
| 9 |
+
model_name = "Rahmat82/t5-small-finetuned-summarization-xsum"
|
| 10 |
+
model = ORTModelForSeq2SeqLM.from_pretrained(model_name, export=True)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 12 |
|
| 13 |
+
# Create summarizer pipeline with Optimum
|
| 14 |
+
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer, device_map="auto", batch_size=12)
|
| 15 |
|
| 16 |
+
# Define summarization function with 1024 token cap
|
| 17 |
def summarize_text(text):
|
| 18 |
+
if not text.strip():
|
| 19 |
return "Please enter some text."
|
| 20 |
+
|
| 21 |
+
# Tokenize and truncate to max 1024 tokens
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
| 23 |
+
input_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
|
| 24 |
+
|
| 25 |
+
# Summarize the truncated text
|
| 26 |
+
result = summarizer(input_text)
|
| 27 |
+
return result[0]["summary_text"]
|
| 28 |
|
| 29 |
+
# Gradio app
|
| 30 |
app = gr.Interface(
|
| 31 |
fn=summarize_text,
|
| 32 |
inputs=gr.Textbox(lines=15, placeholder="Paste your text here...", label="Input Text"),
|
| 33 |
outputs=gr.Textbox(label="Summary"),
|
| 34 |
+
title="🚀 ONNX-Powered T5 Summarizer (1024 tokens)",
|
| 35 |
+
description="Summarize long text using a fine-tuned ONNX-accelerated T5-small model (max input: 1024 tokens)"
|
| 36 |
)
|
| 37 |
|
| 38 |
# Launch the app
|