fantaxy commited on
Commit
2a602ea
Β·
verified Β·
1 Parent(s): 2c08108

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -22
app.py CHANGED
@@ -46,42 +46,68 @@ def split_image(input_image, num_splits=8):
46
 
47
  @spaces.GPU
48
  def infer(prompt, seed=1, randomize_seed=False, num_inference_steps=20, progress=gr.Progress()):
49
- progress(0, desc="Starting...")
50
- # ν”„λ‘¬ν”„νŠΈ ν…œν”Œλ¦Ώ κ°œμ„ 
51
- prompt_template = f"A sequence of 8 distinct consecutive frames showing {prompt}. Each frame should be clearly different and show progression of motion from left to right. High quality, detailed animation frames."
52
 
53
  if randomize_seed:
54
  seed = random.randint(0, MAX_SEED)
55
 
56
- progress(0.3, desc="Generating animation...")
57
- generator = torch.Generator().manual_seed(seed)
58
 
59
- # 생성 λ§€κ°œλ³€μˆ˜ μ‘°μ •
60
- image = pipe(
61
- prompt=prompt_template,
62
- num_inference_steps=num_inference_steps,
63
- num_images_per_prompt=1,
64
- generator=generator,
65
- height=320,
66
- width=2560,
67
- guidance_scale=8.5, # ν’ˆμ§ˆ ν–₯상을 μœ„ν•΄ μ•½κ°„ 증가
68
- ).images[0]
 
 
 
 
 
 
 
 
 
 
69
 
70
- progress(0.8, desc="Creating GIF...")
71
  gif_name = f"{uuid.uuid4().hex}-flux.gif"
72
 
73
- # GIF 생성 μ΅œμ ν™”
74
- frames = split_image(image, 8)
75
-
76
- # ν”„λ ˆμž„ κ°„ μ „ν™˜μ„ λΆ€λ“œλŸ½κ²Œ ν•˜κΈ° μœ„ν•œ 처리
77
  export_to_gif(
78
  frames,
79
  gif_name,
80
- fps=8, # fps μ¦κ°€λ‘œ 더 λΆ€λ“œλŸ¬μš΄ μ• λ‹ˆλ©”μ΄μ…˜
81
  )
82
 
 
 
 
 
 
 
83
  progress(1.0, desc="Done!")
84
- return gif_name, image, seed
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # ν”„λ‘¬ν”„νŠΈ μ˜ˆμ œλ„ 더 λͺ…ν™•ν•˜κ²Œ μˆ˜μ •
87
  examples = [
 
46
 
47
  @spaces.GPU
48
  def infer(prompt, seed=1, randomize_seed=False, num_inference_steps=20, progress=gr.Progress()):
49
+ progress(0, desc="Initializing...")
50
+ # 각 ν”„λ ˆμž„μ΄ 독립적이도둝 ν”„λ‘¬ν”„νŠΈ μˆ˜μ •
51
+ prompt_template = f"A single clear frame of {prompt}. The scene should show only one moment of the action, high quality, detailed, centered composition."
52
 
53
  if randomize_seed:
54
  seed = random.randint(0, MAX_SEED)
55
 
56
+ frames = []
57
+ total_frames = 8
58
 
59
+ # 각 ν”„λ ˆμž„μ„ κ°œλ³„μ μœΌλ‘œ 생성
60
+ for i in range(total_frames):
61
+ progress((i / total_frames) * 0.8, desc=f"Generating frame {i+1}/{total_frames}")
62
+ frame_prompt = f"{prompt_template} Frame {i+1} of sequence."
63
+
64
+ # 각 ν”„λ ˆμž„μ— λŒ€ν•΄ κ°œλ³„ μ‹œλ“œ 생성
65
+ frame_seed = seed + i
66
+ generator = torch.Generator().manual_seed(frame_seed)
67
+
68
+ frame = pipe(
69
+ prompt=frame_prompt,
70
+ num_inference_steps=num_inference_steps,
71
+ num_images_per_prompt=1,
72
+ generator=generator,
73
+ height=320,
74
+ width=320, # 단일 ν”„λ ˆμž„ 크기둜 μˆ˜μ •
75
+ guidance_scale=7.5,
76
+ ).images[0]
77
+
78
+ frames.append(frame)
79
 
80
+ progress(0.9, desc="Creating GIF...")
81
  gif_name = f"{uuid.uuid4().hex}-flux.gif"
82
 
83
+ # GIF 생성
 
 
 
84
  export_to_gif(
85
  frames,
86
  gif_name,
87
+ fps=8
88
  )
89
 
90
+ # ν”„λ ˆμž„λ“€μ„ κ°€λ‘œλ‘œ μ—°κ²°ν•˜μ—¬ 미리보기 이미지 생성
91
+ total_width = 320 * total_frames
92
+ preview_image = Image.new('RGB', (total_width, 320))
93
+ for i, frame in enumerate(frames):
94
+ preview_image.paste(frame, (i * 320, 0))
95
+
96
  progress(1.0, desc="Done!")
97
+ return gif_name, preview_image, seed
98
+
99
+ def create_preview_image(frames):
100
+ """ν”„λ ˆμž„λ“€μ„ κ°€λ‘œλ‘œ μ—°κ²°ν•˜μ—¬ 미리보기 이미지 생성"""
101
+ total_width = sum(frame.width for frame in frames)
102
+ max_height = max(frame.height for frame in frames)
103
+
104
+ preview = Image.new('RGB', (total_width, max_height))
105
+ x_offset = 0
106
+ for frame in frames:
107
+ preview.paste(frame, (x_offset, 0))
108
+ x_offset += frame.width
109
+
110
+ return preview
111
 
112
  # ν”„λ‘¬ν”„νŠΈ μ˜ˆμ œλ„ 더 λͺ…ν™•ν•˜κ²Œ μˆ˜μ •
113
  examples = [