rahul7star commited on
Commit
574d3fe
·
verified ·
1 Parent(s): b7185b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -2
app.py CHANGED
@@ -72,11 +72,95 @@ optimize_pipeline_(pipe,
72
  )
73
  print("All models loaded and optimized. Gradio app is ready.")
74
 
75
- HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/VideoExplain")
76
  from huggingface_hub import HfApi, upload_file
77
  import os
78
  import uuid
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def upload_to_hf(video_path, summary_text):
81
  api = HfApi()
82
  unique_folder = f"WANI2V-FFLF-Vvideo_{uuid.uuid4().hex[:8]}"
@@ -228,7 +312,7 @@ def generate_video(
228
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
229
 
230
  progress(1.0, desc="Done!")
231
- hf_folder = upload_to_hf(video_path, prompt)
232
  return video_path, current_seed
233
 
234
 
 
72
  )
73
  print("All models loaded and optimized. Gradio app is ready.")
74
 
75
+
76
  from huggingface_hub import HfApi, upload_file
77
  import os
78
  import uuid
79
 
80
+ import subprocess
81
+ import tempfile
82
+ import logging
83
+ import shutil
84
+ import os
85
+ from huggingface_hub import HfApi, upload_file
86
+ from datetime import datetime
87
+ import uuid
88
+
89
+ HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/VideoExplain")
90
+
91
+ def upscale_and_upload_4k(input_video_path: str, summary_text: str) -> str:
92
+ """
93
+ Upscale a video to 4K and upload it to Hugging Face Hub without replacing the original file.
94
+
95
+ Args:
96
+ input_video_path (str): Path to the original video.
97
+ summary_text (str): Text summary to upload alongside the video.
98
+
99
+ Returns:
100
+ str: Hugging Face folder path where the video and summary were uploaded.
101
+ """
102
+ logging.info(f"Upscaling video to 4K for upload: {input_video_path}")
103
+
104
+ # Create a temporary file for the upscaled video
105
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_upscaled:
106
+ upscaled_path = tmp_upscaled.name
107
+
108
+ # FFmpeg upscale command
109
+ cmd = [
110
+ "ffmpeg",
111
+ "-i", input_video_path,
112
+ "-vf", "scale=3840:2160:flags=lanczos",
113
+ "-c:v", "libx264",
114
+ "-crf", "18",
115
+ "-preset", "slow",
116
+ "-y",
117
+ upscaled_path,
118
+ ]
119
+ try:
120
+ subprocess.run(cmd, check=True, capture_output=True)
121
+ logging.info(f"✅ Upscaled video created at: {upscaled_path}")
122
+ except subprocess.CalledProcessError as e:
123
+ logging.error(f"FFmpeg failed:\n{e.stderr.decode()}")
124
+ raise
125
+
126
+ # Create a date-based folder on HF
127
+ today_str = datetime.now().strftime("%Y-%m-%d")
128
+ unique_subfolder = f"Upload-4K-{uuid.uuid4().hex[:8]}"
129
+ hf_folder = f"{today_str}/{unique_subfolder}"
130
+
131
+ # Upload video
132
+ video_filename = os.path.basename(input_video_path)
133
+ video_hf_path = f"{hf_folder}/{video_filename}"
134
+ upload_file(
135
+ path_or_fileobj=upscaled_path,
136
+ path_in_repo=video_hf_path,
137
+ repo_id=HF_MODEL,
138
+ repo_type="model",
139
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
140
+ )
141
+ logging.info(f"✅ Uploaded 4K video to HF: {video_hf_path}")
142
+
143
+ # Upload summary.txt
144
+ summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
145
+ with open(summary_file, "w", encoding="utf-8") as f:
146
+ f.write(summary_text)
147
+
148
+ summary_hf_path = f"{hf_folder}/summary.txt"
149
+ upload_file(
150
+ path_or_fileobj=summary_file,
151
+ path_in_repo=summary_hf_path,
152
+ repo_id=HF_MODEL,
153
+ repo_type="model",
154
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
155
+ )
156
+ logging.info(f"✅ Uploaded summary to HF: {summary_hf_path}")
157
+
158
+ # Cleanup temporary files
159
+ os.remove(upscaled_path)
160
+ os.remove(summary_file)
161
+
162
+ return hf_folder
163
+
164
  def upload_to_hf(video_path, summary_text):
165
  api = HfApi()
166
  unique_folder = f"WANI2V-FFLF-Vvideo_{uuid.uuid4().hex[:8]}"
 
312
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
313
 
314
  progress(1.0, desc="Done!")
315
+ hf_folder = upscale_and_upload_4k(video_path,prompt)
316
  return video_path, current_seed
317
 
318