openfree commited on
Commit
12bb0b1
Β·
verified Β·
1 Parent(s): 068452f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -33
app.py CHANGED
@@ -238,7 +238,7 @@ def video_to_audio(video_path, prompt, negative_prompt="music", seed=0, num_step
238
  traceback.print_exc()
239
  return video_path
240
 
241
- def generate_video_from_image(image, prompt="", length=4.0, sound_generation="μ‚¬μš΄λ“œ μ—†μŒ", sound_prompt="", sound_negative_prompt="music"):
242
  if image is None:
243
  return None
244
 
@@ -248,7 +248,7 @@ def generate_video_from_image(image, prompt="", length=4.0, sound_generation="
248
  temp_path = fp.name
249
  Image.fromarray(image).save(temp_path)
250
 
251
- # API 호좜 (μ›λž˜ μ½”λ“œμ™€ 동일)
252
  client = Client(VIDEO_API_URL)
253
  result = client.predict(
254
  input_image=handle_file(temp_path),
@@ -264,40 +264,31 @@ def generate_video_from_image(image, prompt="", length=4.0, sound_generation="
264
 
265
  if result and len(result) > 0:
266
  video_dict = result[0]
267
- video_path = video_dict.get("video") if isinstance(video_dict, dict) else None
268
-
269
- # λΉ„λ””μ˜€ 생성이 μ„±κ³΅ν•œ κ²½μš°μ—λ§Œ μ‚¬μš΄λ“œ μΆ”κ°€
270
- if video_path:
271
- # μ‚¬μš΄λ“œ 생성 μ˜΅μ…˜μ΄ μ„ νƒλœ 경우
272
- if sound_generation == "μ‚¬μš΄λ“œ 생성" and MMAUDIO_LOADED:
273
- try:
274
- # μ‚¬μš΄λ“œ ν”„λ‘¬ν”„νŠΈκ°€ λΉ„μ–΄μžˆμœΌλ©΄ κΈ°λ³Έκ°’ μ‚¬μš©
275
- if not sound_prompt:
276
- sound_prompt = prompt if prompt else "ambient sound"
277
-
278
- # λΉ„λ””μ˜€μ— μ‚¬μš΄λ“œ μΆ”κ°€
279
- video_with_sound = video_to_audio(
280
- video_path=video_path,
281
- prompt=sound_prompt,
282
- negative_prompt=sound_negative_prompt,
283
- seed=random.randint(0, 9999999),
284
- num_steps=25,
285
- cfg_strength=4.5,
286
- target_duration=length
287
- )
288
- return video_with_sound
289
- except Exception as e:
290
- logging.error(f"Sound generation error: {str(e)}")
291
- # μ‚¬μš΄λ“œ 생성 μ‹€νŒ¨ μ‹œ 원본 λΉ„λ””μ˜€ λ°˜ν™˜
292
- return video_path
293
-
294
- # μ‚¬μš΄λ“œ 생성을 μ„ νƒν•˜μ§€ μ•Šμ€ 경우 원본 λΉ„λ””μ˜€ λ°˜ν™˜
295
- return video_path
296
 
297
  except Exception as e:
298
  logging.error(f"Video generation error: {str(e)}")
299
  return None
300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  def prepare_image_and_mask(image, width, height, overlap_percentage, alignment):
302
  """이미지와 마슀크λ₯Ό μ€€λΉ„ν•˜λŠ” ν•¨μˆ˜"""
303
  if image is None:
@@ -626,12 +617,22 @@ with demo:
626
  [sound_generation],
627
  [sound_options]
628
  )
629
-
 
630
  video_btn.click(
631
  generate_video_from_image,
632
- [output_image, video_prompt, video_length, sound_generation, sound_prompt, sound_negative_prompt],
633
  [output_video]
634
  )
 
 
 
 
 
 
 
 
 
635
 
636
  # 이벀트 μ—°κ²° - 두 번째 νƒ­
637
  outpaint_size_preset.change(update_dimensions, [outpaint_size_preset], [outpaint_width, outpaint_height])
 
238
  traceback.print_exc()
239
  return video_path
240
 
241
+ def generate_video_from_image(image, prompt="", length=4.0):
242
  if image is None:
243
  return None
244
 
 
248
  temp_path = fp.name
249
  Image.fromarray(image).save(temp_path)
250
 
251
+ # API 호좜
252
  client = Client(VIDEO_API_URL)
253
  result = client.predict(
254
  input_image=handle_file(temp_path),
 
264
 
265
  if result and len(result) > 0:
266
  video_dict = result[0]
267
+ return video_dict.get("video") if isinstance(video_dict, dict) else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
  except Exception as e:
270
  logging.error(f"Video generation error: {str(e)}")
271
  return None
272
 
273
+ def add_sound_to_video(video_path, sound_prompt, sound_negative_prompt="music"):
274
+ if not video_path or not MMAUDIO_LOADED:
275
+ return video_path
276
+
277
+ try:
278
+ return video_to_audio(
279
+ video_path=video_path,
280
+ prompt=sound_prompt,
281
+ negative_prompt=sound_negative_prompt,
282
+ seed=random.randint(0, 9999999),
283
+ num_steps=25,
284
+ cfg_strength=4.5,
285
+ target_duration=8.0 # κΈ°λ³Έκ°’ μ‚¬μš©
286
+ )
287
+ except Exception as e:
288
+ logging.error(f"Sound addition error: {str(e)}")
289
+ return video_path
290
+
291
+
292
  def prepare_image_and_mask(image, width, height, overlap_percentage, alignment):
293
  """이미지와 마슀크λ₯Ό μ€€λΉ„ν•˜λŠ” ν•¨μˆ˜"""
294
  if image is None:
 
617
  [sound_generation],
618
  [sound_options]
619
  )
620
+
621
+
622
  video_btn.click(
623
  generate_video_from_image,
624
+ [output_image, video_prompt, video_length], # μ›λž˜λŒ€λ‘œ 3개 λ§€κ°œλ³€μˆ˜λ§Œ
625
  [output_video]
626
  )
627
+
628
+ # μ‚¬μš΄λ“œ μΆ”κ°€λŠ” 별도 λ²„νŠΌμœΌλ‘œ
629
+ sound_btn = gr.Button("πŸ”Š λΉ„λ””μ˜€μ— μ‚¬μš΄λ“œ μΆ”κ°€", visible=False)
630
+ sound_btn.click(
631
+ add_sound_to_video,
632
+ [output_video, sound_prompt, sound_negative_prompt],
633
+ [output_video]
634
+ )
635
+
636
 
637
  # 이벀트 μ—°κ²° - 두 번째 νƒ­
638
  outpaint_size_preset.change(update_dimensions, [outpaint_size_preset], [outpaint_width, outpaint_height])