michaelj commited on
Commit
cef2c6e
1 Parent(s): aa021ca

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -23
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(url:str,prompt:str):
27
  MAX_QUEUE_SIZE = 4
28
  start = time.time()
29
-
30
-
31
- url = "https://img2.baidu.com/it/u=1845675188,2679793929&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500"
32
- prompt = "a nice Comfortable and clean. According to Baidu Education Information, the adjectives for a room include: comfortable, clean, beautiful, spacious, warm, quiet, luxurious, pleasant, exquisite, and warm ,colorful, light room width sofa,8k"
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 = init_image.resize((newW, newH))
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
- image_bytes = output_image.to_bytes()
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(request:Request):
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