Commit
·
5dec19d
1
Parent(s):
97530d5
Create Api
Browse files
Api
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import DALL_E
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Load the pre-trained DALL-E model
|
9 |
+
model = DALL_E.from_pretrained("openai/your-dall-e-model-name-here")
|
10 |
+
|
11 |
+
# Define an API endpoint for generating images from text
|
12 |
+
@app.route('/generate_image', methods=['POST'])
|
13 |
+
def generate_image():
|
14 |
+
try:
|
15 |
+
# Get text input from the request
|
16 |
+
text_description = request.json['text_description']
|
17 |
+
|
18 |
+
# Generate an image from the text description
|
19 |
+
output = model.generate_images(text_description)
|
20 |
+
|
21 |
+
# Get the image data
|
22 |
+
image_data = requests.get(output[0]["image"]).content
|
23 |
+
|
24 |
+
# Save the image to a file or return it as a response
|
25 |
+
# For returning as a response:
|
26 |
+
# return jsonify({'image_data': image_data})
|
27 |
+
|
28 |
+
# For saving to a file:
|
29 |
+
with open("generated_image.jpg", "wb") as img_file:
|
30 |
+
img_file.write(image_data)
|
31 |
+
|
32 |
+
return jsonify({'message': 'Image generated successfully'})
|
33 |
+
except Exception as e:
|
34 |
+
return jsonify({'error': str(e)})
|
35 |
+
|
36 |
+
if __name__ == '__main__':
|
37 |
+
app.run(debug=True)
|