yaron123 commited on
Commit
f3a00bf
·
1 Parent(s): 7e44dd0
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +68 -124
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Photo Motion
3
  emoji: 💡
4
  colorFrom: yellow
5
  colorTo: gray
 
1
  ---
2
+ title: Song Cover Image Generator
3
  emoji: 💡
4
  colorFrom: yellow
5
  colorTo: gray
app.py CHANGED
@@ -50,18 +50,15 @@ base = "black-forest-labs/FLUX.1-schnell"
50
 
51
  # variable data
52
 
53
- additional_image = None
54
 
55
  # precision data
56
 
57
  seq=512
58
- fps=15
59
- width=768
60
- height=512
61
  image_steps=8
62
- video_steps=50
63
- img_accu=9
64
- vid_accu=7
65
 
66
  # ui data
67
 
@@ -124,24 +121,8 @@ function custom(){
124
 
125
  # torch pipes
126
 
127
- def disabled_safety_checker(images, clip_input):
128
- if len(images.shape)==4:
129
- num_images = images.shape[0]
130
- return images, [False]*num_images
131
- else:
132
- return images, False
133
-
134
  image_pipe = FluxPipeline.from_pretrained(base, torch_dtype=torch.bfloat16).to(device)
135
  image_pipe.enable_model_cpu_offload()
136
- image_pipe.safety_checker = None
137
-
138
- video_pipe = CogVideoXImageToVideoPipeline.from_pretrained(
139
- "THUDM/CogVideoX-5b-I2V",
140
- torch_dtype=torch.bfloat16
141
- ).to(device)
142
- video_pipe.vae.enable_tiling()
143
- video_pipe.vae.enable_slicing()
144
- video_pipe.safety_checker = None
145
 
146
  # functionality
147
 
@@ -149,12 +130,9 @@ def generate_random_string(length):
149
  characters = str(ascii_letters + digits)
150
  return ''.join(random.choice(characters) for _ in range(length))
151
 
152
- @spaces.GPU(duration=80)
153
- def pipe_generate(img,p1,p2,time,title):
154
- global pipe
155
-
156
- if img is None:
157
- img = image_pipe(
158
  prompt=p1,
159
  negative_prompt=p2,
160
  height=height,
@@ -164,133 +142,99 @@ def pipe_generate(img,p1,p2,time,title):
164
  num_inference_steps=image_steps,
165
  max_sequence_length=seq,
166
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
167
- ).images[0]
168
- additional_image = True
169
-
170
- if title != "":
171
- draw = ImageDraw.Draw(img)
172
- textheight=min(( width // 10 ), ( height // 5 ))
173
- rows = 1
174
- font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
175
- textwidth = draw.textlength(title,font)
176
- x = (width - textwidth) // 2
177
- y = (height - (textheight * rows // 2)) // 2
178
- draw.text((x, y), title, (255,255,255), font=font)
179
-
180
- additional_image = img if additional_image else None
181
-
182
- if time == 0.0:
183
- return img
184
-
185
- return video_pipe(
186
- prompt=p1,
187
- negative_prompt=p2.replace("textual content, ",""),
188
- image=img,
189
- num_inference_steps=video_steps,
190
- guidance_scale=vid_accu,
191
- num_videos_per_prompt=1,
192
- num_frames=(fps*time),
193
- generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
194
- ).frames[0]
195
-
196
- def handle_generate(*_inp):
197
 
198
- additional_image = None
199
 
200
- inp = list(_inp)
201
-
202
- #inp[1] = translate(inp[1],"english")
203
- #inp[2] = translate(inp[2],"english")
204
-
205
- if len(inp[2]) >= 2:
206
- inp[2] = "," + inp[2].strip(",").strip(" ")
207
-
208
- inp[2] = f"textual,labeled,divined,distorted,discontinuous,ugly,blurry,low resolution,motionless,static,wrong body anatomy{inp[2]}"
209
-
210
- if len(inp[1]) >= 2:
211
- inp[1] = "," + inp[1].strip(",").strip(" ")
212
-
213
- inp[1] = f'realistic,natural,genuine,reasonable,highly detailed{inp[1]}'
214
 
215
  print(f"""
216
-
217
  Positive: {inp[1]}
218
 
219
  Negative: {inp[2]}
220
-
221
  """)
222
 
223
- pipe_out = pipe_generate(*inp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
- name = generate_random_string(12) + ( ".png" if inp[3] == 0.0 else ".mp4" )
226
- if inp[3] == 0.0:
227
- pipe_out.save(name)
228
- else:
229
- export_to_video(pipe_out,name,fps=fps)
230
- if inp[3] == 0.0:
231
- return name, None
232
- else:
233
- return additional_image, name
234
 
235
  def ui():
236
- global result
237
  with gr.Blocks(theme=gr.themes.Citrus(),css=css,js=js) as demo:
238
  gr.Markdown(f"""
239
- # Photo Motion - PNG/MP4 Generator
240
  """)
241
  with gr.Row():
242
- title = gr.Textbox(
243
- placeholder="Logo title",
244
- container=False,
245
- max_lines=1
246
- )
247
- prompt = gr.Textbox(
248
- elem_id="prompt",
249
- placeholder="Included keywords",
250
- container=False,
251
- max_lines=1
252
- )
 
 
 
 
 
 
 
253
  with gr.Row():
254
- prompt2 = gr.Textbox(
255
- elem_id="prompt2",
256
- placeholder="Excluded keywords",
257
  container=False,
258
  max_lines=1
259
  )
260
  with gr.Row():
261
- time = gr.Slider(
262
- minimum=0.0,
263
- maximum=3.0,
264
- value=0.0,
265
- step=1.0,
266
- label="Duration (0s = PNG)"
267
- )
268
- with gr.Row(elem_id="col-container"):
269
- with gr.Column():
270
- img = gr.Image(label="Upload photo",show_label=True,container=False,type="pil")
271
- with gr.Column():
272
- res_img = gr.Image(interactive=False,container=False,elem_classes="image-container", label="Result", show_label=True, type='filepath', show_share_button=False)
273
- with gr.Column():
274
- res_vid = gr.Video(interactive=False,container=False,elem_classes="image-container", label="Result", show_label=True, show_share_button=False)
275
  with gr.Row():
276
- run_button = gr.Button("Start!",elem_classes="btn",scale=0)
277
 
278
  gr.on(
279
  triggers=[
280
- run_button.click,
281
- prompt.submit,
282
- prompt2.submit
283
  ],
284
  fn=handle_generate,
285
- inputs=[img,prompt,prompt2,time,title],
286
- outputs=[res_img,res_vid]
287
  )
288
  demo.queue().launch()
289
 
290
  # entry
291
 
292
  if __name__ == "__main__":
293
- os.chdir(os.path.abspath(os.path.dirname(__file__)))
294
  ui()
295
 
296
- # end
 
50
 
51
  # variable data
52
 
53
+
54
 
55
  # precision data
56
 
57
  seq=512
58
+ width=4320
59
+ height=4320
 
60
  image_steps=8
61
+ img_accu=0
 
 
62
 
63
  # ui data
64
 
 
121
 
122
  # torch pipes
123
 
 
 
 
 
 
 
 
124
  image_pipe = FluxPipeline.from_pretrained(base, torch_dtype=torch.bfloat16).to(device)
125
  image_pipe.enable_model_cpu_offload()
 
 
 
 
 
 
 
 
 
126
 
127
  # functionality
128
 
 
130
  characters = str(ascii_letters + digits)
131
  return ''.join(random.choice(characters) for _ in range(length))
132
 
133
+ @spaces.GPU()
134
+ def pipe_generate(p1,p2):
135
+ return image_pipe(
 
 
 
136
  prompt=p1,
137
  negative_prompt=p2,
138
  height=height,
 
142
  num_inference_steps=image_steps,
143
  max_sequence_length=seq,
144
  generator=torch.Generator(device).manual_seed(int(str(random.random()).split(".")[1]))
145
+ ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
+ def handle_generate(artist,song,genre,lyrics):
148
 
149
+ pos_artist = re.sub("([ \t\n]){1,}", " ", artist).strip()
150
+ pos_song = re.sub("([ \t\n]){1,}", " ", song).strip()
151
+ pos_song = ' '.join(word[0].upper() + word[1:] for word in pos_song.split())
152
+ pos_genre = re.sub(f'[{punctuation}]', '', re.sub("([ \t\n]){1,}", " ", genre)).upper().strip()
153
+ pos_lyrics = re.sub(f'[{punctuation}]', '', re.sub("([ \t\n]){1,}", " ", genre)).lower().strip()
154
+ neg = f"Textual Labeled Distorted Discontinuous Ugly Blurry"
155
+ pos = f'Realistic Natural Genuine Reasonable Detailed { pos_genre } GENRE SONG COVER FOR { pos_song }: "{ pos_lyrics }"'
 
 
 
 
 
 
 
156
 
157
  print(f"""
 
158
  Positive: {inp[1]}
159
 
160
  Negative: {inp[2]}
 
161
  """)
162
 
163
+ img = pipe_generate(pos,neg)
164
+
165
+ draw = ImageDraw.Draw(img)
166
+
167
+ rows = 1
168
+ labes_distance = 1 // 3
169
+
170
+ textheight=min(( width // 10 ), ( height // 5 ))
171
+ font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
172
+ textwidth = draw.textlength(pos_song,font)
173
+ x = (width - textwidth) // 2
174
+ y = (height - (textheight * rows // 2)) // 2
175
+ y = y - (y // labes_distance)
176
+ draw.text((x, y), pos_song, (255,255,255), font=font)
177
+
178
+ textheight=min(( width // 12 ), ( height // 6 ))
179
+ font = ImageFont.truetype(r"Alef-Bold.ttf", textheight)
180
+ textwidth = draw.textlength(pos_artist,font)
181
+ x = (width - textwidth) // 2
182
+ y = (height - (textheight * rows // 2)) // 2
183
+ y = y + (y // labes_distance)
184
+ draw.text((x, y), pos_artist, (255,255,255), font=font)
185
 
186
+ name = generate_random_string(12) + ".png"
187
+ img.save(name)
188
+ return name
 
 
 
 
 
 
189
 
190
  def ui():
 
191
  with gr.Blocks(theme=gr.themes.Citrus(),css=css,js=js) as demo:
192
  gr.Markdown(f"""
193
+ # Song Cover Image Generator
194
  """)
195
  with gr.Row():
196
+ with gr.Column():
197
+ artist = gr.Textbox(
198
+ placeholder="Artist name",
199
+ container=False,
200
+ max_lines=1
201
+ )
202
+ with gr.Column():
203
+ song = gr.Textbox(
204
+ placeholder="Song name",
205
+ container=False,
206
+ max_lines=1
207
+ )
208
+ with gr.Column():
209
+ genre = gr.Textbox(
210
+ placeholder="Genre",
211
+ container=False,
212
+ max_lines=1
213
+ )
214
  with gr.Row():
215
+ lyrics = gr.Textbox(
216
+ placeholder="Lyrics (English)",
 
217
  container=False,
218
  max_lines=1
219
  )
220
  with gr.Row():
221
+ cover = gr.Image(interactive=False,container=False,elem_classes="image-container", label="Result", show_label=True, type='filepath', show_share_button=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  with gr.Row():
223
+ run = gr.Button("Generate",elem_classes="btn")
224
 
225
  gr.on(
226
  triggers=[
227
+ run.click
 
 
228
  ],
229
  fn=handle_generate,
230
+ inputs=[artist,song,genre,lyrics],
231
+ outputs=[cover]
232
  )
233
  demo.queue().launch()
234
 
235
  # entry
236
 
237
  if __name__ == "__main__":
 
238
  ui()
239
 
240
+ # end