-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (46 loc) · 2.08 KB
/
main.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, UploadFile, File, Request, Form
from fastapi.templating import Jinja2Templates
import base64
from PIL import Image
from io import BytesIO
from webcolors import hex_to_name # https://pypi.org/project/webcolors/1.3/
from ad_creator import create_ad_template, produce_diffusion_image
# TODO: webcolors library does not include many html hex code , check it
# in webpage write the hex code style as hint
# TODO: improve stable diffusion output
# to run app : uvicorn main:app --reload
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# uvicorn main:app --reload
@app.get("/")
def dynamic_file(request: Request):
return templates.TemplateResponse("get_input.html", {"request": request})
@app.post("/ad")
async def ad(request: Request,
file: UploadFile = File(),
prompt: str = Form(...),
img_hex_code: str = Form(...),
logo: UploadFile = File(...),
color_hex_code: str = Form(...),
punch_line: str = Form(...),
button: str = Form(...)
):
img = Image.open(BytesIO(await file.read()))
# hex codes can be looked up from here : https://www.quackit.com/css/css_color_codes.cfm
try:
color_name = hex_to_name(hex_value=img_hex_code, spec='css3') # Format: #000080
print(color_name)
except Exception:
error_message = "Hex code is not found in webcolors library"
return {"error": error_message}
modified_img = produce_diffusion_image(img,prompt, color_name)
modified_img.save("modified_img.png")
logo_img = Image.open(BytesIO(await logo.read()))
ad_template = create_ad_template(modified_img, logo_img, color_hex_code, punch_line, button)
# Convert the 'PngImageFile' object to bytes
img_bytes = BytesIO()
ad_template.save(img_bytes, format='PNG')
img_bytes = img_bytes.getvalue()
encoded_image = base64.b64encode(img_bytes).decode("utf-8")
return templates.TemplateResponse(
"ad_display.html", {"request": request, "img": encoded_image})