NickVerri commited on
Commit
08ca081
·
verified ·
1 Parent(s): a0ea5c5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import subprocess
3
+ import whisper
4
+ import json
5
+ import os
6
+ import requests
7
+ from datetime import timedelta
8
+
9
+ # --- Configuration ---
10
+ # This key should be set in your Hugging Face Space Secrets
11
+ API_KEY = os.environ.get("GEMINI_API_KEY", "")
12
+
13
+ def format_timecode(seconds, fps=25):
14
+ td = timedelta(seconds=seconds)
15
+ total_seconds = int(td.total_seconds())
16
+ hours = total_seconds // 3600
17
+ minutes = (total_seconds % 3600) // 60
18
+ secs = total_seconds % 60
19
+ frames = int((seconds - total_seconds) * fps)
20
+ return f"{hours:02}:{minutes:02}:{secs:02}:{frames:02}"
21
+
22
+ def generate_cmx_edl(edl_title, segments, source_name, fps=25):
23
+ edl_lines = [f"TITLE: {edl_title}", "FCM: NON-DROP FRAME\n"]
24
+ rec_start = 0.0
25
+ for i, seg in enumerate(segments, 1):
26
+ src_in = format_timecode(seg['src_start'], fps)
27
+ src_out = format_timecode(seg['src_end'], fps)
28
+ duration = seg['src_end'] - seg['src_start']
29
+ rec_in = format_timecode(rec_start, fps)
30
+ rec_out = format_timecode(rec_start + duration, fps)
31
+ edl_lines.append(f"{i:03} AX V C {src_in} {src_out} {rec_in} {rec_out}")
32
+ edl_lines.append(f"* FROM CLIP NAME: {source_name}")
33
+ edl_lines.append(f"* {seg.get('note', 'Clip')}\n")
34
+ rec_start += duration
35
+ return "\n".join(edl_lines)
36
+
37
+ def call_gemini_for_edl(transcript_data, story_prompt):
38
+ if not API_KEY:
39
+ st.error("Gemini API Key not found in Environment Variables. Please set GEMINI_API_KEY in Space Secrets.")
40
+ return None
41
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key={API_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'. "
45
+ "Remove fluff, repeats, and interviewer interruptions. Focus on the most engaging content."
46
+ )
47
+ payload = {
48
+ "contents": [{"parts": [{"text": f"Brief: {story_prompt}\n\nTranscript:\n{json.dumps(transcript_data)}"}]}],
49
+ "systemInstruction": {"parts": [{"text": system_prompt}]},
50
+ "generationConfig": {"responseMimeType": "application/json"}
51
+ }
52
+ try:
53
+ res = requests.post(url, json=payload)
54
+ res.raise_for_status()
55
+ return json.loads(res.json()['candidates'][0]['content']['parts'][0]['text'])
56
+ except Exception as e:
57
+ st.error(f"Error calling Gemini: {e}")
58
+ return None
59
+
60
+ # --- App Interface ---
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... Stripping audio and transcribing (this may take a few minutes):"):
71
+ # Save uploaded file
72
+ with open("temp_input", "wb") as f:
73
+ f.write(uploaded_file.getbuffer())
74
+
75
+ # Extract audio using FFmpeg
76
+ subprocess.run([
77
+ "ffmpeg", "-i", "temp_input", "-vn", "-acodec", "pcm_s16le",
78
+ "-ar", "44100", "-ac", "2", "audio.wav", "-y"
79
+ ])
80
+
81
+ # Transcribe with Whisper
82
+ model = whisper.load_model("base")
83
+ result = model.transcribe("audio.wav", word_timestamps=True)
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 and the importance of public education.")
90
+ if st.button("Step 2: Create EDL"):
91
+ with st.spinner("Senior Editor analyzing transcript..."):
92
+ segments = call_gemini_for_edl(st.session_state.transcript, brief)
93
+ if segments:
94
+ edl_content = generate_cmx_edl("AI_Edit", segments, uploaded_file.name, fps)
95
+ st.subheader("EDL Preview")
96
+ st.text_area("Raw CMX 3600 Output", value=edl_content, height=250)
97
+ st.download_button(
98
+ label="Download EDL for Resolve",
99
+ data=edl_content,
100
+ file_name=f"{os.path.splitext(uploaded_file.name)[0]}_AI_Edit.edl",
101
+ mime="text/plain"
102
+ )