Spaces:
Running
Running
Create Gen
Browse files
Gen
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
app = Flask(__name__)
|
4 |
+
# Load the GPT-2 model and tokenizer
|
5 |
+
model_name = "gpt2"
|
6 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
8 |
+
@app.route('/generate', methods=['POST'])
|
9 |
+
def generate_text():
|
10 |
+
data = request.get_json()
|
11 |
+
input_text = data['input_text']
|
12 |
+
# Tokenize the input text
|
13 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
14 |
+
# Generate text using the GPT-2 model
|
15 |
+
output = model.generate(input_ids, max_length=100, num_return_sequences=1)
|
16 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
17 |
+
return jsonify({'generated_text': generated_text})
|
18 |
+
if __name__ == '__main__':
|
19 |
+
app.run()
|