File size: 2,136 Bytes
dfcd6b0
ce1dd07
 
 
dfcd6b0
a3614ce
50a1127
 
 
c64b66e
 
370c710
ce1dd07
50a1127
de8b3c6
50a1127
 
460cdbf
430abad
ce1dd07
 
 
c64b66e
 
 
 
 
370c710
 
c64b66e
 
5233da8
c64b66e
 
 
370c710
c64b66e
 
 
 
 
370c710
771c3c3
c64b66e
 
771c3c3
370c710
c64b66e
 
 
278270a
7eb427a
50a1127
 
 
 
 
 
 
 
 
 
 
351b0b4
50a1127
 
 
 
 
370c710
50a1127
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
from fastapi import APIRouter, Form, BackgroundTasks
from config import settings
import os
import json
import utils
import torch
import requests
from PIL import Image
from io import BytesIO
from pydantic import BaseModel
import base64
import uuid

from diffusers import StableDiffusionImg2ImgPipeline

model_id_or_path = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe = pipe.to("cuda")


router = APIRouter()

class ActionBody(BaseModel):
    url: str
    prompt: str
    strength: float
    guidance_scale: float
    resizeW: int
    resizeH: int

@router.post("/perform-action")
async def performAction(actionBody: ActionBody):
    
    response = requests.get(actionBody.url)
    init_image = Image.open(BytesIO(response.content)).convert("RGB")
    init_image = init_image.resize((actionBody.resizeW, actionBody.resizeH))
    images = pipe(prompt=actionBody.prompt, image=init_image, strength=actionBody.strength, guidance_scale=actionBody.guidance_scale).images
    print(images)
    buffered = BytesIO()
    images[0].save(buffered, format="JPEG")
    img_str = base64.b64encode(buffered.getvalue())
    imgUUID = str(uuid.uuid4())
    images[0].save(imgUUID+".png")

    return {
        "imageName" : imgUUID+".png",
        "image": "data:image/jpeg;base64,"+img_str
    }


@router.get("/hi")
async def hifunction():
    
    url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
    response = requests.get(url)
    init_image = Image.open(BytesIO(response.content)).convert("RGB")
    init_image = init_image.resize((768, 512))
    prompt = "A fantasy landscape, trending on artstation"
    images = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images
    print(images)
    print(images[0])

    buffered = BytesIO()
    images[0].save(buffered, format="JPEG")
    img_str = base64.b64encode(buffered.getvalue())

    # images[0].save("fantasy_landscape.png")

    return {
        "image": "data:image/jpeg;base64,"+img_str
    }