Update app_enhanced.py
Browse files- app_enhanced.py +30 -6
app_enhanced.py
CHANGED
|
@@ -81,9 +81,18 @@ def generate_save_code(length=8):
|
|
| 81 |
# ======================================================
|
| 82 |
# 🧠 GLOBAL GPU FUNCTIONS
|
| 83 |
# ======================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
@spaces.GPU(duration=300)
|
| 85 |
-
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_pages):
|
| 86 |
print(f"🚀 GPU Task Started: {video_path} | Pages: {target_pages}")
|
|
|
|
| 87 |
|
| 88 |
import cv2
|
| 89 |
import srt
|
|
@@ -103,6 +112,8 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 103 |
cap.release()
|
| 104 |
|
| 105 |
user_srt = os.path.join(user_dir, 'subs.srt')
|
|
|
|
|
|
|
| 106 |
try:
|
| 107 |
get_real_subtitles(video_path)
|
| 108 |
if os.path.exists('test1.srt'):
|
|
@@ -119,7 +130,7 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 119 |
|
| 120 |
if target_pages <= 0: target_pages = 1
|
| 121 |
|
| 122 |
-
# ===
|
| 123 |
panels_per_page = 5
|
| 124 |
total_panels_needed = target_pages * panels_per_page
|
| 125 |
|
|
@@ -138,6 +149,8 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 138 |
count = 0
|
| 139 |
frame_files_ordered = []
|
| 140 |
|
|
|
|
|
|
|
| 141 |
for i, moment in enumerate(selected_moments):
|
| 142 |
mid = (moment['start'] + moment['end']) / 2
|
| 143 |
if mid > duration: mid = duration - 1
|
|
@@ -151,17 +164,24 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 151 |
frame_metadata[fname] = {'dialogue': moment['text'], 'time': mid}
|
| 152 |
frame_files_ordered.append(fname)
|
| 153 |
count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
cap.release()
|
| 155 |
|
| 156 |
with open(metadata_path, 'w') as f: json.dump(frame_metadata, f, indent=2)
|
| 157 |
-
|
|
|
|
| 158 |
try: black_bar_crop()
|
| 159 |
except: pass
|
| 160 |
|
| 161 |
se = SimpleColorEnhancer()
|
| 162 |
qe = QualityColorEnhancer()
|
| 163 |
|
| 164 |
-
for f in frame_files_ordered:
|
|
|
|
| 165 |
p = os.path.join(frames_dir, f)
|
| 166 |
try: se.enhance_single(p, p)
|
| 167 |
except: pass
|
|
@@ -169,6 +189,8 @@ def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, target_p
|
|
| 169 |
except: pass
|
| 170 |
|
| 171 |
bubbles_list = []
|
|
|
|
|
|
|
| 172 |
for f in frame_files_ordered:
|
| 173 |
p = os.path.join(frames_dir, f)
|
| 174 |
dialogue = frame_metadata.get(f, {}).get('dialogue', '')
|
|
@@ -287,8 +309,10 @@ class EnhancedComicGenerator:
|
|
| 287 |
|
| 288 |
def run(self, target_pages):
|
| 289 |
try:
|
| 290 |
-
self.write_status("
|
| 291 |
-
|
|
|
|
|
|
|
| 292 |
with open(os.path.join(self.output_dir, 'pages.json'), 'w') as f:
|
| 293 |
json.dump(data, f, indent=2)
|
| 294 |
self.write_status("Complete!", 100)
|
|
|
|
| 81 |
# ======================================================
|
| 82 |
# 🧠 GLOBAL GPU FUNCTIONS
|
| 83 |
# ======================================================
|
| 84 |
+
def update_status_file(path, msg, progress):
|
| 85 |
+
"""Helper to write status from within GPU function"""
|
| 86 |
+
try:
|
| 87 |
+
with open(path, 'w') as f:
|
| 88 |
+
json.dump({'message': msg, 'progress': progress}, f)
|
| 89 |
+
except:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
@spaces.GPU(duration=300)
|
| 93 |
+
def generate_comic_gpu(video_path, user_dir, frames_dir, metadata_path, status_path, target_pages):
|
| 94 |
print(f"🚀 GPU Task Started: {video_path} | Pages: {target_pages}")
|
| 95 |
+
update_status_file(status_path, "Initializing GPU Models...", 10)
|
| 96 |
|
| 97 |
import cv2
|
| 98 |
import srt
|
|
|
|
| 112 |
cap.release()
|
| 113 |
|
| 114 |
user_srt = os.path.join(user_dir, 'subs.srt')
|
| 115 |
+
update_status_file(status_path, "Extracting Audio & Subtitles...", 20)
|
| 116 |
+
|
| 117 |
try:
|
| 118 |
get_real_subtitles(video_path)
|
| 119 |
if os.path.exists('test1.srt'):
|
|
|
|
| 130 |
|
| 131 |
if target_pages <= 0: target_pages = 1
|
| 132 |
|
| 133 |
+
# === 5 PANELS PER PAGE ===
|
| 134 |
panels_per_page = 5
|
| 135 |
total_panels_needed = target_pages * panels_per_page
|
| 136 |
|
|
|
|
| 149 |
count = 0
|
| 150 |
frame_files_ordered = []
|
| 151 |
|
| 152 |
+
update_status_file(status_path, f"Extracting {total_panels_needed} Frames...", 30)
|
| 153 |
+
|
| 154 |
for i, moment in enumerate(selected_moments):
|
| 155 |
mid = (moment['start'] + moment['end']) / 2
|
| 156 |
if mid > duration: mid = duration - 1
|
|
|
|
| 164 |
frame_metadata[fname] = {'dialogue': moment['text'], 'time': mid}
|
| 165 |
frame_files_ordered.append(fname)
|
| 166 |
count += 1
|
| 167 |
+
# Update progress periodically
|
| 168 |
+
if count % 2 == 0:
|
| 169 |
+
prog = 30 + int((count / total_panels_needed) * 20)
|
| 170 |
+
update_status_file(status_path, f"Extracted Frame {count}/{total_panels_needed}", prog)
|
| 171 |
+
|
| 172 |
cap.release()
|
| 173 |
|
| 174 |
with open(metadata_path, 'w') as f: json.dump(frame_metadata, f, indent=2)
|
| 175 |
+
|
| 176 |
+
update_status_file(status_path, "Auto-Cropping...", 55)
|
| 177 |
try: black_bar_crop()
|
| 178 |
except: pass
|
| 179 |
|
| 180 |
se = SimpleColorEnhancer()
|
| 181 |
qe = QualityColorEnhancer()
|
| 182 |
|
| 183 |
+
for idx, f in enumerate(frame_files_ordered):
|
| 184 |
+
update_status_file(status_path, f"Enhancing Image {idx+1}/{len(frame_files_ordered)}", 60 + int((idx / len(frame_files_ordered)) * 20))
|
| 185 |
p = os.path.join(frames_dir, f)
|
| 186 |
try: se.enhance_single(p, p)
|
| 187 |
except: pass
|
|
|
|
| 189 |
except: pass
|
| 190 |
|
| 191 |
bubbles_list = []
|
| 192 |
+
update_status_file(status_path, "Placing Bubbles...", 85)
|
| 193 |
+
|
| 194 |
for f in frame_files_ordered:
|
| 195 |
p = os.path.join(frames_dir, f)
|
| 196 |
dialogue = frame_metadata.get(f, {}).get('dialogue', '')
|
|
|
|
| 309 |
|
| 310 |
def run(self, target_pages):
|
| 311 |
try:
|
| 312 |
+
self.write_status("Queued for GPU...", 5)
|
| 313 |
+
status_file = os.path.join(self.output_dir, 'status.json')
|
| 314 |
+
# Pass status_file path to GPU function for real-time updates
|
| 315 |
+
data = generate_comic_gpu(self.video_path, self.user_dir, self.frames_dir, self.metadata_path, status_file, int(target_pages))
|
| 316 |
with open(os.path.join(self.output_dir, 'pages.json'), 'w') as f:
|
| 317 |
json.dump(data, f, indent=2)
|
| 318 |
self.write_status("Complete!", 100)
|