File size: 2,595 Bytes
3756e16
 
 
 
 
 
 
6e4bf3d
3756e16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from prodiapy import Custom
from prodiapy.util import load
from PIL import Image
from threading import Thread
from utils import image_to_base64
import gradio as gr
import gradio_user_history as gr_user_history
import os

pipe = Custom(os.getenv("PRODIA_API_KEY"))


def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count, profile: gr.OAuthProfile | None):
    total_images = []
    threads = []

    def generate_one_image():
        result = pipe.create(
            "/sd/generate",
            prompt=prompt,
            negative_prompt=negative_prompt,
            model=model,
            steps=steps,
            cfg_scale=cfg_scale,
            sampler=sampler,
            width=width,
            height=height,
            seed=seed
        )
        job = pipe.wait_for(result)
        total_images.append(job['imageUrl'])

    for x in range(batch_count):
        t = Thread(target=generate_one_image)
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    for image in total_images:
        gr_user_history.save_image(label=prompt, image=Image.open(load(image)), profile=profile)

    return gr.update(value=total_images, preview=False)


def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed,
            batch_count):
    if input_image is None:
        return

    total_images = []
    threads = []

    def generate_one_image():
        result = pipe.create(
            "/sd/transform",
            imageData=image_to_base64(input_image),
            denoising_strength=denoising,
            prompt=prompt,
            negative_prompt=negative_prompt,
            model=model,
            steps=steps,
            cfg_scale=cfg_scale,
            sampler=sampler,
            width=width,
            height=height,
            seed=seed

        )
        job = pipe.wait_for(result)
        total_images.append(job['imageUrl'])

    for x in range(batch_count):
        t = Thread(target=generate_one_image)
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    return gr.update(value=total_images, preview=False)


def upscale(image, scale, profile: gr.OAuthProfile | None):
    if image is None:
        return

    job = pipe.create(
        '/upscale',
        imageData=image_to_base64(image),
        resize=scale
    )
    image = pipe.wait_for(job)['imageUrl']
    gr_user_history.save_image(label=f'upscale by {scale}', image=Image.open(load(image)), profile=profile)

    return image