SamuelVijayJacob commited on
Commit
284f445
·
verified ·
1 Parent(s): 1192e1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -0
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import numpy as np
4
+ import PIL.Image
5
+ from PIL import Image
6
+ import random
7
+ from diffusers import StableDiffusionXLPipeline
8
+ from diffusers import EulerAncestralDiscreteScheduler
9
+ import torch
10
+ import gradio_client.utils
11
+
12
+ # もっと徹底的なモンキーパッチ
13
+ old_get_type = gradio_client.utils.get_type
14
+ def new_get_type(schema):
15
+ if isinstance(schema, bool):
16
+ return "bool"
17
+ return old_get_type(schema)
18
+
19
+ gradio_client.utils.get_type = new_get_type
20
+
21
+ # _json_schema_to_python_typeの修正も追加
22
+ old_json_schema_to_python_type = gradio_client.utils._json_schema_to_python_type
23
+ def new_json_schema_to_python_type(schema, defs=None):
24
+ if isinstance(schema, bool):
25
+ return "bool"
26
+ try:
27
+ return old_json_schema_to_python_type(schema, defs)
28
+ except Exception as e:
29
+ # エラーが発生した場合は汎用的な型を返す
30
+ return "any"
31
+
32
+ gradio_client.utils._json_schema_to_python_type = new_json_schema_to_python_type
33
+
34
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
35
+
36
+ # Make sure to use torch.float16 consistently throughout the pipeline
37
+ pipe = StableDiffusionXLPipeline.from_pretrained(
38
+ "votepurchase/novaAnimeXL_ilV60",
39
+ torch_dtype=torch.float16,
40
+ variant="fp16", # Explicitly use fp16 variant
41
+ use_safetensors=True # Use safetensors if available
42
+ )
43
+
44
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
45
+ pipe.to(device)
46
+
47
+ # Force all components to use the same dtype
48
+ pipe.text_encoder.to(torch.float16)
49
+ pipe.text_encoder_2.to(torch.float16)
50
+ pipe.vae.to(torch.float16)
51
+ pipe.unet.to(torch.float16)
52
+
53
+ MAX_SEED = np.iinfo(np.int32).max
54
+ MAX_IMAGE_SIZE = 1216
55
+
56
+ @spaces.GPU
57
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
58
+ # Check and truncate prompt if too long (CLIP can only handle 77 tokens)
59
+ if len(prompt.split()) > 60: # Rough estimate to avoid exceeding token limit
60
+ print("Warning: Prompt may be too long and will be truncated by the model")
61
+
62
+ if randomize_seed:
63
+ seed = random.randint(0, MAX_SEED)
64
+
65
+ generator = torch.Generator(device=device).manual_seed(seed)
66
+
67
+ try:
68
+ output_image = pipe(
69
+ prompt=prompt,
70
+ negative_prompt=negative_prompt,
71
+ guidance_scale=guidance_scale,
72
+ num_inference_steps=num_inference_steps,
73
+ width=width,
74
+ height=height,
75
+ generator=generator
76
+ ).images[0]
77
+
78
+ return output_image
79
+ except RuntimeError as e:
80
+ print(f"Error during generation: {e}")
81
+ # Return a blank image with error message
82
+ error_img = Image.new('RGB', (width, height), color=(0, 0, 0))
83
+ return error_img
84
+
85
+
86
+ css = """
87
+ #col-container {
88
+ margin: 0 auto;
89
+ max-width: 520px;
90
+ }
91
+ """
92
+
93
+ with gr.Blocks(css=css) as demo:
94
+
95
+ with gr.Column(elem_id="col-container"):
96
+
97
+ with gr.Row():
98
+ prompt = gr.Text(
99
+ label="Prompt",
100
+ show_label=False,
101
+ max_lines=1,
102
+ placeholder="Enter your prompt (keep it under 60 words for best results)",
103
+ container=False,
104
+ )
105
+
106
+ run_button = gr.Button("Run", scale=0)
107
+
108
+ result = gr.Image(label="Result", show_label=False)
109
+
110
+ with gr.Accordion("Advanced Settings", open=False):
111
+
112
+ negative_prompt = gr.Text(
113
+ label="Negative prompt",
114
+ max_lines=1,
115
+ placeholder="Enter a negative prompt",
116
+ value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn"
117
+ )
118
+
119
+ seed = gr.Slider(
120
+ label="Seed",
121
+ minimum=0,
122
+ maximum=MAX_SEED,
123
+ step=1,
124
+ value=0,
125
+ )
126
+
127
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
128
+
129
+ with gr.Row():
130
+ width = gr.Slider(
131
+ label="Width",
132
+ minimum=256,
133
+ maximum=MAX_IMAGE_SIZE,
134
+ step=32,
135
+ value=1024,
136
+ )
137
+
138
+ height = gr.Slider(
139
+ label="Height",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
+ value=1024,
144
+ )
145
+
146
+ with gr.Row():
147
+ guidance_scale = gr.Slider(
148
+ label="Guidance scale",
149
+ minimum=0.0,
150
+ maximum=20.0,
151
+ step=0.1,
152
+ value=7,
153
+ )
154
+
155
+ num_inference_steps = gr.Slider(
156
+ label="Number of inference steps",
157
+ minimum=1,
158
+ maximum=28,
159
+ step=1,
160
+ value=28,
161
+ )
162
+
163
+ run_button.click(
164
+ fn=infer,
165
+ inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
166
+ outputs=[result]
167
+ )
168
+
169
+ demo.queue().launch()