Spaces:
Restarting
on
Zero
Restarting
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -42,9 +42,9 @@ os.environ["HF_HOME"] = cache_path
|
|
42 |
# CUDA 설정
|
43 |
torch.backends.cuda.matmul.allow_tf32 = True
|
44 |
|
45 |
-
# 번역기 초기화
|
46 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
47 |
|
|
|
|
|
48 |
|
49 |
if not path.exists(cache_path):
|
50 |
os.makedirs(cache_path, exist_ok=True)
|
@@ -252,22 +252,110 @@ def save_image(image):
|
|
252 |
logger.error(f"Error in save_image: {str(e)}")
|
253 |
return None
|
254 |
|
255 |
-
|
256 |
-
|
257 |
css = """
|
258 |
-
footer {
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
}
|
261 |
"""
|
262 |
|
263 |
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
266 |
gr.HTML('<div class="title">🎥 Dokdo✨ Digital Odyssey from Korea, Designing Original</div>')
|
267 |
gr.HTML('<div class="title">😄 Enjoy the amazing free video creation and enhancement services!</div>')
|
268 |
-
|
269 |
with gr.Tabs():
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
with gr.Row():
|
272 |
with gr.Column(scale=3):
|
273 |
img_prompt = gr.Textbox(
|
@@ -278,61 +366,20 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
278 |
|
279 |
with gr.Accordion("Advanced Settings", open=False):
|
280 |
with gr.Row():
|
281 |
-
height = gr.Slider(
|
282 |
-
|
283 |
-
minimum=256,
|
284 |
-
maximum=1152,
|
285 |
-
step=64,
|
286 |
-
value=1024
|
287 |
-
)
|
288 |
-
width = gr.Slider(
|
289 |
-
label="Width",
|
290 |
-
minimum=256,
|
291 |
-
maximum=1152,
|
292 |
-
step=64,
|
293 |
-
value=1024
|
294 |
-
)
|
295 |
-
|
296 |
with gr.Row():
|
297 |
-
steps = gr.Slider(
|
298 |
-
|
299 |
-
|
300 |
-
maximum=25,
|
301 |
-
step=1,
|
302 |
-
value=8
|
303 |
-
)
|
304 |
-
scales = gr.Slider(
|
305 |
-
label="Guidance Scale",
|
306 |
-
minimum=0.0,
|
307 |
-
maximum=5.0,
|
308 |
-
step=0.1,
|
309 |
-
value=3.5
|
310 |
-
)
|
311 |
-
|
312 |
-
def get_random_seed():
|
313 |
-
return torch.randint(0, 1000000, (1,)).item()
|
314 |
-
|
315 |
-
seed = gr.Number(
|
316 |
-
label="Seed",
|
317 |
-
value=get_random_seed(),
|
318 |
-
precision=0
|
319 |
-
)
|
320 |
-
|
321 |
randomize_seed = gr.Button("🎲 Randomize Seed", elem_classes=["generate-btn"])
|
322 |
|
323 |
-
generate_btn = gr.Button(
|
324 |
-
"✨ Generate Image",
|
325 |
-
elem_classes=["generate-btn"]
|
326 |
-
)
|
327 |
|
328 |
with gr.Column(scale=4):
|
329 |
-
img_output = gr.Image(
|
330 |
-
label="Generated Image",
|
331 |
-
type="pil",
|
332 |
-
format="png"
|
333 |
-
)
|
334 |
-
|
335 |
|
|
|
336 |
with gr.Tab("Amazing Video Generation"):
|
337 |
with gr.Row():
|
338 |
with gr.Column(scale=3):
|
@@ -341,25 +388,19 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
341 |
placeholder="비디오 설명을 입력하세요... (한글 입력 가능)",
|
342 |
lines=3
|
343 |
)
|
344 |
-
upload_image = gr.Image(
|
345 |
-
|
346 |
-
label="Upload First Frame Image"
|
347 |
-
)
|
348 |
-
video_generate_btn = gr.Button(
|
349 |
-
"🎬 Generate Video",
|
350 |
-
elem_classes=["generate-btn"]
|
351 |
-
)
|
352 |
|
353 |
with gr.Column(scale=4):
|
354 |
video_output = gr.Video(label="Generated Video")
|
355 |
-
|
356 |
@spaces.GPU
|
357 |
def process_and_save_image(height, width, steps, scales, prompt, seed):
|
358 |
is_safe, translated_prompt = process_prompt(prompt)
|
359 |
if not is_safe:
|
360 |
gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
|
361 |
return None
|
362 |
-
|
363 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
364 |
try:
|
365 |
generated_image = pipe(
|
@@ -371,7 +412,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
371 |
width=int(width),
|
372 |
max_sequence_length=256
|
373 |
).images[0]
|
374 |
-
|
375 |
if not isinstance(generated_image, Image.Image):
|
376 |
generated_image = Image.fromarray(generated_image)
|
377 |
|
@@ -386,43 +427,50 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
386 |
logger.error(f"Error in image generation: {str(e)}")
|
387 |
return None
|
388 |
|
389 |
-
|
390 |
-
|
391 |
def process_and_generate_video(image, prompt):
|
392 |
is_safe, translated_prompt = process_prompt(prompt)
|
393 |
if not is_safe:
|
394 |
gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
|
395 |
return None
|
396 |
return generate_video(image, translated_prompt)
|
397 |
-
|
398 |
def update_seed():
|
399 |
return get_random_seed()
|
400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
generate_btn.click(
|
402 |
process_and_save_image,
|
403 |
inputs=[height, width, steps, scales, img_prompt, seed],
|
404 |
outputs=img_output
|
405 |
)
|
406 |
-
|
407 |
video_generate_btn.click(
|
408 |
process_and_generate_video,
|
409 |
inputs=[upload_image, video_prompt],
|
410 |
outputs=video_output
|
411 |
)
|
412 |
-
|
413 |
-
|
414 |
-
update_seed,
|
415 |
-
outputs=[seed]
|
416 |
-
)
|
417 |
-
|
418 |
-
generate_btn.click(
|
419 |
-
update_seed,
|
420 |
-
outputs=[seed]
|
421 |
-
)
|
422 |
|
423 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
424 |
demo.launch(
|
425 |
server_name="0.0.0.0",
|
426 |
server_port=7860,
|
427 |
-
share=
|
428 |
)
|
|
|
42 |
# CUDA 설정
|
43 |
torch.backends.cuda.matmul.allow_tf32 = True
|
44 |
|
|
|
|
|
45 |
|
46 |
+
# 번역기 초기화 부분 수정
|
47 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device="cuda" if torch.cuda.is_available() else "cpu")
|
48 |
|
49 |
if not path.exists(cache_path):
|
50 |
os.makedirs(cache_path, exist_ok=True)
|
|
|
252 |
logger.error(f"Error in save_image: {str(e)}")
|
253 |
return None
|
254 |
|
|
|
|
|
255 |
css = """
|
256 |
+
footer {display: none}
|
257 |
+
.gradio-container {max-width: 1200px !important}
|
258 |
+
#gallery {
|
259 |
+
margin: 20px auto;
|
260 |
+
padding: 20px;
|
261 |
+
}
|
262 |
+
#gallery img {
|
263 |
+
width: 300px !important;
|
264 |
+
height: 300px !important;
|
265 |
+
object-fit: cover;
|
266 |
+
border-radius: 8px;
|
267 |
+
}
|
268 |
+
.gallery-item {
|
269 |
+
margin: 0 !important;
|
270 |
+
padding: 5px !important;
|
271 |
+
}
|
272 |
+
#video_player {
|
273 |
+
margin: 20px auto;
|
274 |
+
max-width: 800px;
|
275 |
+
}
|
276 |
+
.title {
|
277 |
+
text-align: center;
|
278 |
+
font-size: 1.5em;
|
279 |
+
margin: 10px 0;
|
280 |
}
|
281 |
"""
|
282 |
|
283 |
|
284 |
+
def get_random_seed():
|
285 |
+
return torch.randint(0, 1000000, (1,)).item()
|
286 |
+
|
287 |
+
|
288 |
+
def create_thumbnail_gallery():
|
289 |
+
# 0부터 9까지의 이미지 리스트 생성
|
290 |
+
return [
|
291 |
+
"image/0.jpg", "image/1.jpg", "image/2.jpg",
|
292 |
+
"image/3.jpg", "image/4.jpg", "image/5.jpg",
|
293 |
+
"image/6.jpg", "image/7.jpg", "image/8.jpg",
|
294 |
+
"image/9.jpg"
|
295 |
+
]
|
296 |
+
|
297 |
+
def check_image_files():
|
298 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
299 |
+
missing_files = []
|
300 |
+
|
301 |
+
for i in range(10): # 0부터 9까지 확인
|
302 |
+
image_path = os.path.join(current_dir, f"image/{i}.jpg")
|
303 |
+
video_path = os.path.join(current_dir, f"image/{i}.mp4")
|
304 |
+
if not os.path.exists(image_path):
|
305 |
+
missing_files.append(f"{i}.jpg")
|
306 |
+
if not os.path.exists(video_path):
|
307 |
+
missing_files.append(f"{i}.mp4")
|
308 |
+
|
309 |
+
if missing_files:
|
310 |
+
logger.error(f"Missing files: {', '.join(missing_files)}")
|
311 |
+
return False
|
312 |
+
return True
|
313 |
+
|
314 |
+
def load_gallery_images():
|
315 |
+
gallery_images = []
|
316 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
317 |
+
|
318 |
+
try:
|
319 |
+
for i in range(10): # 0부터 9까지 로드
|
320 |
+
image_path = os.path.join(current_dir, f"image/{i}.jpg")
|
321 |
+
if os.path.exists(image_path):
|
322 |
+
img = Image.open(image_path)
|
323 |
+
gallery_images.append(img)
|
324 |
+
else:
|
325 |
+
logger.warning(f"Image not found: {image_path}")
|
326 |
+
except Exception as e:
|
327 |
+
logger.error(f"Error loading gallery images: {str(e)}")
|
328 |
+
|
329 |
+
return gallery_images
|
330 |
+
|
331 |
+
# UI 부분 수정
|
332 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
333 |
gr.HTML('<div class="title">🎥 Dokdo✨ Digital Odyssey from Korea, Designing Original</div>')
|
334 |
gr.HTML('<div class="title">😄 Enjoy the amazing free video creation and enhancement services!</div>')
|
335 |
+
|
336 |
with gr.Tabs():
|
337 |
+
# 첫 번째 탭: Example Gallery
|
338 |
+
with gr.Tab("Example Gallery"):
|
339 |
+
with gr.Row():
|
340 |
+
gallery = gr.Gallery(
|
341 |
+
value=create_thumbnail_gallery(),
|
342 |
+
columns=[5], # 한 줄에 5개씩 표시
|
343 |
+
rows=[2], # 2줄로 표시
|
344 |
+
height="auto",
|
345 |
+
show_label=False,
|
346 |
+
elem_id="gallery"
|
347 |
+
)
|
348 |
+
|
349 |
+
with gr.Row():
|
350 |
+
video_player = gr.Video(
|
351 |
+
label="Selected Video",
|
352 |
+
elem_id="video_player",
|
353 |
+
interactive=False,
|
354 |
+
autoplay=True
|
355 |
+
)
|
356 |
+
|
357 |
+
# 두 번째 탭: Image Generation
|
358 |
+
with gr.Tab("Image Generation & Enhanced"):
|
359 |
with gr.Row():
|
360 |
with gr.Column(scale=3):
|
361 |
img_prompt = gr.Textbox(
|
|
|
366 |
|
367 |
with gr.Accordion("Advanced Settings", open=False):
|
368 |
with gr.Row():
|
369 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1152, step=64, value=1024)
|
370 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1152, step=64, value=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
with gr.Row():
|
372 |
+
steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8)
|
373 |
+
scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, step=0.1, value=3.5)
|
374 |
+
seed = gr.Number(label="Seed", value=get_random_seed(), precision=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
randomize_seed = gr.Button("🎲 Randomize Seed", elem_classes=["generate-btn"])
|
376 |
|
377 |
+
generate_btn = gr.Button("✨ Generate Image", elem_classes=["generate-btn"])
|
|
|
|
|
|
|
378 |
|
379 |
with gr.Column(scale=4):
|
380 |
+
img_output = gr.Image(label="Generated Image", type="pil", format="png")
|
|
|
|
|
|
|
|
|
|
|
381 |
|
382 |
+
# 세 번째 탭: Video Generation
|
383 |
with gr.Tab("Amazing Video Generation"):
|
384 |
with gr.Row():
|
385 |
with gr.Column(scale=3):
|
|
|
388 |
placeholder="비디오 설명을 입력하세요... (한글 입력 가능)",
|
389 |
lines=3
|
390 |
)
|
391 |
+
upload_image = gr.Image(type="filepath", label="Upload First Frame Image")
|
392 |
+
video_generate_btn = gr.Button("🎬 Generate Video", elem_classes=["generate-btn"])
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
with gr.Column(scale=4):
|
395 |
video_output = gr.Video(label="Generated Video")
|
396 |
+
|
397 |
@spaces.GPU
|
398 |
def process_and_save_image(height, width, steps, scales, prompt, seed):
|
399 |
is_safe, translated_prompt = process_prompt(prompt)
|
400 |
if not is_safe:
|
401 |
gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
|
402 |
return None
|
403 |
+
|
404 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
405 |
try:
|
406 |
generated_image = pipe(
|
|
|
412 |
width=int(width),
|
413 |
max_sequence_length=256
|
414 |
).images[0]
|
415 |
+
|
416 |
if not isinstance(generated_image, Image.Image):
|
417 |
generated_image = Image.fromarray(generated_image)
|
418 |
|
|
|
427 |
logger.error(f"Error in image generation: {str(e)}")
|
428 |
return None
|
429 |
|
|
|
|
|
430 |
def process_and_generate_video(image, prompt):
|
431 |
is_safe, translated_prompt = process_prompt(prompt)
|
432 |
if not is_safe:
|
433 |
gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
|
434 |
return None
|
435 |
return generate_video(image, translated_prompt)
|
436 |
+
|
437 |
def update_seed():
|
438 |
return get_random_seed()
|
439 |
+
|
440 |
+
|
441 |
+
# 이벤트 핸들러 수정
|
442 |
+
def show_video(evt: gr.SelectData):
|
443 |
+
video_num = evt.index # 0부터 시작하는 인덱스
|
444 |
+
video_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), f"image/{video_num}.mp4")
|
445 |
+
if os.path.exists(video_path):
|
446 |
+
return video_path
|
447 |
+
return None
|
448 |
+
|
449 |
+
# 이벤트 연결
|
450 |
+
gallery.select(fn=show_video, outputs=video_player)
|
451 |
+
|
452 |
+
|
453 |
generate_btn.click(
|
454 |
process_and_save_image,
|
455 |
inputs=[height, width, steps, scales, img_prompt, seed],
|
456 |
outputs=img_output
|
457 |
)
|
|
|
458 |
video_generate_btn.click(
|
459 |
process_and_generate_video,
|
460 |
inputs=[upload_image, video_prompt],
|
461 |
outputs=video_output
|
462 |
)
|
463 |
+
randomize_seed.click(update_seed, outputs=[seed])
|
464 |
+
generate_btn.click(update_seed, outputs=[seed])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
465 |
|
466 |
if __name__ == "__main__":
|
467 |
+
# 이미지와 비디오 파일 존재 확인
|
468 |
+
if not check_image_files():
|
469 |
+
print("Error: Required image and video files (0.jpg through 9.jpg and 0.mp4 through 9.mp4) are missing!")
|
470 |
+
sys.exit(1)
|
471 |
+
|
472 |
demo.launch(
|
473 |
server_name="0.0.0.0",
|
474 |
server_port=7860,
|
475 |
+
share=False
|
476 |
)
|