leicam commited on
Commit
776388f
·
verified ·
1 Parent(s): 6e438fd

Upload 5 files

Browse files
Files changed (5) hide show
  1. LICENSE +22 -0
  2. README.md +37 -0
  3. app.py +286 -0
  4. huggingface.yaml +5 -0
  5. requirements.txt +3 -0
LICENSE ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ MIT License
3
+
4
+ Copyright (c) 2025
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to do so, subject to the
11
+ following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ title: Agente de Edição XML para Premiere
4
+ emoji: 🎬
5
+ colorFrom: blue
6
+ colorTo: purple
7
+ sdk: gradio
8
+ sdk_version: 4.29.0
9
+ app_file: app.py
10
+ pinned: false
11
+ ---
12
+
13
+ # Agente de Edição XML para Premiere (24 fps, com LLM)
14
+
15
+ Este Space lê uma transcrição com timecodes (hh:mm:ss:ff) e um XML de sequência do Adobe Premiere (FCP XML), seleciona de 2 a 5 trechos "fortes" com ajuda de **LLM (Gemini)** e recria a timeline usando apenas esses cortes, mantendo áudio e vídeo sincronizados — sem criar nova `<sequence>` e sem adicionar mídias externas.
16
+
17
+ ## Como usar
18
+ 1. Envie **o XML (FCP XML) da sequência** exportado do Premiere.
19
+ 2. Envie **um `.txt` com a transcrição**, em que cada linha pode ser:
20
+ - `hh:mm:ss:ff - hh:mm:ss:ff Texto...`
21
+ - `[hh:mm:ss:ff - hh:mm:ss:ff] Texto...`
22
+ - `hh:mm:ss:ff Texto...` (assume janela ~4s).
23
+ 3. (Opcional) Marque **Usar LLM (Gemini)** para seleção semântica dos trechos.
24
+ 4. Baixe o **XML editado**.
25
+
26
+ ## LLM (Gemini)
27
+ - Configure a variável de ambiente **GEMINI_API_KEY** no Space (Settings → Repository secrets) para ativar o LLM.
28
+ - Se o LLM não estiver disponível, o app **usa heurística local** (palavras‑chave de emoção/quebra/aprendizado/viralização) e segue idêntico no restante do pipeline.
29
+
30
+ ## Detalhes
31
+ - Base temporal: **24 fps** (`timebase=24`, `ntsc=FALSE` forçados).
32
+ - Edita **a mesma `<sequence>`**, limpa clipitems originais e recria somente os cortes escolhidos, preservando trilhas e referências de arquivo.
33
+ - Espera estrutura padrão: `media/video/track` e `media/audio/track`, cada um contendo ao menos um `clipitem` de referência.
34
+ - Em projetos multicam/multitrilha complexos, ajuste a função que seleciona o `clipitem` molde.
35
+
36
+ ## Licença
37
+ MIT
app.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import re
4
+ import xml.etree.ElementTree as ET
5
+ from dataclasses import dataclass
6
+ from typing import List
7
+ import gradio as gr
8
+
9
+ # Optional LLM (Gemini)
10
+ USE_LLM_DEFAULT = True # default checked
11
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "").strip()
12
+
13
+ LLM_AVAILABLE = False
14
+ LLM_MODEL_NAME = "gemini-2.5-flash"
15
+ try:
16
+ if GEMINI_API_KEY:
17
+ import google.generativeai as genai
18
+ genai.configure(api_key=GEMINI_API_KEY)
19
+ LLM = genai.GenerativeModel(LLM_MODEL_NAME)
20
+ LLM_AVAILABLE = True
21
+ else:
22
+ LLM = None
23
+ except Exception:
24
+ LLM = None
25
+ LLM_AVAILABLE = False
26
+
27
+ # Config
28
+ FPS = 24
29
+ OUTPUT_DIR = "./Output"
30
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
31
+
32
+ @dataclass
33
+ class Segment:
34
+ start_tc: str
35
+ end_tc: str
36
+ start_f: int
37
+ end_f: int
38
+ text: str
39
+ score: float
40
+
41
+ # ---- Timecode helpers ----
42
+ def parse_timecode_to_frames(tc: str, fps: int = FPS) -> int:
43
+ m = re.match(r"^\s*(\d{2}):(\d{2}):(\d{2})[:;](\d{2})\s*$", tc)
44
+ if not m:
45
+ raise ValueError(f"Invalid timecode: {tc}")
46
+ hh, mm, ss, ff = map(int, m.groups())
47
+ return hh*3600*fps + mm*60*fps + ss*fps + ff
48
+
49
+ def frames_to_timecode(frames: int, fps: int = FPS) -> str:
50
+ hh = frames // (3600*fps)
51
+ rem = frames % (3600*fps)
52
+ mm = rem // (60*fps)
53
+ rem = rem % (60*fps)
54
+ ss = rem // fps
55
+ ff = rem % fps
56
+ return f"{hh:02d}:{mm:02d}:{ss:02d}:{ff:02d}"
57
+
58
+ # ---- Transcript parsing & scoring ----
59
+ def parse_transcript(txt: str) -> List[Segment]:
60
+ lines = [l.strip() for l in txt.splitlines() if l.strip()]
61
+ results: List[Segment] = []
62
+ pat_range = re.compile(r"^\[?\s*(\d{2}:\d{2}:\d{2}[:;]\d{2})\s*[-–]\s*(\d{2}:\d{2}:\d{2}[:;]\d{2})\s*\]?\s+(.*)$")
63
+ pat_point = re.compile(r"^(\d{2}:\d{2}:\d{2}[:;]\d{2})\s+(.*)$")
64
+ for l in lines:
65
+ m = pat_range.match(l)
66
+ if m:
67
+ s, e, text = m.groups()
68
+ try:
69
+ s_f = parse_timecode_to_frames(s)
70
+ e_f = parse_timecode_to_frames(e)
71
+ if e_f > s_f:
72
+ results.append(Segment(s, e, s_f, e_f, text, 0.0))
73
+ except Exception:
74
+ continue
75
+ continue
76
+ m = pat_point.match(l)
77
+ if m:
78
+ s, text = m.groups()
79
+ try:
80
+ s_f = parse_timecode_to_frames(s)
81
+ e_f = s_f + 4*FPS # default window
82
+ e = frames_to_timecode(e_f)
83
+ results.append(Segment(s, e, s_f, e_f, text, 0.0))
84
+ except Exception:
85
+ continue
86
+ return results
87
+
88
+ def keyword_score(text: str) -> float:
89
+ t = text.lower()
90
+ kw_emotion = ["medo", "coragem", "raiva", "chorei", "feliz", "triste", "emocion", "culpa", "vergonha", "orgulho"]
91
+ kw_break = ["nunca", "de repente", "contraintuitivo", "ninguém te conta", "parei", "decidi", "quebrei", "virada"]
92
+ kw_learn = ["aprendi", "descobri", "lição", "entendi", "percebi", "insight", "melhorou", "piorou"]
93
+ kw_viral = ["segredo", "verdade", "por trás", "3 passos", "passo a passo", "como eu", "ninguém fala"]
94
+
95
+ score = 0.0
96
+ for kw in kw_emotion: score += 2.0 if kw in t else 0.0
97
+ for kw in kw_break: score += 1.5 if kw in t else 0.0
98
+ for kw in kw_learn: score += 1.2 if kw in t else 0.0
99
+ for kw in kw_viral: score += 1.0 if kw in t else 0.0
100
+ score += 0.2 * text.count("!")
101
+ score += 0.0005 * len(text)
102
+ return score
103
+
104
+ def llm_rank_segments(candidates: List[Segment]) -> List[Segment]:
105
+ """Ask the LLM to pick 2–5 best segments by narrative strength."""
106
+ if not LLM_AVAILABLE:
107
+ return candidates
108
+ sample = "\n".join([f"{i}. [{c.start_tc}-{c.end_tc}] {c.text[:300]}" for i, c in enumerate(candidates)])
109
+ prompt = (
110
+ "Você é um editor. Selecione de 2 a 5 trechos mais fortes pela emoção, quebra de expectativa e aprendizado, "
111
+ "mantendo uma mini-narrativa coerente. Responda apenas com índices (0-based) separados por vírgula.\n\n"
112
+ f"{sample}"
113
+ )
114
+ try:
115
+ r = LLM.generate_content(prompt, generation_config={"temperature": 0.2})
116
+ txt = (r.text or "").strip()
117
+ idxs = [int(x) for x in re.findall(r"\d+", txt)]
118
+ idxs = [i for i in idxs if 0 <= i < len(candidates)]
119
+ if 2 <= len(idxs) <= 5:
120
+ return [candidates[i] for i in idxs]
121
+ except Exception:
122
+ pass
123
+ return candidates[:min(5, max(2, len(candidates)))]
124
+
125
+ def select_segments(transcript_txt: str, use_llm: bool) -> List[Segment]:
126
+ segs = parse_transcript(transcript_txt)
127
+ if not segs:
128
+ raise ValueError("Nenhum trecho válido encontrado na transcrição.")
129
+ for s in segs:
130
+ s.score = keyword_score(s.text)
131
+ # Heuristic pre-filter
132
+ segs.sort(key=lambda x: x.score, reverse=True)
133
+ top = segs[:12]
134
+ if use_llm and LLM_AVAILABLE:
135
+ ranked = llm_rank_segments(top)
136
+ return ranked[:min(5, max(2, len(ranked)))]
137
+ return top[:min(5, max(2, len(top)))]
138
+
139
+ # ---- XML editing ----
140
+ def get_sequence(root: ET.Element) -> ET.Element:
141
+ seq = root.find(".//sequence")
142
+ if seq is None:
143
+ raise ValueError("Nenhuma <sequence> encontrada no XML.")
144
+ return seq
145
+
146
+ def ensure_rate_24fps(element: ET.Element):
147
+ rate = element.find("./rate")
148
+ if rate is None:
149
+ rate = ET.SubElement(element, "rate")
150
+ tb = rate.find("timebase")
151
+ if tb is None:
152
+ tb = ET.SubElement(rate, "timebase")
153
+ tb.text = str(FPS)
154
+ ntsc = rate.find("ntsc")
155
+ if ntsc is None:
156
+ ntsc = ET.SubElement(rate, "ntsc")
157
+ ntsc.text = "FALSE"
158
+
159
+ def deep_copy(elem: ET.Element) -> ET.Element:
160
+ new = ET.Element(elem.tag, attrib=elem.attrib)
161
+ new.text = elem.text
162
+ new.tail = elem.tail
163
+ for child in list(elem):
164
+ new.append(deep_copy(child))
165
+ return new
166
+
167
+ def clear_clipitems(track_elem: ET.Element):
168
+ for ci in list(track_elem.findall("./clipitem")):
169
+ track_elem.remove(ci)
170
+
171
+ def first_clipitem_ref(track_elem: ET.Element):
172
+ return track_elem.find("./clipitem")
173
+
174
+ def copy_file_ref(from_clip: ET.Element, to_clip: ET.Element):
175
+ src_file = from_clip.find("./file")
176
+ if src_file is not None:
177
+ old = to_clip.find("./file")
178
+ if old is not None:
179
+ to_clip.remove(old)
180
+ to_clip.append(deep_copy(src_file))
181
+
182
+ def build_clipitem(template_ci: ET.Element, cid: str, start_f: int, end_f: int, in_f: int, out_f: int, linked_ids):
183
+ ci = ET.Element("clipitem", {"id": cid})
184
+ name = template_ci.find("name")
185
+ ci_name = ET.SubElement(ci, "name")
186
+ ci_name.text = name.text if name is not None else cid
187
+
188
+ rate = template_ci.find("rate")
189
+ ci.append(deep_copy(rate) if rate is not None else ET.Element("rate"))
190
+ ensure_rate_24fps(ci)
191
+
192
+ for tag, val in [("start", start_f), ("end", end_f), ("in", in_f), ("out", out_f)]:
193
+ t = ET.SubElement(ci, tag)
194
+ t.text = str(val)
195
+
196
+ copy_file_ref(template_ci, ci)
197
+
198
+ for lid in linked_ids:
199
+ link = ET.SubElement(ci, "link")
200
+ linkclipref = ET.SubElement(link, "linkclipref")
201
+ linkclipref.text = lid
202
+ mediatype = ET.SubElement(link, "mediatype")
203
+ mediatype.text = "video" if "-v" in lid else "audio"
204
+ return ci
205
+
206
+ def edit_sequence_with_segments(tree: ET.ElementTree, segs: List[Segment]) -> ET.ElementTree:
207
+ root = tree.getroot()
208
+ seq = get_sequence(root)
209
+ ensure_rate_24fps(seq)
210
+
211
+ video_track = seq.find("./media/video/track")
212
+ audio_track = seq.find("./media/audio/track")
213
+
214
+ if video_track is None or audio_track is None:
215
+ raise ValueError("Estrutura de trilhas não encontrada (esperado media/video/track e media/audio/track).")
216
+
217
+ v_tpl = first_clipitem_ref(video_track)
218
+ a_tpl = first_clipitem_ref(audio_track)
219
+ if v_tpl is None or a_tpl is None:
220
+ raise ValueError("Não há clipitem de referência em V1 e/ou A1.")
221
+
222
+ clear_clipitems(video_track)
223
+ clear_clipitems(audio_track)
224
+
225
+ cursor = 0
226
+ for idx, s in enumerate(segs, start=1):
227
+ dur = s.end_f - s.start_f
228
+ start = cursor
229
+ end = cursor + dur
230
+
231
+ v_id = f"clipitem-v-cut{idx}"
232
+ a_id = f"clipitem-a-cut{idx}"
233
+
234
+ v_ci = build_clipitem(v_tpl, v_id, start, end, s.start_f, s.end_f, [a_id])
235
+ a_ci = build_clipitem(a_tpl, a_id, start, end, s.start_f, s.end_f, [v_id])
236
+
237
+ video_track.append(v_ci)
238
+ audio_track.append(a_ci)
239
+
240
+ cursor = end
241
+
242
+ return tree
243
+
244
+ # ---- Gradio app ----
245
+ def process_xml_and_transcript(premiere_xml_file, transcript_txt_file, use_llm):
246
+ if premiere_xml_file is None or transcript_txt_file is None:
247
+ return "Envie o XML do Premiere e a transcrição em .txt.", None, f"LLM disponível: {LLM_AVAILABLE}"
248
+ with open(transcript_txt_file.name, "r", encoding="utf-8") as f:
249
+ transcript = f.read()
250
+
251
+ segs = select_segments(transcript, use_llm and LLM_AVAILABLE)
252
+
253
+ tree = ET.parse(premiere_xml_file.name)
254
+ tree = edit_sequence_with_segments(tree, segs)
255
+
256
+ base = os.path.splitext(os.path.basename(premiere_xml_file.name))[0]
257
+ out_path = os.path.join(OUTPUT_DIR, f"{base}_EDITADO.xml")
258
+ tree.write(out_path, encoding="utf-8", xml_declaration=True)
259
+
260
+ resumo = "Cortes aplicados (24 fps):\n"
261
+ for i, s in enumerate(segs, 1):
262
+ resumo += f"{i}. {s.start_tc} -> {s.end_tc} | {s.end_f - s.start_f} frames | {s.text[:120]}\n"
263
+
264
+ status = f"LLM disponível: {LLM_AVAILABLE} | LLM ligado na UI: {use_llm}"
265
+ return resumo, out_path, status
266
+
267
+ with gr.Blocks() as demo:
268
+ gr.Markdown("# Agente de Edição XML para Premiere (24 fps) — Modo LLM")
269
+ gr.Markdown("Lê transcrição com timecodes e edita **a mesma sequência** no XML do Premiere. "
270
+ "Mantenha V/A sincronizados, sem mídias externas, sem nova `<sequence>`.")
271
+
272
+ with gr.Row():
273
+ xml_in = gr.File(label="XML da sequência do Premiere (FCP XML)", file_types=[".xml"])
274
+ txt_in = gr.File(label="Transcrição (.txt) hh:mm:ss:ff", file_types=[".txt"])
275
+
276
+ use_llm = gr.Checkbox(label="Usar LLM (Gemini) para seleção semântica", value=USE_LLM_DEFAULT and LLM_AVAILABLE)
277
+ run_btn = gr.Button("Processar")
278
+ resumo_out = gr.Textbox(label="Resumo dos cortes aplicados")
279
+ file_out = gr.File(label="Download do XML Editado")
280
+ status_out = gr.Textbox(label="Status do LLM", interactive=False)
281
+
282
+ run_btn.click(process_xml_and_transcript, inputs=[xml_in, txt_in, use_llm],
283
+ outputs=[resumo_out, file_out, status_out])
284
+
285
+ if __name__ == "__main__":
286
+ demo.launch()
huggingface.yaml ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+
2
+ sdk: gradio
3
+ sdk_version: 4.29.0
4
+ python_version: 3.10
5
+ app_file: app.py
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+
2
+ gradio>=4.29.0
3
+ google-generativeai>=0.7.0