vikigitonga11 commited on
Commit
847952d
·
verified ·
1 Parent(s): 8371da7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -3,21 +3,19 @@ import re
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
 
6
- # Load PEGASUS paraphrase model
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 GPU if available (for faster processing)
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- model.to(device)
14
 
15
- # Initialize pipeline with batch processing and optimized settings
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
- # Process multiple sentences in one batch (improves speed)
36
  paraphrased_results = paraphrase_pipeline(
37
- sentences, max_length=60, do_sample=False, batch_size=4 # Increase batch_size for speed
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="🚀 Fast PEGASUS Paraphraser",
49
- description="Enter text and let AI generate a paraphrased version using the optimized PEGASUS model!",
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