prithivMLmods commited on
Commit
067f239
1 Parent(s): d4e4c4d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +206 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from diffusers import DiffusionPipeline
6
+ import random
7
+ import uuid
8
+ from typing import Tuple
9
+ import numpy as np
10
+
11
+ def save_image(img):
12
+ unique_name = str(uuid.uuid4()) + ".png"
13
+ img.save(unique_name)
14
+ return unique_name
15
+
16
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
17
+ if randomize_seed:
18
+ seed = random.randint(0, MAX_SEED)
19
+ return seed
20
+
21
+ MAX_SEED = np.iinfo(np.int32).max
22
+
23
+ if not torch.cuda.is_available():
24
+ DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
25
+
26
+ base_model = "black-forest-labs/FLUX.1-dev"
27
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
28
+
29
+ lora_repo = "strangerzonehf/Flux-Midjourney-Mix-LoRA"
30
+ trigger_word = "midjourney mix" # Leave trigger_word blank if not used.
31
+
32
+ pipe.load_lora_weights(lora_repo)
33
+ pipe.to("cuda")
34
+
35
+ style_list = [
36
+ {
37
+ "name": "3840 x 2160",
38
+ "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
39
+ },
40
+ {
41
+ "name": "2560 x 1440",
42
+ "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
43
+ },
44
+ {
45
+ "name": "HD+",
46
+ "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
47
+ },
48
+ {
49
+ "name": "Style Zero",
50
+ "prompt": "{prompt}",
51
+ },
52
+ ]
53
+
54
+ styles = {k["name"]: k["prompt"] for k in style_list}
55
+
56
+ DEFAULT_STYLE_NAME = "3840 x 2160"
57
+ STYLE_NAMES = list(styles.keys())
58
+
59
+ def apply_style(style_name: str, positive: str) -> str:
60
+ return styles.get(style_name, styles[DEFAULT_STYLE_NAME]).replace("{prompt}", positive)
61
+
62
+ @spaces.GPU(duration=60, enable_queue=True)
63
+ def generate(
64
+ prompt: str,
65
+ seed: int = 0,
66
+ width: int = 1024,
67
+ height: int = 1024,
68
+ guidance_scale: float = 3,
69
+ randomize_seed: bool = False,
70
+ style_name: str = DEFAULT_STYLE_NAME,
71
+ progress=gr.Progress(track_tqdm=True),
72
+ ):
73
+ seed = int(randomize_seed_fn(seed, randomize_seed))
74
+
75
+ positive_prompt = apply_style(style_name, prompt)
76
+
77
+ if trigger_word:
78
+ positive_prompt = f"{trigger_word} {positive_prompt}"
79
+
80
+ images = pipe(
81
+ prompt=positive_prompt,
82
+ width=width,
83
+ height=height,
84
+ guidance_scale=guidance_scale,
85
+ num_inference_steps=28,
86
+ num_images_per_prompt=1,
87
+ output_type="pil",
88
+ ).images
89
+ image_paths = [save_image(img) for img in images]
90
+ print(image_paths)
91
+ return image_paths, seed
92
+
93
+ examples = [
94
+
95
+ "midjourney mix, a tiny astronaut hatching from an egg on the moon",
96
+ "midjourney mix, Intense Red, a black cat is facing the left side of the frame. The cats head is tilted upward, with its eyes closed. Its whiskers are protruding from its mouth, adding a touch of warmth to the scene. The background is a vibrant red, creating a striking contrast with the cats fur.",
97
+ "midjourney mix, a close-up shot of a womans face, the womans hair is wet, and she is wearing a cream-colored sweater. The background is blurred, and there are red and white signs visible in the background. The womans eyebrows are wet, adding a touch of color to her face. Her lips are a vibrant shade of pink, and her eyes are a darker shade of brown.",
98
+ "midjourney mix, Woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6"
99
+
100
+ ]
101
+
102
+ css = '''
103
+ .gradio-container{max-width: 799px !important}
104
+ h1{text-align:center}
105
+ footer {
106
+ visibility: hidden
107
+ }
108
+ '''
109
+ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
110
+ with gr.Row():
111
+ # Left side: Inputs and options
112
+ with gr.Column(scale=1):
113
+ prompt = gr.Text(
114
+ label="Prompt",
115
+ show_label=False,
116
+ max_lines=1,
117
+ placeholder="Enter your prompt",
118
+ container=False,
119
+ )
120
+ run_button = gr.Button("Generate as ( 768 x 1024 )🤗", scale=0)
121
+
122
+ with gr.Accordion("Advanced options", open=True, visible=True):
123
+ seed = gr.Slider(
124
+ label="Seed",
125
+ minimum=0,
126
+ maximum=MAX_SEED,
127
+ step=1,
128
+ value=0,
129
+ visible=True
130
+ )
131
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
132
+
133
+ with gr.Row(visible=True):
134
+ width = gr.Slider(
135
+ label="Width",
136
+ minimum=512,
137
+ maximum=2048,
138
+ step=64,
139
+ value=768,
140
+ )
141
+ height = gr.Slider(
142
+ label="Height",
143
+ minimum=512,
144
+ maximum=2048,
145
+ step=64,
146
+ value=1024,
147
+ )
148
+
149
+ with gr.Row():
150
+ guidance_scale = gr.Slider(
151
+ label="Guidance Scale",
152
+ minimum=0.1,
153
+ maximum=20.0,
154
+ step=0.1,
155
+ value=3.0,
156
+ )
157
+ num_inference_steps = gr.Slider(
158
+ label="Number of inference steps",
159
+ minimum=1,
160
+ maximum=40,
161
+ step=1,
162
+ value=28,
163
+ )
164
+
165
+ style_selection = gr.Radio(
166
+ show_label=True,
167
+ container=True,
168
+ interactive=True,
169
+ choices=STYLE_NAMES,
170
+ value=DEFAULT_STYLE_NAME,
171
+ label="Quality Style",
172
+ )
173
+
174
+ # Right side: Output gallery and examples
175
+ with gr.Column(scale=2):
176
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
177
+
178
+ gr.Examples(
179
+ examples=examples,
180
+ inputs=prompt,
181
+ outputs=[result, seed],
182
+ fn=generate,
183
+ cache_examples=False,
184
+ )
185
+
186
+ gr.on(
187
+ triggers=[
188
+ prompt.submit,
189
+ run_button.click,
190
+ ],
191
+ fn=generate,
192
+ inputs=[
193
+ prompt,
194
+ seed,
195
+ width,
196
+ height,
197
+ guidance_scale,
198
+ randomize_seed,
199
+ style_selection,
200
+ ],
201
+ outputs=[result, seed],
202
+ api_name="run",
203
+ )
204
+
205
+ if __name__ == "__main__":
206
+ demo.queue(max_size=40).launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ diffusers
3
+ spaces
4
+ transformers
5
+ peft
6
+ sentencepiece
7
+ gradio