Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ import requests
|
|
| 7 |
from datetime import timedelta
|
| 8 |
|
| 9 |
# --- Configuration ---
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
def format_timecode(seconds, fps=25):
|
| 14 |
td = timedelta(seconds=seconds)
|
|
@@ -34,11 +35,11 @@ def generate_cmx_edl(edl_title, segments, source_name, fps=25):
|
|
| 34 |
rec_start += duration
|
| 35 |
return "\n".join(edl_lines)
|
| 36 |
|
| 37 |
-
def call_gemini_for_edl(transcript_data, story_prompt):
|
| 38 |
-
if not
|
| 39 |
-
st.error("Gemini API Key
|
| 40 |
return None
|
| 41 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key={
|
| 42 |
system_prompt = (
|
| 43 |
"You are an expert Documentary Senior Editor. Use the transcript JSON to create a condensed story. "
|
| 44 |
"Output ONLY a valid JSON array of segments with 'src_start', 'src_end', and 'note'. "
|
|
@@ -61,42 +62,62 @@ def call_gemini_for_edl(transcript_data, story_prompt):
|
|
| 61 |
st.set_page_config(page_title="DocAI Editor", layout="wide")
|
| 62 |
st.title("Documentary AI: Senior Editor Pipeline")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
uploaded_file = st.file_uploader("Upload Video/Audio", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
| 65 |
-
fps = st.sidebar.number_input("Timeline FPS", value=25)
|
| 66 |
|
| 67 |
if uploaded_file:
|
| 68 |
if "transcript" not in st.session_state:
|
| 69 |
if st.button("Step 1: Transcribe"):
|
| 70 |
-
with st.spinner("Processing...
|
| 71 |
# Save uploaded file
|
| 72 |
with open("temp_input", "wb") as f:
|
| 73 |
f.write(uploaded_file.getbuffer())
|
| 74 |
|
| 75 |
-
#
|
| 76 |
subprocess.run([
|
| 77 |
-
"ffmpeg", "-i", "temp_input",
|
| 78 |
-
"-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
])
|
| 80 |
|
| 81 |
# Transcribe with Whisper
|
| 82 |
model = whisper.load_model("base")
|
| 83 |
-
result = model.transcribe("audio.
|
| 84 |
st.session_state.transcript = result['segments']
|
| 85 |
st.success("Transcription Complete.")
|
| 86 |
|
| 87 |
if "transcript" in st.session_state:
|
| 88 |
st.divider()
|
| 89 |
-
brief = st.text_area("Creative Brief", placeholder="e.g., Create a 2-minute edit focusing on the 'uncorking the brain' story
|
|
|
|
| 90 |
if st.button("Step 2: Create EDL"):
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
st.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from datetime import timedelta
|
| 8 |
|
| 9 |
# --- Configuration ---
|
| 10 |
+
# Check for key in environment secrets first (Hugging Face / Docker)
|
| 11 |
+
# If not found, we will use the sidebar input
|
| 12 |
+
ENV_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
| 13 |
|
| 14 |
def format_timecode(seconds, fps=25):
|
| 15 |
td = timedelta(seconds=seconds)
|
|
|
|
| 35 |
rec_start += duration
|
| 36 |
return "\n".join(edl_lines)
|
| 37 |
|
| 38 |
+
def call_gemini_for_edl(transcript_data, story_prompt, api_key):
|
| 39 |
+
if not api_key:
|
| 40 |
+
st.error("Gemini API Key is missing. Please provide it in the sidebar.")
|
| 41 |
return None
|
| 42 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key={api_key}"
|
| 43 |
system_prompt = (
|
| 44 |
"You are an expert Documentary Senior Editor. Use the transcript JSON to create a condensed story. "
|
| 45 |
"Output ONLY a valid JSON array of segments with 'src_start', 'src_end', and 'note'. "
|
|
|
|
| 62 |
st.set_page_config(page_title="DocAI Editor", layout="wide")
|
| 63 |
st.title("Documentary AI: Senior Editor Pipeline")
|
| 64 |
|
| 65 |
+
# Sidebar for Key and Settings
|
| 66 |
+
with st.sidebar:
|
| 67 |
+
st.header("Settings")
|
| 68 |
+
# Priority: User input > Environment Secret
|
| 69 |
+
user_key = st.text_input("Gemini API Key", type="password", help="Paste your Gemini API key here.")
|
| 70 |
+
active_api_key = user_key if user_key else ENV_API_KEY
|
| 71 |
+
|
| 72 |
+
if not active_api_key:
|
| 73 |
+
st.warning("⚠️ No API Key found. Enter it above to enable Step 2.")
|
| 74 |
+
|
| 75 |
+
fps = st.number_input("Timeline FPS", value=25)
|
| 76 |
+
|
| 77 |
uploaded_file = st.file_uploader("Upload Video/Audio", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
|
|
|
| 78 |
|
| 79 |
if uploaded_file:
|
| 80 |
if "transcript" not in st.session_state:
|
| 81 |
if st.button("Step 1: Transcribe"):
|
| 82 |
+
with st.spinner("Processing... Extracting optimized audio and transcribing:"):
|
| 83 |
# Save uploaded file
|
| 84 |
with open("temp_input", "wb") as f:
|
| 85 |
f.write(uploaded_file.getbuffer())
|
| 86 |
|
| 87 |
+
# Optimized FFmpeg for voice: m4a, 64kbps, 16kHz, mono
|
| 88 |
subprocess.run([
|
| 89 |
+
"ffmpeg", "-i", "temp_input",
|
| 90 |
+
"-vn", # No video
|
| 91 |
+
"-acodec", "aac", # AAC codec for m4a
|
| 92 |
+
"-ab", "64k", # 64kbps bitrate
|
| 93 |
+
"-ar", "16000", # 16kHz sample rate
|
| 94 |
+
"-ac", "1", # Mono (single channel)
|
| 95 |
+
"audio.m4a", "-y"
|
| 96 |
])
|
| 97 |
|
| 98 |
# Transcribe with Whisper
|
| 99 |
model = whisper.load_model("base")
|
| 100 |
+
result = model.transcribe("audio.m4a", word_timestamps=True)
|
| 101 |
st.session_state.transcript = result['segments']
|
| 102 |
st.success("Transcription Complete.")
|
| 103 |
|
| 104 |
if "transcript" in st.session_state:
|
| 105 |
st.divider()
|
| 106 |
+
brief = st.text_area("Creative Brief", placeholder="e.g., Create a 2-minute edit focusing on the 'uncorking the brain' story.")
|
| 107 |
+
|
| 108 |
if st.button("Step 2: Create EDL"):
|
| 109 |
+
if not active_api_key:
|
| 110 |
+
st.error("Please provide a Gemini API Key in the sidebar to proceed.")
|
| 111 |
+
else:
|
| 112 |
+
with st.spinner("Senior Editor analyzing transcript..."):
|
| 113 |
+
segments = call_gemini_for_edl(st.session_state.transcript, brief, active_api_key)
|
| 114 |
+
if segments:
|
| 115 |
+
edl_content = generate_cmx_edl("AI_Edit", segments, uploaded_file.name, fps)
|
| 116 |
+
st.subheader("EDL Preview")
|
| 117 |
+
st.text_area("Raw CMX 3600 Output", value=edl_content, height=250)
|
| 118 |
+
st.download_button(
|
| 119 |
+
label="Download EDL for Resolve",
|
| 120 |
+
data=edl_content,
|
| 121 |
+
file_name=f"{os.path.splitext(uploaded_file.name)[0]}_AI_Edit.edl",
|
| 122 |
+
mime="text/plain"
|
| 123 |
+
)
|