File size: 1,160 Bytes
5dec19d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
from flask import Flask, request, jsonify
from transformers import DALL_E
from PIL import Image
import requests

app = Flask(__name__)

# Load the pre-trained DALL-E model
model = DALL_E.from_pretrained("openai/your-dall-e-model-name-here")

# Define an API endpoint for generating images from text
@app.route('/generate_image', methods=['POST'])
def generate_image():
    try:
        # Get text input from the request
        text_description = request.json['text_description']

        # Generate an image from the text description
        output = model.generate_images(text_description)

        # Get the image data
        image_data = requests.get(output[0]["image"]).content

        # Save the image to a file or return it as a response
        # For returning as a response:
        # return jsonify({'image_data': image_data})

        # For saving to a file:
        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)