File size: 2,121 Bytes
0ed2ee7
 
 
22c4eb1
ee259c3
e07c4e9
3671cba
423e05a
 
 
 
 
 
 
 
3671cba
aa021ca
697988f
d687e0e
 
 
 
0ed2ee7
d687e0e
e07c4e9
 
423e05a
 
aa021ca
423e05a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa021ca
 
 
 
 
 
423e05a
e07c4e9
22c4eb1
e07c4e9
 
 
0950a65
 
ee259c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# https://medium.com/@qacheampong/building-and-deploying-a-fastapi-app-with-hugging-face-9210e9b4a713
# https://huggingface.co/spaces/Queensly/FastAPI_in_Docker

from fastapi import FastAPI,Request
import uvicorn
import json

from PIL import Image
import time
from constants import DESCRIPTION, LOGO
from model import get_pipeline
from utils import replace_background
from diffusers.utils import load_image
import base64

app = FastAPI()
pipeline = get_pipeline()

#Endpoints 
#Root endpoints
@app.get("/")
def root():
    return {"API": "Sum of 2 Squares"}
    
@app.post("/img2img")
async def predict(url:str,prompt:str):
    MAX_QUEUE_SIZE = 4
    start = time.time()
   
    
    url = "https://img2.baidu.com/it/u=1845675188,2679793929&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500"
    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"
    
    init_image = load_image(url).convert("RGB")
    # image1 = replace_background(init_image.resize((256, 256)))
    w, h = init_image.size
    newW = 512
    newH = int(h * newW / w)
    img = init_image.resize((newW, newH))  
    end1 = time.time()
    print("加载管道:", end1 - start)
    result = pipeline(
        prompt=prompt,
        image=img,
        strength=0.6,
        seed=10,
        width=256,
        height=256,
        guidance_scale=1,
        num_inference_steps=4,
    )
    output_image = result.images[0]
    end2 = time.time()
    print("测试",output_image)
    print("s生成完成:", end2 - end1)
    
    output_image.save("./imageclm5.png")
    # 将图片对象转换为bytes
    image_bytes = output_image.to_bytes() 
    
    # 对bytes进行base64编码    
    encoded_string = base64.b64encode(image_bytes).decode('utf-8')
    return encoded_string
        
    
@app.post("/predict")
async def predict(request:Request):
  body = await request.body()
  data = json.loads(body)
  prompt = data.get("prompt")
  return f"您好,{prompt}"