Upload 2 files
Browse files
api.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template_string
|
2 |
+
from model import generate_image
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
|
7 |
+
# @app.route("/")
|
8 |
+
# def api_generate_image():
|
9 |
+
# # Generate the image using the model
|
10 |
+
# image = generate_image("Hello, world!")
|
11 |
+
|
12 |
+
# # Embed the image in an HTML page
|
13 |
+
# html = """
|
14 |
+
# <html>
|
15 |
+
# <body>
|
16 |
+
# <h1>Generated Image</h1>
|
17 |
+
# <img src="data:image/jpeg;base64,{{ image }}" />
|
18 |
+
# </body>
|
19 |
+
# </html>
|
20 |
+
# """
|
21 |
+
# return render_template_string(html, image=image)
|
22 |
+
|
23 |
+
|
24 |
+
@app.route("/generate_image", methods=["POST"])
|
25 |
+
def api_generate_image():
|
26 |
+
# Get the input text from the request
|
27 |
+
text = request.json["text"]
|
28 |
+
|
29 |
+
# Generate the image using the model
|
30 |
+
image = generate_image(text)
|
31 |
+
|
32 |
+
# Return the generated image as the response
|
33 |
+
response = {"image": image}
|
34 |
+
return jsonify(response)
|
35 |
+
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
app.run(host="0.0.0.0", port=5000)
|
model.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import Image
|
3 |
+
import io
|
4 |
+
import base64
|
5 |
+
|
6 |
+
|
7 |
+
def generate_image(text):
|
8 |
+
# Generate a random image
|
9 |
+
image = np.random.randint(0, 256, size=(1028, 1028, 3), dtype=np.uint8)
|
10 |
+
image = Image.fromarray(image)
|
11 |
+
|
12 |
+
# Save the image to a byte buffer
|
13 |
+
buffer = io.BytesIO()
|
14 |
+
image.save(buffer, format="JPEG")
|
15 |
+
image_bytes = buffer.getvalue()
|
16 |
+
|
17 |
+
# Convert the image bytes to a base64-encoded string
|
18 |
+
image_string = base64.b64encode(image_bytes).decode("utf-8")
|
19 |
+
|
20 |
+
return image_string
|
21 |
+
|
22 |
+
|
23 |
+
# # Assume that `image_string` is the base64-encoded image string returned by `generate_image`
|
24 |
+
# image_bytes = base64.b64decode(generate_image(""))
|
25 |
+
# image = Image.open(io.BytesIO(image_bytes))
|
26 |
+
# image.show() # Show the image in a new window
|