File size: 1,671 Bytes
02afadf
 
 
 
50c63ac
02afadf
 
48675d9
02afadf
 
5ceffc0
 
 
 
 
 
02afadf
 
 
 
235adef
02afadf
 
235adef
02afadf
235adef
 
02afadf
235adef
 
02afadf
 
 
235adef
 
 
48675d9
235adef
 
02afadf
 
 
 
 
 
 
 
 
 
 
 
235adef
02afadf
235adef
248c318
 
50c63ac
235adef
02afadf
48675d9
02afadf
 
235adef
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
#!/usr/bin/env python
import json
import pathlib
import tempfile
from pathlib import Path

import gradio as gr
import gradio_user_history as gr_user_history
from gradio_client import Client

from gradio_space_ci import enable_space_ci

enable_space_ci()




client = Client("runwayml/stable-diffusion-v1-5")


def generate(prompt: str, profile: gr.OAuthProfile | None) -> tuple[str, list[str]]:
    out_dir = client.predict(prompt, fn_index=1)

    metadata = {
        "prompt": prompt,
        "negative_prompt": "",
        "guidance_scale": 0.9,
    }
    with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as metadata_file:
        json.dump(metadata, metadata_file)

    with (pathlib.Path(out_dir) / "captions.json").open() as f:
        paths = list(json.load(f).keys())

    # Saving user history
    for path in paths:
        gr_user_history.save_image(label=prompt, image=path, profile=profile, metadata=metadata)

    return paths  # type: ignore


with gr.Blocks(css="style.css") as demo:
    with gr.Group():
        prompt = gr.Text(show_label=False, placeholder="Prompt")
        gallery = gr.Gallery(
            show_label=False,
            columns=2,
            rows=2,
            height="600px",
            object_fit="scale-down",
        )
    prompt.submit(fn=generate, inputs=prompt, outputs=gallery)

with gr.Blocks() as demo_with_history:
    with gr.Tab("README"):
        gr.Markdown(Path("README.md").read_text().split("---")[-1])
    with gr.Tab("Demo"):
        demo.render()
    with gr.Tab("Past generations"):
        gr_user_history.render()

if __name__ == "__main__":
    demo_with_history.queue().launch()