Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Paraphrasing text with transformers library

# -*- coding: utf-8 -*-
"""Paraphrasing-with-Transformers_PythonCode.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1bPfvSF7bJqDfw9ZMgfIZPd1Bk-fW7AJY
"""

# !pip install transformers sentencepiece

from transformers import *

# models we gonna use for this tutorial
model_names = [
  "tuner007/pegasus_paraphrase",
  "Vamsi/T5_Paraphrase_Paws",
  "prithivida/parrot_paraphraser_on_T5", # Parrot
]

model = PegasusForConditionalGeneration.from_pretrained("tuner007/pegasus_paraphrase")
tokenizer = PegasusTokenizerFast.from_pretrained("tuner007/pegasus_paraphrase")

def get_paraphrased_sentences(model, tokenizer, sentence, num_return_sequences=5, num_beams=5):
  # tokenize the text to be form of a list of token IDs
  inputs = tokenizer([sentence], truncation=True, padding="longest", return_tensors="pt")
  # generate the paraphrased sentences
  outputs = model.generate(
    **inputs,
    num_beams=num_beams,
    num_return_sequences=num_return_sequences,
  )
  # decode the generated sentences using the tokenizer to get them back to text
  return tokenizer.batch_decode(outputs, skip_special_tokens=True)

sentence = "Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences."

get_paraphrased_sentences(model, tokenizer, sentence, num_beams=10, num_return_sequences=10)

get_paraphrased_sentences(model, tokenizer, "To paraphrase a source, you have to rewrite a passage without changing the meaning of the original text.", num_beams=10, num_return_sequences=10)

tokenizer = AutoTokenizer.from_pretrained("Vamsi/T5_Paraphrase_Paws")
model = AutoModelForSeq2SeqLM.from_pretrained("Vamsi/T5_Paraphrase_Paws")

get_paraphrased_sentences(model, tokenizer, "paraphrase: " + "One of the best ways to learn is to teach what you've already learned")

# !pip install git+https://github.com/PrithivirajDamodaran/Parrot_Paraphraser.git

from parrot import Parrot

parrot = Parrot()

phrases = [
  sentence,
  "One of the best ways to learn is to teach what you've already learned",
  "Paraphrasing is the process of coming up with someone else's ideas in your own words"
]

for phrase in phrases:
  print("-"*100)
  print("Input_phrase: ", phrase)
  print("-"*100)
  paraphrases = parrot.augment(input_phrase=phrase)
  for paraphrase in paraphrases:
   print(paraphrase)
Comment

PREVIOUS NEXT
Code Example
Python :: export ifc dataframe python 
Python :: airflow set ui color of operator ui_color 
Python :: pandas read sql generator to dataframe 
Python :: python glob sort numerically 
Python :: how to test webhook in python.py 
Python :: pubmed database python 
Python :: how to stop a while loop in opencv 
Python :: pandas month number to name 
Python :: Jupyter get cell output 
Python :: sublime python input 
Python :: index operator with if and elif statement in python 
Python :: how to convert multiple jupyter notebook into python script with single commanf 
Python :: kali linux run python script anywhere 
Python :: pip_install_packages2.bat 
Python :: open a tkinter window fullscreen with button 
Python :: flask run function every minute 
Python :: RuntimeError: DataLoader worker (pid(s) 13615) exited unexpectedly 
Python :: sss 
Python :: django query column 
Python :: bulk upload with dictionary or list in django moels 
Python :: dict to csv keys as rows and subkey as columns in python 
Python :: create empty polygon python 
Python :: how to close turle loop 
Python :: remove repetitive characters from the specified column of a given DataFrame 
Python :: prefetched_related django rest framework 
Python :: ya mom 
Python :: python generate fibonacci series 
Python :: python sum over specific indexes 
Python :: flask request file upload to dropbox 
Python :: fastapi authentication 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =