farukbera commited on
Commit
344843b
·
1 Parent(s): 7e78b89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -4,7 +4,7 @@ from PIL import Image, ImageDraw, ImageFont
4
  import torch
5
  from io import BytesIO
6
  from diffusers import StableDiffusionImg2ImgPipeline
7
- from starlette.responses import StreamingResponse
8
 
9
  app = FastAPI()
10
 
@@ -17,10 +17,19 @@ pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion
17
  async def root():
18
  return {"message": "Welcome to the Text-to-Image API!"}
19
 
20
- @app.get("/generate", response_class=StreamingResponse)
21
- async def generate_image(prompt: str = Query(..., description="Text prompt for image generation")):
22
- image = pipe(prompt).images[0]
23
- image_data = io.BytesIO()
24
- image.save(image_data, format="PNG")
25
- image_data.seek(0)
26
- return StreamingResponse(image_data, media_type="image/png")
 
 
 
 
 
 
 
 
 
 
4
  import torch
5
  from io import BytesIO
6
  from diffusers import StableDiffusionImg2ImgPipeline
7
+ #from starlette.responses import StreamingResponse
8
 
9
  app = FastAPI()
10
 
 
17
  async def root():
18
  return {"message": "Welcome to the Text-to-Image API!"}
19
 
20
+ @app.get("/generate_ad")
21
+ async def generate_ad(prompt: str,hex_code:str, button_color:str, punchline_color:str, image_file: UploadFile):
22
+
23
+ image_bytes = await image_file.read()
24
+ init_image = Image.open(BytesIO(image_bytes)).convert("RGB")
25
+ init_image.thumbnail((768, 768))
26
+ # Generate the image using the text-to-image model
27
+ generator = torch.Generator(device=device).manual_seed(1024)
28
+ ad_prompt = "From init image create me a new image that will attract customers to put in a (ad template)"
29
+ image = pipe(prompt=ad_prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
30
+
31
+ # Create the ad template
32
+ ad_template = create_ad_template(image, hex_code, prompt, button_color, punchline_color)
33
+
34
+ # Return the ad template as a response
35
+ return ad_template