Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,50 @@
|
|
| 1 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline, BitsAndBytesConfig
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from optimum.onnxruntime import ORTModelForSeq2SeqLM
|
| 3 |
+
from transformers import AutoTokenizer, pipeline
|
| 4 |
+
|
| 5 |
+
# Load ONNX model
|
| 6 |
+
def create_fast_summarizer():
|
| 7 |
+
model = ORTModelForSeq2SeqLM.from_pretrained(
|
| 8 |
+
"onnx-community/bart-large-cnn-ONNX",
|
| 9 |
+
encoder_file_name="encoder_model_q4.onnx",
|
| 10 |
+
decoder_file_name="decoder_model_q4.onnx",
|
| 11 |
+
provider="CPUExecutionProvider",
|
| 12 |
+
use_io_binding=True
|
| 13 |
+
)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 15 |
+
"onnx-community/bart-large-cnn-ONNX",
|
| 16 |
+
use_fast=True
|
| 17 |
+
)
|
| 18 |
+
return pipeline(
|
| 19 |
+
"summarization",
|
| 20 |
+
model=model,
|
| 21 |
+
tokenizer=tokenizer,
|
| 22 |
+
device=-1
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
summarizer = create_fast_summarizer()
|
| 26 |
+
|
| 27 |
+
# Summarize function with prompt + tuned params
|
| 28 |
+
def summarize_text(text):
|
| 29 |
+
prompt = "Summarize the key events, including casualties and political context:\n" + text
|
| 30 |
+
result = summarizer(
|
| 31 |
+
prompt,
|
| 32 |
+
max_length=160,
|
| 33 |
+
min_length=80,
|
| 34 |
+
do_sample=False,
|
| 35 |
+
num_beams=6,
|
| 36 |
+
length_penalty=1.5,
|
| 37 |
+
early_stopping=True
|
| 38 |
+
)
|
| 39 |
+
return result[0]['summary_text']
|
| 40 |
+
|
| 41 |
+
# Build Gradio interface
|
| 42 |
+
app = gr.Interface(
|
| 43 |
+
fn=summarize_text,
|
| 44 |
+
inputs=gr.Textbox(lines=15, placeholder="Paste your text here..."),
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="ONNX Summarizer 🚀",
|
| 47 |
+
description="Paste any news or article text and get a concise, context-rich summary."
|
| 48 |
)
|
| 49 |
|
| 50 |
+
app.launch()
|