arevedudaa commited on
Commit
cae1fec
·
verified ·
1 Parent(s): 7affc0c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
+ import requests
3
+ from gradio_client import Client, handle_file
4
+ import shutil
5
+ import os
6
+
7
+ app = FastAPI()
8
+
9
+ client = Client("yanze/PuLID-FLUX")
10
+ BASE_URL = "https://yanze-pulid-flux.hf.space/file="
11
+
12
+ @app.post("/generate_image")
13
+ async def generate_image(
14
+ prompt: str = Form(...),
15
+ image_url: str = Form(None),
16
+ image_file: UploadFile = File(None)
17
+ ):
18
+ # Determine input image
19
+ id_image = None
20
+ if image_url:
21
+ id_image = handle_file(image_url)
22
+ elif image_file:
23
+ file_location = f"temp_{image_file.filename}"
24
+ with open(file_location, "wb") as buffer:
25
+ shutil.copyfileobj(image_file.file, buffer)
26
+ id_image = handle_file(file_location)
27
+ os.remove(file_location)
28
+
29
+ try:
30
+ result = client.predict(
31
+ prompt=prompt,
32
+ id_image=id_image,
33
+ start_step=0,
34
+ guidance=4,
35
+ seed="-1",
36
+ true_cfg=1,
37
+ width=896,
38
+ height=1152,
39
+ num_steps=20,
40
+ id_weight=1,
41
+ neg_prompt="bad quality, worst quality, text, signature, watermark, extra limbs",
42
+ timestep_to_start_cfg=1,
43
+ max_sequence_length=128,
44
+ api_name="/generate_image"
45
+ )
46
+
47
+ file_path = result[0]
48
+ full_url = f"{BASE_URL}{file_path}"
49
+
50
+ # Download and save image
51
+ response = requests.get(full_url)
52
+ if response.status_code == 200:
53
+ with open("output_image.jpg", "wb") as f:
54
+ f.write(response.content)
55
+ return {"message": "Image saved", "url": full_url}
56
+
57
+ return {"message": "Image generated", "url": full_url}
58
+ except Exception as e:
59
+ return {"error": str(e)}