fantaxy commited on
Commit
29675eb
·
verified ·
1 Parent(s): 455b50b

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -856
app.py DELETED
@@ -1,856 +0,0 @@
1
- import spaces
2
- from functools import lru_cache
3
- import gradio as gr
4
- from gradio_toggle import Toggle
5
- import torch
6
- from huggingface_hub import snapshot_download
7
- from transformers import CLIPProcessor, CLIPModel, pipeline
8
- import random
9
- from xora.models.autoencoders.causal_video_autoencoder import CausalVideoAutoencoder
10
- from xora.models.transformers.transformer3d import Transformer3DModel
11
- from xora.models.transformers.symmetric_patchifier import SymmetricPatchifier
12
- from xora.schedulers.rf import RectifiedFlowScheduler
13
- from xora.pipelines.pipeline_xora_video import XoraVideoPipeline
14
- from transformers import T5EncoderModel, T5Tokenizer
15
- from xora.utils.conditioning_method import ConditioningMethod
16
- from pathlib import Path
17
- import safetensors.torch
18
- import json
19
- import numpy as np
20
- import cv2
21
- from PIL import Image
22
- import tempfile
23
- import os
24
- import gc
25
- import csv
26
- from datetime import datetime
27
- from openai import OpenAI
28
-
29
-
30
-
31
- import argparse
32
- import time
33
- from os import path
34
- import shutil
35
- from datetime import datetime
36
- from safetensors.torch import load_file
37
- from diffusers import FluxPipeline
38
- from diffusers.pipelines.stable_diffusion import safety_checker
39
- from PIL import Image
40
- from transformers import pipeline
41
- import replicate
42
- import logging
43
- import requests
44
- from pathlib import Path
45
- import sys
46
- import io
47
-
48
- # 한글-영어 번역기 초기화
49
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
50
-
51
- torch.backends.cuda.matmul.allow_tf32 = False
52
- torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
53
- torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
54
- torch.backends.cudnn.allow_tf32 = False
55
- torch.backends.cudnn.deterministic = False
56
- torch.backends.cuda.preferred_blas_library="cublas"
57
- torch.set_float32_matmul_precision("highest")
58
-
59
- MAX_SEED = np.iinfo(np.int32).max
60
-
61
- # Load Hugging Face token if needed
62
- hf_token = os.getenv("HF_TOKEN")
63
- openai_api_key = os.getenv("OPENAI_API_KEY")
64
- client = OpenAI(api_key=openai_api_key)
65
-
66
- system_prompt_t2v_path = "assets/system_prompt_t2v.txt"
67
- with open(system_prompt_t2v_path, "r") as f:
68
- system_prompt_t2v = f.read()
69
-
70
- # Set model download directory within Hugging Face Spaces
71
- model_path = "asset"
72
-
73
- commit_hash='c7c8ad4c2ddba847b94e8bfaefbd30bd8669fafc'
74
-
75
- if not os.path.exists(model_path):
76
- snapshot_download("Lightricks/LTX-Video", revision=commit_hash, local_dir=model_path, repo_type="model", token=hf_token)
77
-
78
- # Global variables to load components
79
- vae_dir = Path(model_path) / "vae"
80
- unet_dir = Path(model_path) / "unet"
81
- scheduler_dir = Path(model_path) / "scheduler"
82
-
83
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
84
-
85
- clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32", cache_dir=model_path).to(torch.device("cuda:0"))
86
- clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32", cache_dir=model_path)
87
-
88
- def process_prompt(prompt):
89
- # 한글이 포함되어 있는지 확인
90
- if any(ord('가') <= ord(char) <= ord('힣') for char in prompt):
91
- # 한글을 영어로 번역
92
- translated = translator(prompt)[0]['translation_text']
93
- return translated
94
- return prompt
95
-
96
- def compute_clip_embedding(text=None):
97
- inputs = clip_processor(text=text, return_tensors="pt", padding=True).to(device)
98
- outputs = clip_model.get_text_features(**inputs)
99
- embedding = outputs.detach().cpu().numpy().flatten().tolist()
100
- return embedding
101
-
102
- def load_vae(vae_dir):
103
- vae_ckpt_path = vae_dir / "vae_diffusion_pytorch_model.safetensors"
104
- vae_config_path = vae_dir / "config.json"
105
- with open(vae_config_path, "r") as f:
106
- vae_config = json.load(f)
107
- vae = CausalVideoAutoencoder.from_config(vae_config)
108
- vae_state_dict = safetensors.torch.load_file(vae_ckpt_path)
109
- vae.load_state_dict(vae_state_dict)
110
- return vae.to(device).to(torch.bfloat16)
111
-
112
- def load_unet(unet_dir):
113
- unet_ckpt_path = unet_dir / "unet_diffusion_pytorch_model.safetensors"
114
- unet_config_path = unet_dir / "config.json"
115
- transformer_config = Transformer3DModel.load_config(unet_config_path)
116
- transformer = Transformer3DModel.from_config(transformer_config)
117
- unet_state_dict = safetensors.torch.load_file(unet_ckpt_path)
118
- transformer.load_state_dict(unet_state_dict, strict=True)
119
- return transformer.to(device).to(torch.bfloat16)
120
-
121
- def load_scheduler(scheduler_dir):
122
- scheduler_config_path = scheduler_dir / "scheduler_config.json"
123
- scheduler_config = RectifiedFlowScheduler.load_config(scheduler_config_path)
124
- return RectifiedFlowScheduler.from_config(scheduler_config)
125
-
126
- # Preset options for resolution and frame configuration
127
- preset_options = [
128
- {"label": "1216x704, 41 frames", "width": 1216, "height": 704, "num_frames": 41},
129
- {"label": "1088x704, 49 frames", "width": 1088, "height": 704, "num_frames": 49},
130
- {"label": "1056x640, 57 frames", "width": 1056, "height": 640, "num_frames": 57},
131
- {"label": "448x448, 100 frames", "width": 448, "height": 448, "num_frames": 100},
132
- {"label": "448x448, 200 frames", "width": 448, "height": 448, "num_frames": 200},
133
- {"label": "448x448, 300 frames", "width": 448, "height": 448, "num_frames": 300},
134
- {"label": "640x640, 80 frames", "width": 640, "height": 640, "num_frames": 80},
135
- {"label": "640x640, 120 frames", "width": 640, "height": 640, "num_frames": 120},
136
- {"label": "768x768, 64 frames", "width": 768, "height": 768, "num_frames": 64},
137
- {"label": "768x768, 90 frames", "width": 768, "height": 768, "num_frames": 90},
138
- {"label": "720x720, 64 frames", "width": 768, "height": 768, "num_frames": 64},
139
- {"label": "720x720, 100 frames", "width": 768, "height": 768, "num_frames": 100},
140
- {"label": "768x512, 97 frames", "width": 768, "height": 512, "num_frames": 97},
141
- {"label": "512x512, 160 frames", "width": 512, "height": 512, "num_frames": 160},
142
- {"label": "512x512, 200 frames", "width": 512, "height": 512, "num_frames": 200},
143
- ]
144
-
145
- def preset_changed(preset):
146
- if preset != "Custom":
147
- selected = next(item for item in preset_options if item["label"] == preset)
148
- return (
149
- selected["height"],
150
- selected["width"],
151
- selected["num_frames"],
152
- gr.update(visible=False),
153
- gr.update(visible=False),
154
- gr.update(visible=False),
155
- )
156
- else:
157
- return (
158
- None,
159
- None,
160
- None,
161
- gr.update(visible=True),
162
- gr.update(visible=True),
163
- gr.update(visible=True),
164
- )
165
-
166
- # Load models
167
- vae = load_vae(vae_dir)
168
- unet = load_unet(unet_dir)
169
- scheduler = load_scheduler(scheduler_dir)
170
- patchifier = SymmetricPatchifier(patch_size=1)
171
- text_encoder = T5EncoderModel.from_pretrained("PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="text_encoder").to(torch.device("cuda:0"))
172
- tokenizer = T5Tokenizer.from_pretrained("PixArt-alpha/PixArt-XL-2-1024-MS", subfolder="tokenizer")
173
-
174
- pipeline_video = XoraVideoPipeline(
175
- transformer=unet,
176
- patchifier=patchifier,
177
- text_encoder=text_encoder,
178
- tokenizer=tokenizer,
179
- scheduler=scheduler,
180
- vae=vae,
181
- ).to(torch.device("cuda:0"))
182
-
183
- def enhance_prompt_if_enabled(prompt, enhance_toggle):
184
- if not enhance_toggle:
185
- print("Enhance toggle is off, Prompt: ", prompt)
186
- return prompt
187
-
188
- messages = [
189
- {"role": "system", "content": system_prompt_t2v},
190
- {"role": "user", "content": prompt},
191
- ]
192
-
193
- try:
194
- response = client.chat.completions.create(
195
- model="gpt-4-mini",
196
- messages=messages,
197
- max_tokens=200,
198
- )
199
- print("Enhanced Prompt: ", response.choices[0].message.content.strip())
200
- return response.choices[0].message.content.strip()
201
- except Exception as e:
202
- print(f"Error: {e}")
203
- return prompt
204
-
205
- @spaces.GPU(duration=90)
206
- def generate_video_from_text_90(
207
- prompt="",
208
- enhance_prompt_toggle=False,
209
- negative_prompt="",
210
- frame_rate=25,
211
- seed=random.randint(0, MAX_SEED),
212
- num_inference_steps=30,
213
- guidance_scale=3.2,
214
- height=768,
215
- width=768,
216
- num_frames=60,
217
- progress=gr.Progress(),
218
- ):
219
- # 프롬프트 전처리 (한글 -> 영어)
220
- prompt = process_prompt(prompt)
221
- negative_prompt = process_prompt(negative_prompt)
222
-
223
- if len(prompt.strip()) < 50:
224
- raise gr.Error(
225
- "Prompt must be at least 50 characters long. Please provide more details for the best results.",
226
- duration=5,
227
- )
228
-
229
- prompt = enhance_prompt_if_enabled(prompt, enhance_prompt_toggle)
230
-
231
- sample = {
232
- "prompt": prompt,
233
- "prompt_attention_mask": None,
234
- "negative_prompt": negative_prompt,
235
- "negative_prompt_attention_mask": None,
236
- "media_items": None,
237
- }
238
-
239
- generator = torch.Generator(device="cuda").manual_seed(seed)
240
-
241
- def gradio_progress_callback(self, step, timestep, kwargs):
242
- progress((step + 1) / num_inference_steps)
243
-
244
- try:
245
- with torch.no_grad():
246
- images = pipeline_video(
247
- num_inference_steps=num_inference_steps,
248
- num_images_per_prompt=1,
249
- guidance_scale=guidance_scale,
250
- generator=generator,
251
- output_type="pt",
252
- height=height,
253
- width=width,
254
- num_frames=num_frames,
255
- frame_rate=frame_rate,
256
- **sample,
257
- is_video=True,
258
- vae_per_channel_normalize=True,
259
- conditioning_method=ConditioningMethod.UNCONDITIONAL,
260
- mixed_precision=True,
261
- callback_on_step_end=gradio_progress_callback,
262
- ).images
263
- except Exception as e:
264
- raise gr.Error(
265
- f"An error occurred while generating the video. Please try again. Error: {e}",
266
- duration=5,
267
- )
268
- finally:
269
- torch.cuda.empty_cache()
270
- gc.collect()
271
-
272
- output_path = tempfile.mktemp(suffix=".mp4")
273
- video_np = images.squeeze(0).permute(1, 2, 3, 0).cpu().float().numpy()
274
- video_np = (video_np * 255).astype(np.uint8)
275
- height, width = video_np.shape[1:3]
276
- out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (width, height))
277
- for frame in video_np[..., ::-1]:
278
- out.write(frame)
279
- out.release()
280
- del images
281
- del video_np
282
- torch.cuda.empty_cache()
283
- return output_path
284
-
285
- def create_advanced_options():
286
- with gr.Accordion("Step 4: Advanced Options (Optional)", open=False):
287
- seed = gr.Slider(label="4.1 Seed", minimum=0, maximum=1000000, step=1, value=646373)
288
- inference_steps = gr.Slider(label="4.2 Inference Steps", minimum=5, maximum=150, step=5, value=40)
289
- guidance_scale = gr.Slider(label="4.3 Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=4.2)
290
-
291
- height_slider = gr.Slider(
292
- label="4.4 Height",
293
- minimum=256,
294
- maximum=1024,
295
- step=64,
296
- value=768,
297
- visible=False,
298
- )
299
- width_slider = gr.Slider(
300
- label="4.5 Width",
301
- minimum=256,
302
- maximum=1024,
303
- step=64,
304
- value=768,
305
- visible=False,
306
- )
307
- num_frames_slider = gr.Slider(
308
- label="4.5 Number of Frames",
309
- minimum=1,
310
- maximum=500,
311
- step=1,
312
- value=60,
313
- visible=False,
314
- )
315
-
316
- return [
317
- seed,
318
- inference_steps,
319
- guidance_scale,
320
- height_slider,
321
- width_slider,
322
- num_frames_slider,
323
- ]
324
-
325
- ###############################################
326
- # 여기서부터 두 번째 코드 통합 적용
327
- ###############################################
328
-
329
- import argparse
330
- import time
331
- from os import path
332
- import shutil
333
- from safetensors.torch import load_file
334
- from diffusers import FluxPipeline
335
- from diffusers.pipelines.stable_diffusion import safety_checker
336
- import replicate
337
- import logging
338
- import requests
339
- from pathlib import Path
340
- import sys
341
- import io
342
-
343
- # 로깅 설정
344
- logging.basicConfig(level=logging.INFO)
345
- logger = logging.getLogger(__name__)
346
-
347
- # Setup and initialization code
348
- cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
349
- PERSISTENT_DIR = os.environ.get("PERSISTENT_DIR", ".")
350
- gallery_path = path.join(PERSISTENT_DIR, "gallery")
351
- video_gallery_path = path.join(PERSISTENT_DIR, "video_gallery")
352
-
353
- # API 설정
354
- CATBOX_USER_HASH = "e7a96fc68dd4c7d2954040cd5"
355
- REPLICATE_API_TOKEN = os.getenv("API_KEY")
356
-
357
- # 환경 변수 설정
358
- os.environ["TRANSFORMERS_CACHE"] = cache_path
359
- os.environ["HF_HUB_CACHE"] = cache_path
360
- os.environ["HF_HOME"] = cache_path
361
-
362
- # CUDA 설정
363
- torch.backends.cuda.matmul.allow_tf32 = True
364
-
365
- # 번역기 초기화 (이미 위에서 translator 선언됨, 중복 선언)
366
- translator2 = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en") # 두 번째 코드에서도 선언. 누락없이 출력하기 위해 추가.
367
-
368
- # 디렉토리 생성
369
- for dir_path in [gallery_path, video_gallery_path]:
370
- if not path.exists(dir_path):
371
- os.makedirs(dir_path, exist_ok=True)
372
-
373
- def check_api_key():
374
- """API 키 확인 및 설정"""
375
- if not REPLICATE_API_TOKEN:
376
- logger.error("Replicate API key not found")
377
- return False
378
- os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
379
- logger.info("Replicate API token set successfully")
380
- return True
381
-
382
- def translate_if_korean(text):
383
- """한글이 포함된 경우 영어로 번역"""
384
- if any(ord(char) >= 0xAC00 and ord(char) <= 0xD7A3 for char in text):
385
- translation = translator2(text)[0]['translation_text']
386
- return translation
387
- return text
388
-
389
- def filter_prompt(prompt):
390
- inappropriate_keywords = [
391
- "nude", "naked", "nsfw", "porn", "sex", "explicit", "adult", "xxx",
392
- "erotic", "sensual", "seductive", "provocative", "intimate",
393
- "violence", "gore", "blood", "death", "kill", "murder", "torture",
394
- "drug", "suicide", "abuse", "hate", "discrimination"
395
- ]
396
-
397
- prompt_lower = prompt.lower()
398
- for keyword in inappropriate_keywords:
399
- if keyword in prompt_lower:
400
- return False, "부적절한 내용이 포함된 프롬프트입니다."
401
- return True, prompt
402
-
403
- def process_prompt_for_sd(prompt):
404
- """프롬프트 전처리 (번역 및 필터링)"""
405
- translated_prompt = translate_if_korean(prompt)
406
- is_safe, filtered_prompt = filter_prompt(translated_prompt)
407
- return is_safe, filtered_prompt
408
-
409
- class timer:
410
- def __init__(self, method_name="timed process"):
411
- self.method = method_name
412
- def __enter__(self):
413
- self.start = time.time()
414
- print(f"{self.method} starts")
415
- def __exit__(self, exc_type, exc_val, exc_tb):
416
- end = time.time()
417
- print(f"{self.method} took {str(round(end - self.start, 2))}s")
418
-
419
- # Model initialization
420
- if not path.exists(cache_path):
421
- os.makedirs(cache_path, exist_ok=True)
422
-
423
- pipe_sd = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16)
424
- pipe_sd.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"))
425
- pipe_sd.fuse_lora(lora_scale=0.125)
426
- pipe_sd.to(device="cuda", dtype=torch.bfloat16)
427
- pipe_sd.safety_checker = safety_checker.StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker")
428
-
429
- def upload_to_catbox(image_path):
430
- """catbox.moe API를 사용하여 이미지 업로드"""
431
- try:
432
- logger.info(f"Preparing to upload image: {image_path}")
433
- url = "https://catbox.moe/user/api.php"
434
-
435
- file_extension = Path(image_path).suffix.lower()
436
- if file_extension not in ['.jpg', '.jpeg', '.png', '.gif']:
437
- logger.error(f"Unsupported file type: {file_extension}")
438
- return None
439
-
440
- files = {
441
- 'fileToUpload': (
442
- os.path.basename(image_path),
443
- open(image_path, 'rb'),
444
- 'image/jpeg' if file_extension in ['.jpg', '.jpeg'] else 'image/png'
445
- )
446
- }
447
-
448
- data = {
449
- 'reqtype': 'fileupload',
450
- 'userhash': CATBOX_USER_HASH
451
- }
452
-
453
- response = requests.post(url, files=files, data=data)
454
-
455
- if response.status_code == 200 and response.text.startswith('http'):
456
- image_url = response.text
457
- logger.info(f"Image uploaded successfully: {image_url}")
458
- return image_url
459
- else:
460
- raise Exception(f"Upload failed: {response.text}")
461
-
462
- except Exception as e:
463
- logger.error(f"Image upload error: {str(e)}")
464
- return None
465
-
466
- def add_watermark(video_path):
467
- """OpenCV를 사용하여 비디오에 워터마크 추가"""
468
- try:
469
- cap = cv2.VideoCapture(video_path)
470
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
471
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
472
- fps = int(cap.get(cv2.CAP_PROP_FPS))
473
-
474
- text = "GiniGEN.AI"
475
- font = cv2.FONT_HERSHEY_SIMPLEX
476
- font_scale = height * 0.05 / 30
477
- thickness = 2
478
- color = (255, 255, 255)
479
-
480
- (text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, thickness)
481
- margin = int(height * 0.02)
482
- x_pos = width - text_width - margin
483
- y_pos = height - margin
484
-
485
- output_path = "watermarked_output.mp4"
486
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
487
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
488
-
489
- while cap.isOpened():
490
- ret, frame = cap.read()
491
- if not ret:
492
- break
493
- cv2.putText(frame, text, (x_pos, y_pos), font, font_scale, color, thickness)
494
- out.write(frame)
495
-
496
- cap.release()
497
- out.release()
498
-
499
- return output_path
500
-
501
- except Exception as e:
502
- logger.error(f"Error adding watermark: {str(e)}")
503
- return video_path
504
-
505
- def generate_video(image, prompt):
506
- logger.info("Starting video generation")
507
- try:
508
- if not check_api_key():
509
- return "Replicate API key not properly configured"
510
-
511
- if not image:
512
- logger.error("No image provided")
513
- return "Please upload an image"
514
-
515
- image_url = upload_to_catbox(image)
516
- if not image_url:
517
- return "Failed to upload image"
518
-
519
- input_data = {
520
- "prompt": prompt,
521
- "first_frame_image": image_url
522
- }
523
-
524
- try:
525
- replicate.Client(api_token=REPLICATE_API_TOKEN)
526
- output = replicate.run(
527
- "minimax/video-01-live",
528
- input=input_data
529
- )
530
-
531
- temp_file = "temp_output.mp4"
532
-
533
- if hasattr(output, 'read'):
534
- with open(temp_file, "wb") as file:
535
- file.write(output.read())
536
- elif isinstance(output, str):
537
- response = requests.get(output)
538
- with open(temp_file, "wb") as file:
539
- file.write(response.content)
540
-
541
- final_video = add_watermark(temp_file)
542
- return final_video
543
-
544
- except Exception as api_error:
545
- logger.error(f"API call failed: {str(api_error)}")
546
- return f"API call failed: {str(api_error)}"
547
-
548
- except Exception as e:
549
- logger.error(f"Unexpected error: {str(e)}")
550
- return f"Unexpected error: {str(e)}"
551
-
552
- def save_image(image):
553
- """Save the generated image in PNG format and return the path"""
554
- try:
555
- if not os.path.exists(gallery_path):
556
- os.makedirs(gallery_path, exist_ok=True)
557
-
558
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
559
- random_suffix = os.urandom(4).hex()
560
- filename = f"generated_{timestamp}_{random_suffix}.png"
561
- filepath = os.path.join(gallery_path, filename)
562
-
563
- if not isinstance(image, Image.Image):
564
- image = Image.fromarray(image)
565
-
566
- if image.mode != 'RGB':
567
- image = image.convert('RGB')
568
-
569
- image.save(
570
- filepath,
571
- format='PNG',
572
- optimize=True,
573
- quality=100
574
- )
575
-
576
- logger.info(f"Image saved successfully as PNG: {filepath}")
577
- return filepath
578
- except Exception as e:
579
- logger.error(f"Error in save_image: {str(e)}")
580
- return None
581
-
582
- def load_gallery():
583
- """Load all images from the gallery directory"""
584
- try:
585
- os.makedirs(gallery_path, exist_ok=True)
586
-
587
- image_files = []
588
- for f in os.listdir(gallery_path):
589
- if f.lower().endswith(('.png', '.jpg', '.jpeg')):
590
- full_path = os.path.join(gallery_path, f)
591
- image_files.append((full_path, os.path.getmtime(full_path)))
592
-
593
- image_files.sort(key=lambda x: x[1], reverse=True)
594
- return [f[0] for f in image_files]
595
- except Exception as e:
596
- print(f"Error loading gallery: {str(e)}")
597
- return []
598
-
599
- # CSS 스타일 정의
600
- css = """
601
- [이전의 CSS 코드를 그대로 유지]
602
- """
603
-
604
- def get_random_seed():
605
- return torch.randint(0, 1000000, (1,)).item()
606
-
607
- ###############################################
608
- # 여기서부터 Gradio UI 통합
609
- ###############################################
610
-
611
- with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
612
- gr.HTML('<div class="title">AI Image & Video Generator</div>')
613
-
614
- with gr.Tabs():
615
- with gr.Tab("Image Generation"):
616
- with gr.Row():
617
- with gr.Column(scale=3):
618
- img_prompt = gr.Textbox(
619
- label="Image Description",
620
- placeholder="이미지 설명을 입력하세요... (한글 입력 가능)",
621
- lines=3
622
- )
623
-
624
- with gr.Accordion("Advanced Settings", open=False):
625
- with gr.Row():
626
- height = gr.Slider(
627
- label="Height",
628
- minimum=256,
629
- maximum=1152,
630
- step=64,
631
- value=1024
632
- )
633
- width = gr.Slider(
634
- label="Width",
635
- minimum=256,
636
- maximum=1152,
637
- step=64,
638
- value=1024
639
- )
640
-
641
- with gr.Row():
642
- steps = gr.Slider(
643
- label="Inference Steps",
644
- minimum=6,
645
- maximum=25,
646
- step=1,
647
- value=8
648
- )
649
- scales = gr.Slider(
650
- label="Guidance Scale",
651
- minimum=0.0,
652
- maximum=5.0,
653
- step=0.1,
654
- value=3.5
655
- )
656
-
657
- seed = gr.Number(
658
- label="Seed",
659
- value=get_random_seed(),
660
- precision=0
661
- )
662
-
663
- randomize_seed = gr.Button("🎲 Randomize Seed", elem_classes=["generate-btn"])
664
-
665
- generate_btn = gr.Button(
666
- "✨ Generate Image",
667
- elem_classes=["generate-btn"]
668
- )
669
-
670
- with gr.Column(scale=4):
671
- img_output = gr.Image(
672
- label="Generated Image",
673
- type="pil",
674
- format="png"
675
- )
676
- img_gallery = gr.Gallery(
677
- label="Image Gallery",
678
- show_label=True,
679
- elem_id="gallery",
680
- columns=[4],
681
- rows=[2],
682
- height="auto",
683
- object_fit="cover"
684
- )
685
- img_gallery.value = load_gallery()
686
-
687
- with gr.Tab("Video Generation"):
688
- with gr.Row():
689
- with gr.Column(scale=3):
690
- video_prompt = gr.Textbox(
691
- label="Video Description",
692
- placeholder="비디오 설명을 입력하세요... (한글 입력 가능)",
693
- lines=3
694
- )
695
- upload_image = gr.Image(
696
- type="filepath",
697
- label="Upload First Frame Image"
698
- )
699
- video_generate_btn = gr.Button(
700
- "🎬 Generate Video",
701
- elem_classes=["generate-btn"]
702
- )
703
-
704
- with gr.Column(scale=4):
705
- video_output = gr.Video(label="Generated Video")
706
- video_gallery = gr.Gallery(
707
- label="Video Gallery",
708
- show_label=True,
709
- columns=[4],
710
- rows=[2],
711
- height="auto",
712
- object_fit="cover"
713
- )
714
-
715
- # 이하 첫 번째 코드의 txt2vid 관련 UI를 통합
716
- # 첫 번째 코드의 txt2vid UI를 추가 탭으로 통합
717
- with gr.Tab("Text-to-Video Generation"):
718
- with gr.Column():
719
- txt2vid_prompt = gr.Textbox(
720
- label="Step 1: Enter Your Prompt (한글 또는 영어)",
721
- placeholder="생성하고 싶은 비디오를 설명하세요 (최소 50자)...",
722
- value="긴 갈색 머리와 밝은 피부를 가진 여성이 긴 금발 머리를 가진 다른 여성을 향해 미소 짓습니다. 갈색 머리 여성은 검은 재킷을 입고 있으며 오른쪽 뺨에 작고 거의 눈에 띄지 않는 점이 있습니다. 카메라 앵글은 갈색 머리 여성의 얼굴에 초점을 맞춘 클로즈업입니다. 조명은 따뜻하고 자연스러우며, 아마도 지는 해에서 나오는 것 같아 장면에 부드러운 빛을 비춥니다.",
723
- lines=5,
724
- )
725
-
726
- txt2vid_enhance_toggle = Toggle(
727
- label="Enhance Prompt",
728
- value=False,
729
- interactive=True,
730
- )
731
-
732
- txt2vid_negative_prompt = gr.Textbox(
733
- label="Step 2: Enter Negative Prompt",
734
- placeholder="비디오에서 원하지 않는 요소를 설명하세요...",
735
- value="low quality, worst quality, deformed, distorted, damaged, motion blur, motion artifacts, fused fingers, incorrect anatomy, strange hands, ugly",
736
- lines=2,
737
- )
738
-
739
- txt2vid_preset = gr.Dropdown(
740
- choices=[p["label"] for p in preset_options],
741
- value="512x512, 160 frames",
742
- label="Step 3.1: Choose Resolution Preset",
743
- )
744
-
745
- txt2vid_frame_rate = gr.Slider(
746
- label="Step 3.2: Frame Rate",
747
- minimum=6,
748
- maximum=60,
749
- step=1,
750
- value=20,
751
- )
752
-
753
- txt2vid_advanced = create_advanced_options()
754
- txt2vid_generate = gr.Button(
755
- "Step 5: Generate Video",
756
- variant="primary",
757
- size="lg",
758
- )
759
-
760
- txt2vid_output = gr.Video(label="Generated Output")
761
-
762
- txt2vid_preset.change(
763
- fn=preset_changed,
764
- inputs=[txt2vid_preset],
765
- outputs=txt2vid_advanced[3:],
766
- )
767
-
768
- txt2vid_generate.click(
769
- fn=generate_video_from_text_90,
770
- inputs=[
771
- txt2vid_prompt,
772
- txt2vid_enhance_toggle,
773
- txt2vid_negative_prompt,
774
- txt2vid_frame_rate,
775
- *txt2vid_advanced,
776
- ],
777
- outputs=txt2vid_output,
778
- concurrency_limit=1,
779
- concurrency_id="generate_video",
780
- queue=True,
781
- )
782
-
783
- @spaces.GPU
784
- def process_and_save_image(height, width, steps, scales, prompt, seed):
785
- is_safe, translated_prompt = process_prompt_for_sd(prompt)
786
- if not is_safe:
787
- gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
788
- return None, load_gallery()
789
-
790
- with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
791
- try:
792
- generated_image = pipe_sd(
793
- prompt=[translated_prompt],
794
- generator=torch.Generator().manual_seed(int(seed)),
795
- num_inference_steps=int(steps),
796
- guidance_scale=float(scales),
797
- height=int(height),
798
- width=int(width),
799
- max_sequence_length=256
800
- ).images[0]
801
-
802
- if not isinstance(generated_image, Image.Image):
803
- generated_image = Image.fromarray(generated_image)
804
-
805
- if generated_image.mode != 'RGB':
806
- generated_image = generated_image.convert('RGB')
807
-
808
- img_byte_arr = io.BytesIO()
809
- generated_image.save(img_byte_arr, format='PNG')
810
- img_byte_arr = img_byte_arr.getvalue()
811
-
812
- saved_path = save_image(generated_image)
813
- if saved_path is None:
814
- logger.warning("Failed to save generated image")
815
- return None, load_gallery()
816
-
817
- return Image.open(io.BytesIO(img_byte_arr)), load_gallery()
818
- except Exception as e:
819
- logger.error(f"Error in image generation: {str(e)}")
820
- return None, load_gallery()
821
-
822
-
823
- def process_and_generate_video(image, prompt):
824
- is_safe, translated_prompt = process_prompt_for_sd(prompt)
825
- if not is_safe:
826
- gr.Warning("부적절한 내용이 포함된 프롬프트입니다.")
827
- return None
828
- return generate_video(image, translated_prompt)
829
-
830
- def update_seed():
831
- return get_random_seed()
832
-
833
- generate_btn.click(
834
- process_and_save_image,
835
- inputs=[height, width, steps, scales, img_prompt, seed],
836
- outputs=[img_output, img_gallery]
837
- )
838
-
839
- video_generate_btn.click(
840
- process_and_generate_video,
841
- inputs=[upload_image, video_prompt],
842
- outputs=video_output
843
- )
844
-
845
- randomize_seed.click(
846
- update_seed,
847
- outputs=[seed]
848
- )
849
-
850
- generate_btn.click(
851
- update_seed,
852
- outputs=[seed]
853
- )
854
-
855
- if __name__ == "__main__":
856
- demo.queue(max_size=64, default_concurrency_limit=1, api_open=False).launch(share=True, show_api=False, allowed_paths=[PERSISTENT_DIR])