AI-Rephraser / app.py
imseldrith's picture
Create app.py
d4399fb
raw
history blame
842 Bytes
from flask import Flask, request
from transformers import GPT2Tokenizer, GPT2LMHeadModel
app = Flask(__name__)
# Load the GPT-2 model and tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')
@app.route('/paraphrase', methods=['POST'])
def paraphrase():
# Get the input text from the request
input_text = request.form.get('input_text')
# Encode the input text using the tokenizer
input_ids = tokenizer.encode(input_text, return_tensors='pt')
# Generate the paraphrased text using the model
output = model.generate(input_ids, max_length=1024, top_k=5, top_p=0.95, num_return_sequences=1)
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
# Return the paraphrased text
return output_text
if __name__ == '__main__':
app.run()