rahul7star commited on
Commit
22a57d0
·
verified ·
1 Parent(s): 64fffe5

Update wan2_fast.py

Browse files
Files changed (1) hide show
  1. wan2_fast.py +84 -1
wan2_fast.py CHANGED
@@ -47,6 +47,89 @@ import uuid
47
  import logging
48
  from datetime import datetime
49
  from huggingface_hub import HfApi, upload_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  def upload_to_hf(video_path, summary_text):
52
  api = HfApi()
@@ -332,7 +415,7 @@ def generate_video(prompt, height, width,
332
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
333
  video_path = tmpfile.name
334
  export_to_video(output_frames_list, video_path, fps=FIXED_OUTPUT_FPS)
335
- hf_folder = upload_to_hf(video_path, prompt)
336
  return video_path, current_seed
337
 
338
 
 
47
  import logging
48
  from datetime import datetime
49
  from huggingface_hub import HfApi, upload_file
50
+ import subprocess
51
+ import tempfile
52
+ import logging
53
+ import shutil
54
+ import os
55
+ from huggingface_hub import HfApi, upload_file
56
+ from datetime import datetime
57
+ import uuid
58
+
59
+ HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/VideoExplain")
60
+
61
+ def upscale_and_upload_4k(input_video_path: str, summary_text: str) -> str:
62
+ """
63
+ Upscale a video to 4K and upload it to Hugging Face Hub without replacing the original file.
64
+
65
+ Args:
66
+ input_video_path (str): Path to the original video.
67
+ summary_text (str): Text summary to upload alongside the video.
68
+
69
+ Returns:
70
+ str: Hugging Face folder path where the video and summary were uploaded.
71
+ """
72
+ logging.info(f"Upscaling video to 4K for upload: {input_video_path}")
73
+
74
+ # Create a temporary file for the upscaled video
75
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_upscaled:
76
+ upscaled_path = tmp_upscaled.name
77
+
78
+ # FFmpeg upscale command
79
+ cmd = [
80
+ "ffmpeg",
81
+ "-i", input_video_path,
82
+ "-vf", "scale=3840:2160:flags=lanczos",
83
+ "-c:v", "libx264",
84
+ "-crf", "18",
85
+ "-preset", "slow",
86
+ "-y",
87
+ upscaled_path,
88
+ ]
89
+ try:
90
+ subprocess.run(cmd, check=True, capture_output=True)
91
+ logging.info(f"✅ Upscaled video created at: {upscaled_path}")
92
+ except subprocess.CalledProcessError as e:
93
+ logging.error(f"FFmpeg failed:\n{e.stderr.decode()}")
94
+ raise
95
+
96
+ # Create a date-based folder on HF
97
+ today_str = datetime.now().strftime("%Y-%m-%d")
98
+ unique_subfolder = f"Upload-4K-{uuid.uuid4().hex[:8]}"
99
+ hf_folder = f"{today_str}/{unique_subfolder}"
100
+
101
+ # Upload video
102
+ video_filename = os.path.basename(input_video_path)
103
+ video_hf_path = f"{hf_folder}/{video_filename}"
104
+ upload_file(
105
+ path_or_fileobj=upscaled_path,
106
+ path_in_repo=video_hf_path,
107
+ repo_id=HF_MODEL,
108
+ repo_type="model",
109
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
110
+ )
111
+ logging.info(f"✅ Uploaded 4K video to HF: {video_hf_path}")
112
+
113
+ # Upload summary.txt
114
+ summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
115
+ with open(summary_file, "w", encoding="utf-8") as f:
116
+ f.write(summary_text)
117
+
118
+ summary_hf_path = f"{hf_folder}/summary.txt"
119
+ upload_file(
120
+ path_or_fileobj=summary_file,
121
+ path_in_repo=summary_hf_path,
122
+ repo_id=HF_MODEL,
123
+ repo_type="model",
124
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
125
+ )
126
+ logging.info(f"✅ Uploaded summary to HF: {summary_hf_path}")
127
+
128
+ # Cleanup temporary files
129
+ os.remove(upscaled_path)
130
+ os.remove(summary_file)
131
+
132
+ return hf_folder
133
 
134
  def upload_to_hf(video_path, summary_text):
135
  api = HfApi()
 
415
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
416
  video_path = tmpfile.name
417
  export_to_video(output_frames_list, video_path, fps=FIXED_OUTPUT_FPS)
418
+ upscale_and_upload_4k(video_path, prompt)
419
  return video_path, current_seed
420
 
421