prithivMLmods commited on
Commit
45a12ec
·
verified ·
1 Parent(s): 4d82453

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -243
app.py DELETED
@@ -1,243 +0,0 @@
1
- #!/usr/bin/env python
2
-
3
- import os
4
- import random
5
- import uuid
6
- import base64
7
- import gradio as gr
8
- import numpy as np
9
- from PIL import Image
10
- import spaces
11
- import torch
12
-
13
- from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
14
-
15
- DESCRIPTION = """# DALL•E 3 XL v2 High Fi"""
16
-
17
- def create_download_link(filename):
18
- with open(filename, "rb") as file:
19
- encoded_string = base64.b64encode(file.read()).decode('utf-8')
20
- download_link = f'<a href="data:image/png;base64,{encoded_string}" download="{filename}">Download Image</a>'
21
- return download_link
22
-
23
- def save_image(img, prompt):
24
- unique_name = str(uuid.uuid4()) + ".png"
25
- img.save(unique_name)
26
-
27
- # save with promp to save prompt as image file name
28
- filename = f"{prompt}.png"
29
- img.save(filename)
30
- return filename
31
-
32
- return unique_name
33
-
34
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
35
- if randomize_seed:
36
- seed = random.randint(0, MAX_SEED)
37
- return seed
38
-
39
- MAX_SEED = np.iinfo(np.int32).max
40
-
41
- if not torch.cuda.is_available():
42
- DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
43
-
44
- MAX_SEED = np.iinfo(np.int32).max
45
-
46
- USE_TORCH_COMPILE = 0
47
- ENABLE_CPU_OFFLOAD = 0
48
-
49
-
50
- if torch.cuda.is_available():
51
- pipe = StableDiffusionXLPipeline.from_pretrained(
52
- "fluently/Fluently-XL-v4",
53
- torch_dtype=torch.float16,
54
- use_safetensors=True,
55
- )
56
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
57
-
58
-
59
- pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
60
- pipe.set_adapters("dalle")
61
-
62
- pipe.to("cuda")
63
-
64
-
65
-
66
- @spaces.GPU(enable_queue=True)
67
- def generate(
68
- prompt: str,
69
- negative_prompt: str = "",
70
- use_negative_prompt: bool = False,
71
- seed: int = 0,
72
- width: int = 1024,
73
- height: int = 1024,
74
- guidance_scale: float = 3,
75
- randomize_seed: bool = False,
76
- progress=gr.Progress(track_tqdm=True),
77
- ):
78
-
79
-
80
- seed = int(randomize_seed_fn(seed, randomize_seed))
81
-
82
- if not use_negative_prompt:
83
- negative_prompt = "" # type: ignore
84
-
85
- images = pipe(
86
- prompt=prompt,
87
- negative_prompt=negative_prompt,
88
- width=width,
89
- height=height,
90
- guidance_scale=guidance_scale,
91
- num_inference_steps=25,
92
- num_images_per_prompt=1,
93
- cross_attention_kwargs={"scale": 0.65},
94
- output_type="pil",
95
- ).images
96
- image_paths = [save_image(img, prompt) for img in images]
97
-
98
- #image_paths = [save_image(img) for img in images]
99
- download_links = [create_download_link(path) for path in image_paths]
100
-
101
- print(image_paths)
102
- #return image_paths, seed
103
- return image_paths, seed, download_links
104
-
105
-
106
- #examples = [
107
- # "neon holography crystal cat",
108
- # "a cat eating a piece of cheese",
109
- # "an astronaut riding a horse in space",
110
- # "a cartoon of a boy playing with a tiger",
111
- # "a cute robot artist painting on an easel, concept art",
112
- # "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
113
- #]
114
-
115
- examples = [
116
- "a modern hospital room with advanced medical equipment and a patient resting comfortably",
117
- "a team of surgeons performing a delicate operation using state-of-the-art surgical robots",
118
- "a elderly woman smiling while a nurse checks her vital signs using a holographic display",
119
- "a child receiving a painless vaccination from a friendly robot nurse in a colorful pediatric clinic",
120
- "a group of researchers working in a high-tech laboratory, developing new treatments for rare diseases",
121
- "a telemedicine consultation between a doctor and a patient, using virtual reality technology for a immersive experience"
122
- ]
123
-
124
-
125
- css = '''
126
- .gradio-container{max-width: 1024px !important}
127
- h1{text-align:center}
128
- footer {
129
- visibility: hidden
130
- }
131
- '''
132
-
133
-
134
- #css = '''
135
- #.gradio-container{max-width: 560px !important}
136
- #h1{text-align:center}
137
- #footer {
138
- # visibility: hidden
139
- #}
140
- #'''
141
-
142
-
143
- with gr.Blocks(css=css, theme="pseudolab/huggingface-korea-theme") as demo:
144
- gr.Markdown(DESCRIPTION)
145
- gr.DuplicateButton(
146
- value="Duplicate Space for private use",
147
- elem_id="duplicate-button",
148
- visible=False,
149
- )
150
-
151
- with gr.Group():
152
- with gr.Row():
153
- prompt = gr.Text(
154
- label="Prompt",
155
- show_label=False,
156
- max_lines=1,
157
- placeholder="Enter your prompt",
158
- container=False,
159
- )
160
- run_button = gr.Button("Run", scale=0)
161
- result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
162
- with gr.Accordion("Advanced options", open=False):
163
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
164
- negative_prompt = gr.Text(
165
- label="Negative prompt",
166
- lines=4,
167
- max_lines=6,
168
- value="""(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)""",
169
- placeholder="Enter a negative prompt",
170
- visible=True,
171
- )
172
- seed = gr.Slider(
173
- label="Seed",
174
- minimum=0,
175
- maximum=MAX_SEED,
176
- step=1,
177
- value=0,
178
- visible=True
179
- )
180
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
181
- with gr.Row(visible=True):
182
- width = gr.Slider(
183
- label="Width",
184
- minimum=512,
185
- maximum=2048,
186
- step=8,
187
- value=1024,
188
- )
189
- height = gr.Slider(
190
- label="Height",
191
- minimum=512,
192
- maximum=2048,
193
- step=8,
194
- value=1024,
195
- )
196
- with gr.Row():
197
- guidance_scale = gr.Slider(
198
- label="Guidance Scale",
199
- minimum=0.1,
200
- maximum=20.0,
201
- step=0.1,
202
- value=6,
203
- )
204
-
205
- gr.Examples(
206
- examples=examples,
207
- inputs=prompt,
208
- outputs=[result, seed],
209
- fn=generate,
210
- cache_examples=False,
211
- )
212
-
213
- use_negative_prompt.change(
214
- fn=lambda x: gr.update(visible=x),
215
- inputs=use_negative_prompt,
216
- outputs=negative_prompt,
217
- api_name=False,
218
- )
219
-
220
-
221
- gr.on(
222
- triggers=[
223
- prompt.submit,
224
- negative_prompt.submit,
225
- run_button.click,
226
- ],
227
- fn=generate,
228
- inputs=[
229
- prompt,
230
- negative_prompt,
231
- use_negative_prompt,
232
- seed,
233
- width,
234
- height,
235
- guidance_scale,
236
- randomize_seed,
237
- ],
238
- outputs=[result, seed],
239
- api_name="run",
240
- )
241
-
242
- if __name__ == "__main__":
243
- demo.queue(max_size=20).launch(show_api=False, debug=False)