|
from flask import Flask, request, jsonify |
|
from transformers import DALL_E |
|
from PIL import Image |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
model = DALL_E.from_pretrained("openai/your-dall-e-model-name-here") |
|
|
|
|
|
@app.route('/generate_image', methods=['POST']) |
|
def generate_image(): |
|
try: |
|
|
|
text_description = request.json['text_description'] |
|
|
|
|
|
output = model.generate_images(text_description) |
|
|
|
|
|
image_data = requests.get(output[0]["image"]).content |
|
|
|
|
|
|
|
|
|
|
|
|
|
with open("generated_image.jpg", "wb") as img_file: |
|
img_file.write(image_data) |
|
|
|
return jsonify({'message': 'Image generated successfully'}) |
|
except Exception as e: |
|
return jsonify({'error': str(e)}) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |