Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,19 @@ import re
|
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
5 |
|
6 |
-
# Load PEGASUS
|
7 |
model_name = "tuner007/pegasus_paraphrase"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
10 |
|
11 |
-
# Move model to
|
12 |
-
|
13 |
-
model.to(device)
|
14 |
|
15 |
-
# Initialize pipeline with
|
16 |
paraphrase_pipeline = pipeline(
|
17 |
-
"text2text-generation",
|
18 |
-
model=model,
|
19 |
-
tokenizer=tokenizer,
|
20 |
-
device=0 if torch.cuda.is_available() else -1, # Use GPU if available
|
21 |
truncation=True
|
22 |
)
|
23 |
|
@@ -31,10 +29,10 @@ def paraphrase_text(text):
|
|
31 |
return "⚠️ Please enter some text to paraphrase."
|
32 |
|
33 |
sentences = split_sentences(text)
|
34 |
-
|
35 |
-
#
|
36 |
paraphrased_results = paraphrase_pipeline(
|
37 |
-
sentences, max_length=
|
38 |
)
|
39 |
|
40 |
paraphrased_sentences = [result['generated_text'] for result in paraphrased_results]
|
@@ -45,8 +43,8 @@ demo = gr.Interface(
|
|
45 |
fn=paraphrase_text,
|
46 |
inputs=gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
|
47 |
outputs=gr.Textbox(label="Paraphrased Text", lines=10),
|
48 |
-
title="🚀
|
49 |
-
description="Enter text and let AI generate a paraphrased version using
|
50 |
theme="huggingface"
|
51 |
)
|
52 |
|
|
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
5 |
|
6 |
+
# Load PEGASUS model in optimized mode
|
7 |
model_name = "tuner007/pegasus_paraphrase"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.float16) # Use half precision
|
10 |
|
11 |
+
# Move model to CPU for consistency
|
12 |
+
model.to("cpu")
|
|
|
13 |
|
14 |
+
# Initialize paraphrase pipeline with optimized settings
|
15 |
paraphrase_pipeline = pipeline(
|
16 |
+
"text2text-generation",
|
17 |
+
model=model,
|
18 |
+
tokenizer=tokenizer,
|
|
|
19 |
truncation=True
|
20 |
)
|
21 |
|
|
|
29 |
return "⚠️ Please enter some text to paraphrase."
|
30 |
|
31 |
sentences = split_sentences(text)
|
32 |
+
|
33 |
+
# Batch processing with optimized settings
|
34 |
paraphrased_results = paraphrase_pipeline(
|
35 |
+
sentences, max_length=50, do_sample=False, batch_size=8 # Reduced max_length & increased batch_size
|
36 |
)
|
37 |
|
38 |
paraphrased_sentences = [result['generated_text'] for result in paraphrased_results]
|
|
|
43 |
fn=paraphrase_text,
|
44 |
inputs=gr.Textbox(label="Enter text", placeholder="Type your text to paraphrase...", lines=10),
|
45 |
outputs=gr.Textbox(label="Paraphrased Text", lines=10),
|
46 |
+
title="🚀 Faster PEGASUS Paraphraser",
|
47 |
+
description="Enter text and let AI generate a paraphrased version using an optimized PEGASUS model!",
|
48 |
theme="huggingface"
|
49 |
)
|
50 |
|