Thafx commited on
Commit
63488fd
1 Parent(s): 8609e3f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +134 -12
app.py CHANGED
@@ -1,15 +1,85 @@
1
- import os
2
  import gradio as gr
 
 
3
 
4
- API_KEY=os.environ.get('HUGGING_FACE_HUB_TOKEN', None)
 
 
 
5
 
6
- article = """---
7
- This space was created using [SD Space Creator](https://huggingface.co/spaces/anzorq/sd-space-creator)."""
 
 
8
 
9
- gr.Interface.load(
10
- name="models/SG161222/Realistic_Vision_V1.3",
11
- title="""Realistic Vision V1.3""",
12
- description="""Demo for <a href="https://huggingface.co/SG161222/Realistic_Vision_V1.3">Realistic Vision V1.3</a> Stable Diffusion model by "Eugene".
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  Please use this prompt template to get the desired generation results:
15
 
@@ -30,7 +100,59 @@ or
30
  10 Hires steps and Denoising strength 0.025-0.1 for save more details
31
 
32
  Have Fun & Enjoy
33
- //THAFX""",
34
- article=article,
35
- api_key=API_KEY,
36
- ).queue(concurrency_count=20).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
2
  import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
 
6
+ model_id = 'SG161222/Realistic_Vision_V1.3'
7
+ prefix = 'RAW photo,'
8
+
9
+ scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
10
 
11
+ pipe = StableDiffusionPipeline.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
14
+ scheduler=scheduler)
15
 
16
+ pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
19
+ scheduler=scheduler)
20
+
21
+ if torch.cuda.is_available():
22
+ pipe = pipe.to("cuda")
23
+ pipe_i2i = pipe_i2i.to("cuda")
24
+
25
+ def error_str(error, title="Error"):
26
+ return f"""#### {title}
27
+ {error}""" if error else ""
28
+
29
+ def inference(prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt="", auto_prefix=False):
30
+
31
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
32
+ prompt = f"{prefix} {prompt}" if auto_prefix else prompt
33
+
34
+ try:
35
+ if img is not None:
36
+ return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
37
+ else:
38
+ return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
39
+ except Exception as e:
40
+ return None, error_str(e)
41
+
42
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
43
+
44
+ result = pipe(
45
+ prompt,
46
+ negative_prompt = neg_prompt,
47
+ num_inference_steps = int(steps),
48
+ guidance_scale = guidance,
49
+ width = width,
50
+ height = height,
51
+ generator = generator)
52
+
53
+ return result.images[0]
54
+
55
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
56
+
57
+ ratio = min(height / img.height, width / img.width)
58
+ img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
59
+ result = pipe_i2i(
60
+ prompt,
61
+ negative_prompt = neg_prompt,
62
+ init_image = img,
63
+ num_inference_steps = int(steps),
64
+ strength = strength,
65
+ guidance_scale = guidance,
66
+ width = width,
67
+ height = height,
68
+ generator = generator)
69
+
70
+ return result.images[0]
71
+
72
+ css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
73
+ """
74
+ with gr.Blocks(css=css) as demo:
75
+ gr.HTML(
76
+ f"""
77
+ <div class="main-div">
78
+ <div>
79
+ <h1>Realistic Vision V1.3</h1>
80
+ </div>
81
+ <p>
82
+ Demo for <a href="https://huggingface.co/SG161222/Realistic_Vision_V1.3">Realistic Vision V1.3</a> Stable Diffusion model by "Eugene".
83
 
84
  Please use this prompt template to get the desired generation results:
85
 
 
100
  10 Hires steps and Denoising strength 0.025-0.1 for save more details
101
 
102
  Have Fun & Enjoy
103
+ //THAFX<br>
104
+ {"Add the following tokens to your prompts for the model to work properly: <b>prefix</b>" if prefix else ""}
105
+ </p>
106
+ Running on {"<b>GPU 🔥</b>" if torch.cuda.is_available() else f"<b>CPU 🥶</b>. For faster inference it is recommended to <b>upgrade to GPU in <a href='https://huggingface.co/spaces/Thafx/sdrv1_3/settings'>Settings</a></b>"} after duplicating the space<br><br>
107
+ <a style="display:inline-block" href="https://huggingface.co/spaces/Thafx/sdrv1_3?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
108
+ </div>
109
+ """
110
+ )
111
+ with gr.Row():
112
+
113
+ with gr.Column(scale=55):
114
+ with gr.Group():
115
+ with gr.Row():
116
+ prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False)
117
+ generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
118
+
119
+ image_out = gr.Image(height=512)
120
+ error_output = gr.Markdown()
121
+
122
+ with gr.Column(scale=45):
123
+ with gr.Tab("Options"):
124
+ with gr.Group():
125
+ neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
126
+ auto_prefix = gr.Checkbox(label="Prefix styling tokens automatically (RAW photo,)", value=prefix, visible=prefix)
127
+
128
+ with gr.Row():
129
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
130
+ steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
131
+
132
+ with gr.Row():
133
+ width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
134
+ height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
135
+
136
+ seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
137
+
138
+ with gr.Tab("Image to image"):
139
+ with gr.Group():
140
+ image = gr.Image(label="Image", height=256, tool="editor", type="pil")
141
+ strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
142
+
143
+ auto_prefix.change(lambda x: gr.update(placeholder=f"{prefix} [your prompt]" if x else "[Your prompt]"), inputs=auto_prefix, outputs=prompt, queue=False)
144
+
145
+ inputs = [prompt, guidance, steps, width, height, seed, image, strength, neg_prompt, auto_prefix]
146
+ outputs = [image_out, error_output]
147
+ prompt.submit(inference, inputs=inputs, outputs=outputs)
148
+ generate.click(inference, inputs=inputs, outputs=outputs)
149
+
150
+ gr.HTML("""
151
+ <div style="border-top: 1px solid #303030;">
152
+ <br>
153
+ <p>This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p>
154
+ </div>
155
+ """)
156
+
157
+ demo.queue(concurrency_count=1)
158
+ demo.launch()