Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,41 @@
|
|
| 1 |
-
import
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
device = -1
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
return output[0]['summary_text']
|
| 26 |
|
| 27 |
gr.close_all()
|
| 28 |
|
|
|
|
| 29 |
demo = gr.Interface(
|
| 30 |
-
fn=
|
| 31 |
inputs=[gr.Textbox(label="Input text to summarize", lines=6)],
|
| 32 |
outputs=[gr.Textbox(label="Summarized text", lines=4)],
|
| 33 |
-
title="
|
| 34 |
-
description="
|
| 35 |
)
|
|
|
|
| 36 |
demo.launch()
|
|
|
|
| 1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
model_name = "sshleifer/distilbart-cnn-12-6"
|
|
|
|
| 6 |
|
| 7 |
+
# Load tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Load model in 8-bit (CPU-friendly)
|
| 11 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
+
load_in_8bit=True, # reduces memory & speeds up CPU
|
| 14 |
+
device_map="auto" # automatically maps to CPU
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Create pipeline
|
| 18 |
+
summarizer = pipeline(
|
| 19 |
+
"summarization",
|
| 20 |
+
model=model,
|
| 21 |
+
tokenizer=tokenizer,
|
| 22 |
+
device=-1
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
# Function for Gradio
|
| 26 |
+
def summary_ui(text):
|
| 27 |
+
output = summarizer(text, max_length=512, min_length=30, truncation=True)
|
| 28 |
return output[0]['summary_text']
|
| 29 |
|
| 30 |
gr.close_all()
|
| 31 |
|
| 32 |
+
# Gradio interface
|
| 33 |
demo = gr.Interface(
|
| 34 |
+
fn=summary_ui,
|
| 35 |
inputs=[gr.Textbox(label="Input text to summarize", lines=6)],
|
| 36 |
outputs=[gr.Textbox(label="Summarized text", lines=4)],
|
| 37 |
+
title="8-bit CPU Text Summarizer",
|
| 38 |
+
description="Summarize your text fast on CPU using 8-bit quantization"
|
| 39 |
)
|
| 40 |
+
|
| 41 |
demo.launch()
|