rahul7star commited on
Commit
83cfdfe
·
verified ·
1 Parent(s): fc829ec

Update app_lora.py

Browse files
Files changed (1) hide show
  1. app_lora.py +84 -1
app_lora.py CHANGED
@@ -67,6 +67,89 @@ import uuid
67
  import logging
68
  from datetime import datetime
69
  from huggingface_hub import HfApi, upload_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def upload_to_hf(video_path, summary_text):
72
  api = HfApi()
@@ -187,7 +270,7 @@ def generate_video(input_image, prompt, height, width,
187
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
188
  video_path = tmpfile.name
189
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
190
- hf_folder = upload_to_hf(video_path, prompt)
191
  return video_path, current_seed
192
 
193
  with gr.Blocks() as demo:
 
67
  import logging
68
  from datetime import datetime
69
  from huggingface_hub import HfApi, upload_file
70
+ import subprocess
71
+ import tempfile
72
+ import logging
73
+ import shutil
74
+ import os
75
+ from huggingface_hub import HfApi, upload_file
76
+ from datetime import datetime
77
+ import uuid
78
+
79
+ HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/VideoExplain")
80
+
81
+ def upscale_and_upload_4k(input_video_path: str, summary_text: str) -> str:
82
+ """
83
+ Upscale a video to 4K and upload it to Hugging Face Hub without replacing the original file.
84
+
85
+ Args:
86
+ input_video_path (str): Path to the original video.
87
+ summary_text (str): Text summary to upload alongside the video.
88
+
89
+ Returns:
90
+ str: Hugging Face folder path where the video and summary were uploaded.
91
+ """
92
+ logging.info(f"Upscaling video to 4K for upload: {input_video_path}")
93
+
94
+ # Create a temporary file for the upscaled video
95
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_upscaled:
96
+ upscaled_path = tmp_upscaled.name
97
+
98
+ # FFmpeg upscale command
99
+ cmd = [
100
+ "ffmpeg",
101
+ "-i", input_video_path,
102
+ "-vf", "scale=3840:2160:flags=lanczos",
103
+ "-c:v", "libx264",
104
+ "-crf", "18",
105
+ "-preset", "slow",
106
+ "-y",
107
+ upscaled_path,
108
+ ]
109
+ try:
110
+ subprocess.run(cmd, check=True, capture_output=True)
111
+ logging.info(f"✅ Upscaled video created at: {upscaled_path}")
112
+ except subprocess.CalledProcessError as e:
113
+ logging.error(f"FFmpeg failed:\n{e.stderr.decode()}")
114
+ raise
115
+
116
+ # Create a date-based folder on HF
117
+ today_str = datetime.now().strftime("%Y-%m-%d")
118
+ unique_subfolder = f"Upload-4K-{uuid.uuid4().hex[:8]}"
119
+ hf_folder = f"{today_str}/{unique_subfolder}"
120
+
121
+ # Upload video
122
+ video_filename = os.path.basename(input_video_path)
123
+ video_hf_path = f"{hf_folder}/{video_filename}"
124
+ upload_file(
125
+ path_or_fileobj=upscaled_path,
126
+ path_in_repo=video_hf_path,
127
+ repo_id=HF_MODEL,
128
+ repo_type="model",
129
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
130
+ )
131
+ logging.info(f"✅ Uploaded 4K video to HF: {video_hf_path}")
132
+
133
+ # Upload summary.txt
134
+ summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
135
+ with open(summary_file, "w", encoding="utf-8") as f:
136
+ f.write(summary_text)
137
+
138
+ summary_hf_path = f"{hf_folder}/summary.txt"
139
+ upload_file(
140
+ path_or_fileobj=summary_file,
141
+ path_in_repo=summary_hf_path,
142
+ repo_id=HF_MODEL,
143
+ repo_type="model",
144
+ token=os.environ.get("HUGGINGFACE_HUB_TOKEN"),
145
+ )
146
+ logging.info(f"✅ Uploaded summary to HF: {summary_hf_path}")
147
+
148
+ # Cleanup temporary files
149
+ os.remove(upscaled_path)
150
+ os.remove(summary_file)
151
+
152
+ return hf_folder
153
 
154
  def upload_to_hf(video_path, summary_text):
155
  api = HfApi()
 
270
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
271
  video_path = tmpfile.name
272
  export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
273
+ upscale_and_upload_4k(video_path, prompt)
274
  return video_path, current_seed
275
 
276
  with gr.Blocks() as demo: