File size: 1,095 Bytes
0b50181
9bd40b7
0b50181
 
 
 
 
 
 
 
9bd40b7
 
 
0b50181
 
 
 
 
 
 
9bd40b7
26dda45
0b50181
 
26dda45
 
0b50181
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
import torch
from transformers import DallEProcessor, DallEModel
from PIL import Image
import requests
from flask import Flask, request, jsonify
import io

app = Flask(__name__)

# Initialize the DALL-E mini model and processor
model_name = "dalle-mini/dalle-mini"
processor = DallEProcessor.from_pretrained(model_name)
model = DallEModel.from_pretrained(model_name)

@app.route('/generate', methods=['POST'])
def generate_image():
    data = request.json
    prompt = data.get('prompt', '')

    # Generate images from prompt
    inputs = processor(text=prompt, return_tensors="pt")
    outputs = model.generate(**inputs)

    # Post-process the generated image
    generated_image = outputs[0]
    image = Image.fromarray(generated_image.numpy().astype('uint8'))

    # Save image to a BytesIO object
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='PNG')
    img_byte_arr = img_byte_arr.getvalue()

    return jsonify({'image': img_byte_arr.hex()})

@app.route('/')
def home():
    return "Welcome to GenArt Narrative!"

if __name__ == '__main__':
    app.run(debug=True)