Spaces:
Runtime error
Runtime error
added upload image
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ from dotenv import load_dotenv
|
|
| 12 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
|
|
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
allow_origins=["*"],
|
|
@@ -40,22 +42,31 @@ async def generate_image(prompt: str = Query(..., description="Text prompt for i
|
|
| 40 |
image.save(image_data, format="PNG")
|
| 41 |
image_data.seek(0)
|
| 42 |
return StreamingResponse(image_data, media_type="image/png")
|
| 43 |
-
|
| 44 |
-
@app.
|
| 45 |
-
async def
|
| 46 |
-
|
| 47 |
image_bytes = await image_file.read()
|
| 48 |
init_image = Image.open(io.BytesIO(image_bytes)).convert("RGBA")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
ad_prompt = f"""Your system prompt is this: {prompt} Consider your system prompt first.
|
| 53 |
-
Then from init image create me a new image that will attract customers to put in a (ad template)
|
| 54 |
-
Also, use this hex code {hex_code} in the image"""
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
init_image = None
|
| 16 |
+
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
|
|
| 42 |
image.save(image_data, format="PNG")
|
| 43 |
image_data.seek(0)
|
| 44 |
return StreamingResponse(image_data, media_type="image/png")
|
| 45 |
+
|
| 46 |
+
@app.post("/uploadImage", response_class=StreamingResponse)
|
| 47 |
+
async def upload_image(image_file:UploadFile):
|
| 48 |
+
global init_image
|
| 49 |
image_bytes = await image_file.read()
|
| 50 |
init_image = Image.open(io.BytesIO(image_bytes)).convert("RGBA")
|
| 51 |
+
init_image.thumbnail((768, 768))
|
| 52 |
+
return {"message": "You have uploaded the image!"}
|
| 53 |
+
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
@app.get("/generate_ad",response_class=StreamingResponse)
|
| 56 |
+
async def generate_ad(hex_code:str, prompt: str = Query(..., description="Text prompt for image generation")):
|
| 57 |
|
| 58 |
+
if init_image is not None:
|
| 59 |
+
#Generate the image using the text-to-image model
|
| 60 |
+
generator = torch.Generator(device=device).manual_seed(1024)
|
| 61 |
+
ad_prompt = f"""Your system prompt is this: {prompt} Consider your system prompt first.
|
| 62 |
+
Then from the initial image create a new image that will attract customers to put in an (ad template)
|
| 63 |
+
Also, use this hex code {hex_code} in the image"""
|
| 64 |
+
|
| 65 |
+
image = pipe(prompt=ad_prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
|
| 66 |
+
|
| 67 |
+
image_data = io.BytesIO()
|
| 68 |
+
image.save(image_data, format="PNG")
|
| 69 |
+
image_data.seek(0)
|
| 70 |
+
return StreamingResponse(image_data, media_type="image/png")
|
| 71 |
+
else
|
| 72 |
+
return {"message": "You have not uploaded the image!"}
|