File size: 1,044 Bytes
c906021
 
732d546
c906021
 
ff60146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c906021
 
ff60146
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from flask import Flask, request, jsonify
from transformers import pipeline

app = Flask(__name__)

# Initialize the text generation pipeline
generator = pipeline('text-generation', model='gpt2')  # Replace 'gpt2' with your model

@app.route('/')
def home():
    return '''
    <html>
    <body>
        <h1>Text Generation</h1>
        <form action="/generate" method="post">
            <textarea name="text_input" rows="4" cols="50" placeholder="Enter text here..."></textarea><br>
            <input type="submit" value="Generate">
        </form>
    </body>
    </html>
    '''

@app.route('/generate', methods=['POST'])
def generate():
    text_input = request.form['text_input']
    if text_input:
        # Generate text using the model
        result = generator(text_input, max_length=100, num_return_sequences=1)
        generated_text = result[0]['generated_text']
        return jsonify({'generated_text': generated_text})
    return jsonify({'error': 'No input provided'}), 400

if __name__ == '__main__':
    app.run(debug=True)