Spaces:
Runtime error
Runtime error
add all
Browse files
main.py
CHANGED
|
@@ -5,6 +5,14 @@ from fastapi import FastAPI,Request
|
|
| 5 |
import uvicorn
|
| 6 |
import json
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
|
|
@@ -16,8 +24,42 @@ def root():
|
|
| 16 |
|
| 17 |
@app.post("/img2img")
|
| 18 |
async def predict(url:str,prompt:str):
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@app.post("/predict")
|
| 23 |
async def predict(request:Request):
|
|
|
|
| 5 |
import uvicorn
|
| 6 |
import json
|
| 7 |
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import time
|
| 10 |
+
from constants import DESCRIPTION, LOGO
|
| 11 |
+
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 |
|
| 18 |
|
|
|
|
| 24 |
|
| 25 |
@app.post("/img2img")
|
| 26 |
async def predict(url:str,prompt:str):
|
| 27 |
+
MAX_QUEUE_SIZE = 4
|
| 28 |
+
start = time.time()
|
| 29 |
+
pipeline = get_pipeline()
|
| 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,
|
| 44 |
+
image=img,
|
| 45 |
+
strength=0.6,
|
| 46 |
+
seed=10,
|
| 47 |
+
width=256,
|
| 48 |
+
height=256,
|
| 49 |
+
guidance_scale=1,
|
| 50 |
+
num_inference_steps=4,
|
| 51 |
+
)
|
| 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 |
+
with open(output_image, 'rb') as image_file:
|
| 59 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 60 |
+
print(encoded_string)
|
| 61 |
+
return encoded_string
|
| 62 |
+
|
| 63 |
|
| 64 |
@app.post("/predict")
|
| 65 |
async def predict(request:Request):
|