premkhumar / driver

0 stars 0 forks source link

Rephrase #1

Open premkhumar opened 2 months ago

premkhumar commented 2 months ago

import openai

Function to rephrase a paragraph

def rephrase_paragraph(paragraph, api_key): openai.api_key = api_key

response = openai.Completion.create(
    model="text-davinci-003",
    prompt=f"Rephrase the following paragraph:\n\n{paragraph}",
    max_tokens=200,
    n=1,
    stop=None,
    temperature=0.7,
)

rephrased_paragraph = response.choices[0].text.strip()
return rephrased_paragraph

Example usage

api_key = "your_openai_api_key_here" # Replace with your actual OpenAI API key paragraph = """ The quick brown fox jumps over the lazy dog. This phrase is commonly used to demonstrate font rendering and keyboard layouts because it contains every letter of the English alphabet. The sentence is easy to understand and widely recognized. """

rephrased = rephrase_paragraph(paragraph, api_key) print("Original Paragraph:\n", paragraph) print("\nRephrased Paragraph:\n", rephrased)

premkhumar commented 2 months ago

If you'd like to explore an alternative approach for paraphrasing text without relying on OpenAI's API, you can use the transformers library by Hugging Face. This library provides access to a variety of pre-trained language models that can perform tasks like text paraphrasing.

Step 1: Install Necessary Libraries

Ensure you have the transformers and torch libraries installed. You can install them using pip if you haven't already:

pip install transformers torch

Step 2: Write the Python Program

Here's a sample Python program that uses a T5 model from Hugging Face to rephrase a paragraph:

from transformers import T5ForConditionalGeneration, T5Tokenizer

# Load pre-trained model and tokenizer
model_name = "t5-base"
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = T5Tokenizer.from_pretrained(model_name)

# Function to rephrase a paragraph
def rephrase_paragraph(paragraph):
    # Preprocess the input text
    input_text = f"paraphrase: {paragraph} </s>"
    encoding = tokenizer.encode_plus(input_text, max_length=512, pad_to_max_length=True, return_tensors="pt")

    # Generate paraphrase
    input_ids = encoding['input_ids']
    attention_mask = encoding['attention_mask']
    outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask, max_length=512, num_beams=5, early_stopping=True)

    # Decode the output text
    rephrased_paragraph = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return rephrased_paragraph

# Example usage
paragraph = """
The quick brown fox jumps over the lazy dog. This phrase is commonly used to demonstrate font rendering and keyboard layouts because it contains every letter of the English alphabet. The sentence is easy to understand and widely recognized.
"""

rephrased = rephrase_paragraph(paragraph)
print("Original Paragraph:\n", paragraph)
print("\nRephrased Paragraph:\n", rephrased)

Explanation

  1. Installation: Ensure the transformers and torch libraries are installed.
  2. Model and Tokenizer: Load the pre-trained T5 model and its tokenizer using the transformers library.
  3. Function: The rephrase_paragraph function takes a paragraph as input, preprocesses it for the T5 model, generates a paraphrase, and decodes the output text.
  4. Example Usage: The script demonstrates how to use the function with a sample paragraph.

Running the Program

Save the script to a Python file and then run the script. The output will display the original and rephrased paragraphs.

Note