farukbera commited on
Commit
0c8265b
·
1 Parent(s): 4403328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,8 +1,10 @@
1
- #pip install fastapi uvicorn transformers
2
- from fastapi import FastAPI, Request
3
  from PIL import Image, ImageDraw, ImageFont
4
  import torch
 
5
  from diffusers import StableDiffusionImg2ImgPipeline
 
6
  app = FastAPI()
7
 
8
  device = "cuda"
@@ -42,20 +44,14 @@ async def generate_ad(request: Request):
42
  return {"message": "Welcome to the Ad Template API"}
43
 
44
  @app.get("/generate_ad")
45
- async def generate_ad(request: Request):
46
- # Get the input data from the request
47
- data = await request.json()
48
-
49
- # Extract the inputs from the data
50
- prompt = data["prompt"]
51
- hex_code = data["hex_code"]
52
- text = data["text"]
53
- button_color = data["button_color"]
54
- punchline_color = data["punchline_color"]
55
- init_image = "flat-white-3402c4f.jpg"
56
- # Generate the image using the text-to-image model
57
  generator = torch.Generator(device=device).manual_seed(1024)
58
- image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
 
59
 
60
  # Create the ad template
61
  ad_template = create_ad_template(image, hex_code, text, button_color, punchline_color)
 
1
+ #pip install fastapi uvicorn transformers diffusers
2
+ from fastapi import FastAPI, Request, UploadFile, File
3
  from PIL import Image, ImageDraw, ImageFont
4
  import torch
5
+ from io import BytesIO
6
  from diffusers import StableDiffusionImg2ImgPipeline
7
+
8
  app = FastAPI()
9
 
10
  device = "cuda"
 
44
  return {"message": "Welcome to the Ad Template API"}
45
 
46
  @app.get("/generate_ad")
47
+ async def generate_ad(prompt: str,hex_code:str, button_color:str, punchline_color:str, image_file: UploadFile):
48
+ image_bytes = await image_file.read()
49
+ init_image = Image.open(BytesIO(image_bytes)).convert("RGB")
50
+ init_image.thumbnail((768, 768))
51
+ # Generate the image using the img-to-image model
 
 
 
 
 
 
 
52
  generator = torch.Generator(device=device).manual_seed(1024)
53
+ ad_prompt = "From init image create me a new image that will attract customers to put in a (ad template)"
54
+ image = pipe(prompt=ad_prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
55
 
56
  # Create the ad template
57
  ad_template = create_ad_template(image, hex_code, text, button_color, punchline_color)