Spaces:
Running
Running
Commit
·
d4399fb
1
Parent(s):
cf030dd
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# Load the GPT-2 model and tokenizer
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
8 |
+
model = GPT2LMHeadModel.from_pretrained('gpt2')
|
9 |
+
|
10 |
+
@app.route('/paraphrase', methods=['POST'])
|
11 |
+
def paraphrase():
|
12 |
+
# Get the input text from the request
|
13 |
+
input_text = request.form.get('input_text')
|
14 |
+
|
15 |
+
# Encode the input text using the tokenizer
|
16 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
17 |
+
|
18 |
+
# Generate the paraphrased text using the model
|
19 |
+
output = model.generate(input_ids, max_length=1024, top_k=5, top_p=0.95, num_return_sequences=1)
|
20 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
# Return the paraphrased text
|
23 |
+
return output_text
|
24 |
+
|
25 |
+
if __name__ == '__main__':
|
26 |
+
app.run()
|