Spaces:
Build error
Build error
File size: 935 Bytes
1a4b563 |
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 |
import os
from flask import Flask, request, jsonify, send_file
from transformers import pipeline
app = Flask(__name__)
# Load the Hugging Face pipeline for image generation
generator = pipeline('text-to-image', model='CompVis/stable-diffusion-v1-4', device=0) # Use device=0 if using GPU
@app.route('/generate_image', methods=['POST'])
def generate_image():
# Get the description from the request
description = request.json.get('description')
if not description:
return jsonify({"error": "Description is required"}), 400
# Generate the image using the description
image = generator(description)[0]["sample"] # This will return the generated image
# Save the image to a file
output_path = "generated_image.png"
image.save(output_path)
# Return the image file
return send_file(output_path, mimetype='image/png')
if __name__ == '__main__':
app.run(debug=True)
|