multimodalart HF staff commited on
Commit
c94fbf5
1 Parent(s): 294c1a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -22
app.py CHANGED
@@ -26,35 +26,112 @@ scheduler = Scheduler.from_pretrained(model_path, subfolder="scheduler")
26
  pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler)
27
  pipe.to(device)
28
 
29
- @spaces.GPU
30
- def generate_image(prompt, negative_prompt, resolution, steps, cfg, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
31
  image = pipe(
32
  prompt=prompt,
33
  negative_prompt=negative_prompt,
34
- height=resolution,
35
- width=resolution,
36
- guidance_scale=cfg,
37
- num_inference_steps=steps
38
  ).images[0]
39
 
40
- return image
41
 
42
  # Default negative prompt
43
  default_negative_prompt = "worst quality, normal quality, low quality, low res, blurry, distortion, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch, duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, bad anatomy, bad proportions, bad quality, deformed, disconnected limbs, out of frame, out of focus, dehydrated, disfigured, extra arms, extra limbs, extra hands, fused fingers, gross proportions, long neck, jpeg, malformed limbs, mutated, mutated hands, mutated limbs, missing arms, missing fingers, picture frame, poorly drawn hands, poorly drawn face, collage, pixel, pixelated, grainy, color aberration, amputee, autograph, bad illustration, beyond the borders, blank background, body out of frame, boring background, branding, cut off, dismembered, disproportioned, distorted, draft, duplicated features, extra fingers, extra legs, fault, flaw, grains, hazy, identifying mark, improper scale, incorrect physiology, incorrect ratio, indistinct, kitsch, low resolution"
44
 
45
- # Gradio interface
46
- iface = gr.Interface(
47
- fn=generate_image,
48
- inputs=[
49
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
50
- gr.Textbox(label="Negative Prompt", value=default_negative_prompt),
51
- gr.Slider(512, 1024, 1024, step=64, label="Resolution"),
52
- gr.Slider(1, 100, 48, step=1, label="Number of Steps"),
53
- gr.Slider(1, 20, 9, step=0.5, label="CFG Scale")
54
- ],
55
- outputs=gr.Image(type="pil"),
56
- title="Meissonic Image Generator",
57
- description="Generate images using the Meissonic model. Enter a prompt and adjust parameters to create your image.",
58
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- iface.launch()
 
26
  pipe = Pipeline(vq_model, tokenizer=tokenizer, text_encoder=text_encoder, transformer=model, scheduler=scheduler)
27
  pipe.to(device)
28
 
29
+
30
+ def generate_image(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
31
+ if randomize_seed or seed == 0:
32
+ seed = torch.randint(0, MAX_SEED, (1,)).item()
33
+ torch.manual_seed(seed)
34
+
35
  image = pipe(
36
  prompt=prompt,
37
  negative_prompt=negative_prompt,
38
+ height=height,
39
+ width=width,
40
+ guidance_scale=guidance_scale,
41
+ num_inference_steps=num_inference_steps
42
  ).images[0]
43
 
44
+ return image, seed
45
 
46
  # Default negative prompt
47
  default_negative_prompt = "worst quality, normal quality, low quality, low res, blurry, distortion, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch, duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, bad anatomy, bad proportions, bad quality, deformed, disconnected limbs, out of frame, out of focus, dehydrated, disfigured, extra arms, extra limbs, extra hands, fused fingers, gross proportions, long neck, jpeg, malformed limbs, mutated, mutated hands, mutated limbs, missing arms, missing fingers, picture frame, poorly drawn hands, poorly drawn face, collage, pixel, pixelated, grainy, color aberration, amputee, autograph, bad illustration, beyond the borders, blank background, body out of frame, boring background, branding, cut off, dismembered, disproportioned, distorted, draft, duplicated features, extra fingers, extra legs, fault, flaw, grains, hazy, identifying mark, improper scale, incorrect physiology, incorrect ratio, indistinct, kitsch, low resolution"
48
 
49
+ css = """
50
+ #col-container {
51
+ margin: 0 auto;
52
+ max-width: 640px;
53
+ }
54
+ """
55
+
56
+ examples = [
57
+ "a beautiful landscape painting with a river and mountains in the background",
58
+ "a futuristic cityscape at night with flying cars",
59
+ "a portrait of a smiling old man with wrinkles and wisdom in his eyes",
60
+ ]
61
+
62
+ with gr.Blocks(css=css) as demo:
63
+ with gr.Column(elem_id="col-container"):
64
+ gr.Markdown("# Meissonic Text-to-Image Generator")
65
+ with gr.Row():
66
+ prompt = gr.Text(
67
+ label="Prompt",
68
+ show_label=False,
69
+ max_lines=1,
70
+ placeholder="Enter your prompt",
71
+ container=False,
72
+ )
73
+ run_button = gr.Button("Run", scale=0, variant="primary")
74
+ result = gr.Image(label="Result", show_label=False)
75
+ with gr.Accordion("Advanced Settings", open=False):
76
+ negative_prompt = gr.Text(
77
+ label="Negative prompt",
78
+ max_lines=1,
79
+ placeholder="Enter a negative prompt",
80
+ value=default_negative_prompt,
81
+ )
82
+ seed = gr.Slider(
83
+ label="Seed",
84
+ minimum=0,
85
+ maximum=MAX_SEED,
86
+ step=1,
87
+ value=0,
88
+ )
89
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
90
+ with gr.Row():
91
+ width = gr.Slider(
92
+ label="Width",
93
+ minimum=256,
94
+ maximum=MAX_IMAGE_SIZE,
95
+ step=32,
96
+ value=1024,
97
+ )
98
+ height = gr.Slider(
99
+ label="Height",
100
+ minimum=256,
101
+ maximum=MAX_IMAGE_SIZE,
102
+ step=32,
103
+ value=1024,
104
+ )
105
+ with gr.Row():
106
+ guidance_scale = gr.Slider(
107
+ label="Guidance scale",
108
+ minimum=0.0,
109
+ maximum=20.0,
110
+ step=0.1,
111
+ value=9.0,
112
+ )
113
+ num_inference_steps = gr.Slider(
114
+ label="Number of inference steps",
115
+ minimum=1,
116
+ maximum=100,
117
+ step=1,
118
+ value=48,
119
+ )
120
+ gr.Examples(examples=examples, inputs=[prompt])
121
+ gr.on(
122
+ triggers=[run_button.click, prompt.submit],
123
+ fn=generate_image,
124
+ inputs=[
125
+ prompt,
126
+ negative_prompt,
127
+ seed,
128
+ randomize_seed,
129
+ width,
130
+ height,
131
+ guidance_scale,
132
+ num_inference_steps,
133
+ ],
134
+ outputs=[result, seed],
135
+ )
136
 
137
+ demo.launch()