farukbera commited on
Commit
cd8f78d
·
1 Parent(s): f90afe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -49
app.py CHANGED
@@ -12,53 +12,14 @@ device = "cuda"
12
  device1 = "cpu"
13
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device1)
14
 
15
- def create_ad_template(image, hex_code, text, button_color, punchline_color):
16
- # Load the logo image
17
- logo_image = Image.open("flat-white-3402c4f.jpg")
18
-
19
- # Create a blank canvas for the ad template
20
- ad_template = Image.new("RGB", (800, 600), hex_code)
21
-
22
- # Paste the logo image at the top of the ad template
23
- ad_template.paste(logo_image, (0, 0))
24
-
25
- # Paste the generated image in the middle of the ad template
26
- ad_template.paste(image, (200, 100))
27
-
28
- # Add the text at the bottom of the ad template
29
- draw = ImageDraw.Draw(ad_template)
30
- font = ImageFont.truetype("arial.ttf", 24)
31
- text_width, text_height = draw.textsize(text, font=font)
32
- text_position = (400 - text_width / 2, 500 - text_height / 2)
33
- draw.text(text_position, text, font=font, fill=punchline_color)
34
-
35
- # Add the button at the bottom of the ad template
36
- button_width, button_height = 200, 50
37
- button_position = (400 - button_width / 2, 550 - button_height / 2)
38
- draw.rectangle(button_position, fill=button_color)
39
-
40
- return ad_template
41
-
42
  @app.get("/")
43
- async def generate_ad(request: Request):
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)
58
-
59
- # Return the ad template as a response
60
- return ad_template
61
-
62
- """
63
- The application is now deployed and running. You can access it in your browser at http://localhost:8000/generate_ad. You can send a POST request to this endpoint with the necessary inputs to generate the ad template.
64
- """
 
12
  device1 = "cpu"
13
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device1)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.get("/")
16
+ async def root():
17
+ return {"message": "Welcome to the Text-to-Image API!"}
18
+
19
+ @app.get("/generate", response_class=StreamingResponse)
20
+ async def generate_image(prompt: str = Query(..., description="Text prompt for image generation")):
21
+ image = pipe(prompt).images[0]
22
+ image_data = io.BytesIO()
23
+ image.save(image_data, format="PNG")
24
+ image_data.seek(0)
25
+ return StreamingResponse(image_data, media_type="image/png")