rahul7star commited on
Commit
c84409e
·
verified ·
1 Parent(s): 65c41e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -1
app.py CHANGED
@@ -125,6 +125,92 @@ def upload_to_hf(video_path: str, summary_text: str):
125
  return hf_folder
126
 
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  def save_video_ffmpeg(frames: list, video_path: str, fps: int = FIXED_FPS):
129
  h, w, c = frames[0].shape
130
  process = (
@@ -244,7 +330,8 @@ def generate_video(
244
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
245
 
246
 
247
- hf_folder = upload_to_hf(video_path, prompt)
 
248
  logging.info(f"✅ Uploaded folder: {hf_folder}")
249
 
250
  return video_path, current_seed
 
125
  return hf_folder
126
 
127
 
128
+ import subprocess
129
+ import tempfile
130
+ import logging
131
+ import shutil
132
+ import os
133
+ from huggingface_hub import HfApi, upload_file
134
+ from datetime import datetime
135
+ import uuid
136
+
137
+ HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/VideoExplain")
138
+
139
+ def upscale_and_upload_4k(input_video_path: str, summary_text: str) -> str:
140
+ """
141
+ Upscale a video to 4K and upload it to Hugging Face Hub without replacing the original file.
142
+
143
+ Args:
144
+ input_video_path (str): Path to the original video.
145
+ summary_text (str): Text summary to upload alongside the video.
146
+
147
+ Returns:
148
+ str: Hugging Face folder path where the video and summary were uploaded.
149
+ """
150
+ logging.info(f"Upscaling video to 4K for upload: {input_video_path}")
151
+
152
+ # Create a temporary file for the upscaled video
153
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_upscaled:
154
+ upscaled_path = tmp_upscaled.name
155
+
156
+ # FFmpeg upscale command
157
+ cmd = [
158
+ "ffmpeg",
159
+ "-i", input_video_path,
160
+ "-vf", "scale=3840:2160:flags=lanczos",
161
+ "-c:v", "libx264",
162
+ "-crf", "18",
163
+ "-preset", "slow",
164
+ "-y",
165
+ upscaled_path,
166
+ ]
167
+ try:
168
+ subprocess.run(cmd, check=True, capture_output=True)
169
+ logging.info(f"✅ Upscaled video created at: {upscaled_path}")
170
+ except subprocess.CalledProcessError as e:
171
+ logging.error(f"FFmpeg failed:\n{e.stderr.decode()}")
172
+ raise
173
+
174
+ # Create a date-based folder on HF
175
+ today_str = datetime.now().strftime("%Y-%m-%d")
176
+ unique_subfolder = f"Upload-4K-{uuid.uuid4().hex[:8]}"
177
+ hf_folder = f"{today_str}/{unique_subfolder}"
178
+
179
+ # Upload video
180
+ video_filename = os.path.basename(input_video_path)
181
+ video_hf_path = f"{hf_folder}/{video_filename}"
182
+ upload_file(
183
+ path_or_fileobj=upscaled_path,
184
+ path_in_repo=video_hf_path,
185
+ repo_id=HF_MODEL,
186
+ repo_type="model",
187
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
188
+ )
189
+ logging.info(f"✅ Uploaded 4K video to HF: {video_hf_path}")
190
+
191
+ # Upload summary.txt
192
+ summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
193
+ with open(summary_file, "w", encoding="utf-8") as f:
194
+ f.write(summary_text)
195
+
196
+ summary_hf_path = f"{hf_folder}/summary.txt"
197
+ upload_file(
198
+ path_or_fileobj=summary_file,
199
+ path_in_repo=summary_hf_path,
200
+ repo_id=HF_MODEL,
201
+ repo_type="model",
202
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
203
+ )
204
+ logging.info(f"✅ Uploaded summary to HF: {summary_hf_path}")
205
+
206
+ # Cleanup temporary files
207
+ os.remove(upscaled_path)
208
+ os.remove(summary_file)
209
+
210
+ return hf_folder
211
+
212
+
213
+
214
  def save_video_ffmpeg(frames: list, video_path: str, fps: int = FIXED_FPS):
215
  h, w, c = frames[0].shape
216
  process = (
 
330
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
331
 
332
 
333
+ #hf_folder = upload_to_hf(video_path, prompt)
334
+ upscale_and_upload_4k(video_path,prompt)
335
  logging.info(f"✅ Uploaded folder: {hf_folder}")
336
 
337
  return video_path, current_seed