Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify, send_file
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the Hugging Face pipeline for image generation
|
| 8 |
+
generator = pipeline('text-to-image', model='CompVis/stable-diffusion-v1-4', device=0) # Use device=0 if using GPU
|
| 9 |
+
|
| 10 |
+
@app.route('/generate_image', methods=['POST'])
|
| 11 |
+
def generate_image():
|
| 12 |
+
# Get the description from the request
|
| 13 |
+
description = request.json.get('description')
|
| 14 |
+
|
| 15 |
+
if not description:
|
| 16 |
+
return jsonify({"error": "Description is required"}), 400
|
| 17 |
+
|
| 18 |
+
# Generate the image using the description
|
| 19 |
+
image = generator(description)[0]["sample"] # This will return the generated image
|
| 20 |
+
|
| 21 |
+
# Save the image to a file
|
| 22 |
+
output_path = "generated_image.png"
|
| 23 |
+
image.save(output_path)
|
| 24 |
+
|
| 25 |
+
# Return the image file
|
| 26 |
+
return send_file(output_path, mimetype='image/png')
|
| 27 |
+
|
| 28 |
+
if __name__ == '__main__':
|
| 29 |
+
app.run(debug=True)
|