Spaces:
Sleeping
Sleeping
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 | |
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> | |
''' | |
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) |