Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,8 @@ import requests
|
|
3 |
import os
|
4 |
import json
|
5 |
import time
|
6 |
-
|
|
|
7 |
|
8 |
# --- 1. CONFIGURATION & CONSTANTS ---
|
9 |
# Securely load API key from Hugging Face Space secrets.
|
@@ -92,18 +93,18 @@ def call_gpt4o_oneapi(transcript_content, prompt_template):
|
|
92 |
except json.JSONDecodeError:
|
93 |
return f"Error: Failed to decode JSON from API response.\nResponse Body: {response.text}"
|
94 |
|
95 |
-
# --- 3. CORE ORCHESTRATOR FUNCTION
|
96 |
|
97 |
def generate_viral_clip(video_file, srt_file, analysis_mode, progress=gr.Progress()):
|
98 |
-
# The `video_file` and `srt_file` inputs are now file PATHS (strings), not objects.
|
99 |
if not video_file or not srt_file:
|
100 |
return "Error: Please upload both a video file and an SRT file.", None
|
101 |
if not ONE_API_KEY or not ONE_API_URL:
|
102 |
return "Error: API keys for OneAPI are not configured correctly in the Space secrets.", None
|
103 |
|
|
|
|
|
104 |
try:
|
105 |
progress(0.1, desc="Reading SRT file...")
|
106 |
-
# FIX: Use `srt_file` directly, as it's now a path string.
|
107 |
with open(srt_file, 'r', encoding='utf-8') as f:
|
108 |
transcript_content = f.read()
|
109 |
|
@@ -145,20 +146,32 @@ def generate_viral_clip(video_file, srt_file, analysis_mode, progress=gr.Progres
|
|
145 |
|
146 |
progress(0.8, desc="Clipping video...")
|
147 |
output_filename = "viral_clip.mp4"
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
156 |
|
157 |
progress(1.0, desc="Done!")
|
158 |
return summary, output_filename
|
159 |
|
160 |
except Exception as e:
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
# --- 4. GRADIO UI DEFINITION ---
|
164 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -192,4 +205,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
192 |
|
193 |
if __name__ == "__main__":
|
194 |
demo.launch(debug=True)
|
195 |
-
|
|
|
3 |
import os
|
4 |
import json
|
5 |
import time
|
6 |
+
# CORRECT, HIGH-LEVEL IMPORT for MoviePy. This brings in all editing functions.
|
7 |
+
from moviepy.editor import VideoFileClip
|
8 |
|
9 |
# --- 1. CONFIGURATION & CONSTANTS ---
|
10 |
# Securely load API key from Hugging Face Space secrets.
|
|
|
93 |
except json.JSONDecodeError:
|
94 |
return f"Error: Failed to decode JSON from API response.\nResponse Body: {response.text}"
|
95 |
|
96 |
+
# --- 3. CORE ORCHESTRATOR FUNCTION ---
|
97 |
|
98 |
def generate_viral_clip(video_file, srt_file, analysis_mode, progress=gr.Progress()):
|
|
|
99 |
if not video_file or not srt_file:
|
100 |
return "Error: Please upload both a video file and an SRT file.", None
|
101 |
if not ONE_API_KEY or not ONE_API_URL:
|
102 |
return "Error: API keys for OneAPI are not configured correctly in the Space secrets.", None
|
103 |
|
104 |
+
video = None
|
105 |
+
new_clip = None
|
106 |
try:
|
107 |
progress(0.1, desc="Reading SRT file...")
|
|
|
108 |
with open(srt_file, 'r', encoding='utf-8') as f:
|
109 |
transcript_content = f.read()
|
110 |
|
|
|
146 |
|
147 |
progress(0.8, desc="Clipping video...")
|
148 |
output_filename = "viral_clip.mp4"
|
149 |
+
|
150 |
+
# FIX: Load video using the correct high-level object
|
151 |
+
video = VideoFileClip(video_file)
|
152 |
+
|
153 |
+
if end_time > video.duration:
|
154 |
+
end_time = video.duration
|
155 |
+
summary += f"\n\n⚠️ Warning: End time was beyond video duration, adjusted to {end_time:.2f}s."
|
156 |
+
|
157 |
+
# Now .subclip() will exist because we imported from moviepy.editor
|
158 |
+
new_clip = video.subclip(start_time, end_time)
|
159 |
+
new_clip.write_videofile(output_filename, codec="libx264", audio_codec="aac")
|
160 |
|
161 |
progress(1.0, desc="Done!")
|
162 |
return summary, output_filename
|
163 |
|
164 |
except Exception as e:
|
165 |
+
import traceback
|
166 |
+
tb_str = traceback.format_exc()
|
167 |
+
return f"An unexpected error occurred in the main process: {str(e)}\n\nTraceback:\n{tb_str}", None
|
168 |
+
|
169 |
+
finally:
|
170 |
+
# Manually close the clips to release file resources, preventing locks.
|
171 |
+
if new_clip:
|
172 |
+
new_clip.close()
|
173 |
+
if video:
|
174 |
+
video.close()
|
175 |
|
176 |
# --- 4. GRADIO UI DEFINITION ---
|
177 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
205 |
|
206 |
if __name__ == "__main__":
|
207 |
demo.launch(debug=True)
|
|