Spaces:
Build error
Build error
woods-today
commited on
Commit
·
c64b66e
1
Parent(s):
351b0b4
Working on it
Browse files- routers/training.py +29 -0
routers/training.py
CHANGED
@@ -9,6 +9,8 @@ import torch
|
|
9 |
import requests
|
10 |
from PIL import Image
|
11 |
from io import BytesIO
|
|
|
|
|
12 |
|
13 |
from diffusers import StableDiffusionImg2ImgPipeline
|
14 |
|
@@ -19,6 +21,33 @@ pipe = pipe.to("cuda")
|
|
19 |
|
20 |
router = APIRouter()
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
@router.get("/hi")
|
23 |
async def hifunction():
|
24 |
|
|
|
9 |
import requests
|
10 |
from PIL import Image
|
11 |
from io import BytesIO
|
12 |
+
from pydantic import BaseModel
|
13 |
+
import base64
|
14 |
|
15 |
from diffusers import StableDiffusionImg2ImgPipeline
|
16 |
|
|
|
21 |
|
22 |
router = APIRouter()
|
23 |
|
24 |
+
class ActionBody(BaseModel):
|
25 |
+
url: str
|
26 |
+
prompt: str
|
27 |
+
strength: float
|
28 |
+
guidance_scale: float
|
29 |
+
|
30 |
+
@router.post("/perform-action")
|
31 |
+
async def hifunction(actionBody: ActionBody):
|
32 |
+
|
33 |
+
response = requests.get(actionBody.url)
|
34 |
+
init_image = Image.open(BytesIO(response.content)).convert("RGB")
|
35 |
+
init_image = init_image.resize((768, 512))
|
36 |
+
images = pipe(prompt=actionBody.prompt, image=init_image, strength=actionBody.strength, guidance_scale=actionBody.guidance_scale).images
|
37 |
+
print(images)
|
38 |
+
print(images[0])
|
39 |
+
|
40 |
+
buffered = BytesIO()
|
41 |
+
images[0].save(buffered, format="JPEG")
|
42 |
+
img_str = base64.b64encode(buffered.getvalue())
|
43 |
+
|
44 |
+
# images[0].save("fantasy_landscape.png")
|
45 |
+
|
46 |
+
return {
|
47 |
+
"image": img_str
|
48 |
+
}
|
49 |
+
|
50 |
+
|
51 |
@router.get("/hi")
|
52 |
async def hifunction():
|
53 |
|