Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -12,7 +12,7 @@ from model import get_pipeline
|
|
12 |
from utils import replace_background
|
13 |
from diffusers.utils import load_image
|
14 |
import base64
|
15 |
-
|
16 |
app = FastAPI()
|
17 |
pipeline = get_pipeline()
|
18 |
|
@@ -23,21 +23,18 @@ def root():
|
|
23 |
return {"API": "Sum of 2 Squares"}
|
24 |
|
25 |
@app.post("/img2img")
|
26 |
-
async def predict(
|
27 |
MAX_QUEUE_SIZE = 4
|
28 |
start = time.time()
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
init_image = load_image(url).convert("RGB")
|
35 |
-
# image1 = replace_background(init_image.resize((256, 256)))
|
36 |
-
w, h = init_image.size
|
37 |
newW = 512
|
38 |
newH = int(h * newW / w)
|
39 |
-
img =
|
40 |
end1 = time.time()
|
|
|
41 |
print("加载管道:", end1 - start)
|
42 |
result = pipeline(
|
43 |
prompt=prompt,
|
@@ -52,21 +49,13 @@ async def predict(url:str,prompt:str):
|
|
52 |
output_image = result.images[0]
|
53 |
end2 = time.time()
|
54 |
print("测试",output_image)
|
55 |
-
print("s生成完成:", end2 - end1)
|
56 |
-
|
57 |
-
output_image.save("./imageclm5.png")
|
58 |
# 将图片对象转换为bytes
|
59 |
-
|
60 |
-
|
61 |
-
# 对bytes进行base64编码
|
62 |
-
encoded_string = base64.b64encode(image_bytes).decode('utf-8')
|
63 |
-
return encoded_string
|
64 |
|
65 |
|
66 |
@app.post("/predict")
|
67 |
-
async def predict(
|
68 |
-
body = await request.body()
|
69 |
-
data = json.loads(body)
|
70 |
-
prompt = data.get("prompt")
|
71 |
return f"您好,{prompt}"
|
72 |
|
|
|
12 |
from utils import replace_background
|
13 |
from diffusers.utils import load_image
|
14 |
import base64
|
15 |
+
import io
|
16 |
app = FastAPI()
|
17 |
pipeline = get_pipeline()
|
18 |
|
|
|
23 |
return {"API": "Sum of 2 Squares"}
|
24 |
|
25 |
@app.post("/img2img")
|
26 |
+
async def predict(prompt=Body(...),imgbase64data=Body(...)):
|
27 |
MAX_QUEUE_SIZE = 4
|
28 |
start = time.time()
|
29 |
+
print("参数",imgbase64data,prompt)
|
30 |
+
image_data = base64.b64decode(imgbase64data)
|
31 |
+
image1 = Image.open(io.BytesIO(image_data))
|
32 |
+
w, h = image1.size
|
|
|
|
|
|
|
|
|
33 |
newW = 512
|
34 |
newH = int(h * newW / w)
|
35 |
+
img = image1.resize((newW, newH))
|
36 |
end1 = time.time()
|
37 |
+
print("图像:", img.size)
|
38 |
print("加载管道:", end1 - start)
|
39 |
result = pipeline(
|
40 |
prompt=prompt,
|
|
|
49 |
output_image = result.images[0]
|
50 |
end2 = time.time()
|
51 |
print("测试",output_image)
|
52 |
+
print("s生成完成:", end2 - end1)
|
|
|
|
|
53 |
# 将图片对象转换为bytes
|
54 |
+
output_image_base64 = base64.b64encode(output_image.tobytes()).decode()
|
55 |
+
return output_image_base64
|
|
|
|
|
|
|
56 |
|
57 |
|
58 |
@app.post("/predict")
|
59 |
+
async def predict(prompt=Body(...)):
|
|
|
|
|
|
|
60 |
return f"您好,{prompt}"
|
61 |
|