NickVerri commited on
Commit
ca338e2
·
verified ·
1 Parent(s): f59e7a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -1
app.py CHANGED
@@ -69,5 +69,126 @@ def call_gemini_for_edl(transcript_data, story_prompt, api_key):
69
  "4. TIMESTAMP INTEGRITY: Use only the exact word-level start and end times from the data."
70
  )
71
 
 
 
 
72
  payload = {
73
- "contents": [{"parts": [{"text": f"Creative Brief: {story_prompt}\n\nTranscript Data:\n{json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  "4. TIMESTAMP INTEGRITY: Use only the exact word-level start and end times from the data."
70
  )
71
 
72
+ # Cleaned up payload to prevent SyntaxErrors with f-strings
73
+ prompt_text = f"Creative Brief: {story_prompt}\n\nTranscript Data:\n{json.dumps(transcript_data)}"
74
+
75
  payload = {
76
+ "contents": [{
77
+ "parts": [{"text": prompt_text}]
78
+ }],
79
+ "systemInstruction": {
80
+ "parts": [{"text": system_prompt}]
81
+ },
82
+ "generationConfig": {
83
+ "responseMimeType": "application/json"
84
+ }
85
+ }
86
+
87
+ try:
88
+ res = requests.post(url, json=payload)
89
+ res.raise_for_status()
90
+ result_json = res.json()
91
+ return json.loads(result_json['candidates'][0]['content']['parts'][0]['text'])
92
+ except Exception as e:
93
+ st.error(f"Senior Editor AI Error: {e}")
94
+ return None
95
+
96
+ # --- Streamlit UI ---
97
+ st.set_page_config(page_title="DocAI Editor", layout="wide")
98
+ st.title("Documentary AI: Pipeline")
99
+
100
+ with st.sidebar:
101
+ st.header("Project Settings")
102
+ fps = st.number_input("Timeline FPS", value=25)
103
+
104
+ st.divider()
105
+ st.info("API Keys and Tokens are managed via Environment Secrets for security.")
106
+ if not ACTIVE_GEMINI_KEY:
107
+ st.error("⚠️ Gemini API Key not found!")
108
+ if not ACTIVE_HF_TOKEN:
109
+ st.error("⚠️ HF Token not found!")
110
+
111
+ uploaded_file = st.file_uploader("Upload Video/Audio Clip", type=["mp4", "m4a", "wav", "mp3", "mov"])
112
+
113
+ if uploaded_file:
114
+ # --- Step 1: Technical Processing ---
115
+ if "transcript" not in st.session_state:
116
+ if st.button("Step 1: Transcribe & Diarize"):
117
+ if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
118
+ st.error("Please provide a valid Hugging Face Token in Space Secrets or app.py.")
119
+ else:
120
+ with st.spinner("Processing... Extracting audio, identifying speakers, and transcribing:"):
121
+ # Save local temp file
122
+ with open("temp_input", "wb") as f:
123
+ f.write(uploaded_file.getbuffer())
124
+
125
+ # Optimized Audio: m4a, 64kbps, 16kHz, mono
126
+ subprocess.run([
127
+ "ffmpeg", "-i", "temp_input",
128
+ "-vn", "-acodec", "aac", "-ab", "64k", "-ar", "16000", "-ac", "1",
129
+ "audio_optimized.m4a", "-y"
130
+ ])
131
+
132
+ # 1. Diarization
133
+ try:
134
+ # Fix for pyannote.audio 3.0+: 'use_auth_token' is now just 'token'
135
+ try:
136
+ pipeline = Pipeline.from_pretrained(
137
+ "pyannote/speaker-diarization@2.1",
138
+ token=ACTIVE_HF_TOKEN
139
+ )
140
+ except TypeError:
141
+ # Fallback for older versions of pyannote.audio
142
+ pipeline = Pipeline.from_pretrained(
143
+ "pyannote/speaker-diarization@2.1",
144
+ use_auth_token=ACTIVE_HF_TOKEN
145
+ )
146
+
147
+ if torch.cuda.is_available():
148
+ pipeline.to(torch.device("cuda"))
149
+
150
+ diarization = pipeline("audio_optimized.m4a")
151
+ except Exception as e:
152
+ st.error(f"Diarization Error: {e}")
153
+ st.stop()
154
+
155
+ # 2. Whisper Transcription
156
+ model = whisper.load_model("base")
157
+ result = model.transcribe("audio_optimized.m4a", word_timestamps=True)
158
+
159
+ # 3. Alignment
160
+ final_segments = []
161
+ for segment in result['segments']:
162
+ mid_time = (segment['start'] + segment['end']) / 2
163
+ speaker = "Unknown"
164
+ for turn, _, speaker_id in diarization.itertracks(yield_label=True):
165
+ if turn.start <= mid_time <= turn.end:
166
+ speaker = speaker_id
167
+ break
168
+
169
+ final_segments.append({
170
+ "speaker": speaker,
171
+ "text": segment['text'],
172
+ "start": segment['start'],
173
+ "end": segment['end'],
174
+ "words": segment.get('words', [])
175
+ })
176
+
177
+ st.session_state.transcript = final_segments
178
+ st.success("Transcription and Diarization Complete.")
179
+
180
+ if "transcript" in st.session_state:
181
+ st.divider()
182
+ brief = st.text_area("Creative Brief", placeholder="e.g. Focus on the yeast story, remove the interviewer.")
183
+
184
+ if st.button("Step 2: Create EDL"):
185
+ if not ACTIVE_GEMINI_KEY:
186
+ st.error("Gemini API Key required. Please set it in Secrets or app.py.")
187
+ else:
188
+ with st.spinner("Analyzing..."):
189
+ edl_segments = call_gemini_for_edl(st.session_state.transcript, brief, ACTIVE_GEMINI_KEY)
190
+ if edl_segments:
191
+ final_edl = generate_cmx_edl("AI_Senior_Editor_Cut", edl_segments, uploaded_file.name, fps)
192
+ st.subheader("EDL Preview")
193
+ st.code(final_edl, language="text")
194
+ st.download_button("Download EDL", data=final_edl, file_name="edit.edl")