alfredplpl commited on
Commit
04e62f7
1 Parent(s): d4b205e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Thanks: https://huggingface.co/spaces/stabilityai/stable-diffusion-3-medium
2
+ import spaces
3
+ import os
4
+ import gradio as gr
5
+ import numpy as np
6
+ import random
7
+ import torch
8
+ from diffusers import StableDiffusion3Pipeline
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
10
+
11
+ device = "cuda"
12
+ dtype = torch.float16
13
+
14
+ repo = "stabilityai/stable-diffusion-3.5-large"
15
+ t2i = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.bfloat16, token=os.environ["TOKEN"]).to(device)
16
+
17
+ model = AutoModelForCausalLM.from_pretrained(
18
+ "microsoft/Phi-3-mini-4k-instruct",
19
+ device_map="cuda",
20
+ torch_dtype=torch.bfloat16,
21
+ trust_remote_code=True,
22
+ token=os.environ["TOKEN"]
23
+ )
24
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", token=os.environ["TOKEN"])
25
+ upsampler = pipeline(
26
+ "text-generation",
27
+ model=model,
28
+ tokenizer=tokenizer,
29
+ )
30
+
31
+ generation_args = {
32
+ "max_new_tokens": 226,
33
+ "return_full_text": False,
34
+ "temperature": 0.7,
35
+ "do_sample": True,
36
+ "top_p": 0.95
37
+ }
38
+
39
+ MAX_SEED = np.iinfo(np.int32).max
40
+ MAX_IMAGE_SIZE = 1344
41
+
42
+ @spaces.GPU
43
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
44
+ messages = [
45
+ {"role": "user", "content": "次のプロンプトを想像を膨らませて英語に翻訳してください。「クールなアニメ風の女の子」"},
46
+ {"role": "assistant", "content": "An anime style illustration of a cool-looking teenage girl with an edgy, confident expression. She has piercing eyes, a slight smirk, and colorful hair that flows in the wind. "},
47
+ {"role": "user", "content": "次のプロンプトを想像を膨らませて英語に翻訳してください。「実写風の女子高生」"},
48
+ {"role": "assistant", "content": "A photorealistic image of a female high school student standing on a city street. She is wearing a traditional Japanese school uniform, consisting of a navy blue blazer, a white blouse, and a knee-length plaid skirt. "},
49
+ {"role": "user", "content": f"次のプロンプトを想像を膨らませて英語に翻訳してください。「{prompt}」" },
50
+ ]
51
+ output = upsampler(messages, **generation_args)
52
+ upsampled_prompt=output[0]['generated_text']
53
+ print(upsampled_prompt)
54
+
55
+ if randomize_seed:
56
+ seed = random.randint(0, MAX_SEED)
57
+
58
+ generator = torch.Generator().manual_seed(seed)
59
+
60
+ image = t2i(
61
+ prompt = upsampled_prompt,
62
+ negative_prompt = negative_prompt,
63
+ guidance_scale = guidance_scale,
64
+ num_inference_steps = num_inference_steps,
65
+ width = width,
66
+ height = height,
67
+ generator = generator
68
+ ).images[0]
69
+
70
+ return image, seed, upsampled_prompt
71
+
72
+ examples = [
73
+ "美味しい肉",
74
+ "馬に乗った宇宙飛行士",
75
+ "アニメ風の美少女",
76
+ "女子高生の写真",
77
+ "寿司でできた家に入っているコーギー",
78
+ "バナナとアボカドが戦っている様子"
79
+ ]
80
+
81
+ css="""
82
+ #col-container {
83
+ margin: 0 auto;
84
+ max-width: 580px;
85
+ }
86
+ """
87
+
88
+ with gr.Blocks(css=css) as demo:
89
+
90
+ with gr.Column(elem_id="col-container"):
91
+ gr.Markdown(f"""
92
+ # 日本語が入力できる SD3.5 Large
93
+ """)
94
+
95
+ with gr.Row():
96
+
97
+ prompt = gr.Text(
98
+ label="プロンプト",
99
+ show_label=False,
100
+ max_lines=1,
101
+ placeholder="作りたい画像の特徴を入力してください",
102
+ container=False,
103
+ )
104
+
105
+ run_button = gr.Button("実行", scale=0)
106
+
107
+ result = gr.Image(label="結果", show_label=False)
108
+ generated_prompt = gr.Textbox(label="生成に使ったプロンプト", show_label=False, interactive=False)
109
+
110
+ with gr.Accordion("詳細設定", open=False):
111
+
112
+ negative_prompt = gr.Text(
113
+ label="ネガティブプロンプト",
114
+ max_lines=1,
115
+ placeholder="画像から排除したい要素を入力してください",
116
+ )
117
+
118
+ seed = gr.Slider(
119
+ label="乱数のシード",
120
+ minimum=0,
121
+ maximum=MAX_SEED,
122
+ step=1,
123
+ value=0,
124
+ )
125
+
126
+ randomize_seed = gr.Checkbox(label="ランダム生成", value=True)
127
+
128
+ with gr.Row():
129
+
130
+ width = gr.Slider(
131
+ label="横",
132
+ minimum=256,
133
+ maximum=MAX_IMAGE_SIZE,
134
+ step=64,
135
+ value=1024,
136
+ )
137
+
138
+ height = gr.Slider(
139
+ label="縦",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=64,
143
+ value=1024,
144
+ )
145
+
146
+ with gr.Row():
147
+
148
+ guidance_scale = gr.Slider(
149
+ label="プロンプトの忠実さ",
150
+ minimum=0.0,
151
+ maximum=10.0,
152
+ step=0.1,
153
+ value=3.5,
154
+ )
155
+
156
+ num_inference_steps = gr.Slider(
157
+ label="推論回数",
158
+ minimum=1,
159
+ maximum=50,
160
+ step=1,
161
+ value=28,
162
+ )
163
+
164
+ gr.Examples(
165
+ examples = examples,
166
+ inputs = [prompt]
167
+ )
168
+ gr.on(
169
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
170
+ fn = infer,
171
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
172
+ outputs = [result, seed, generated_prompt]
173
+ )
174
+
175
+ demo.launch()