Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,10 @@ token = os.getenv("HUGGINGFACE_TOKEN")
|
|
| 12 |
def create_dubsync_interface():
|
| 13 |
client = Client("Tamiloneto8/Test1", hf_token=token, verbose=True)
|
| 14 |
|
| 15 |
-
target_langs = [
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
css = """
|
| 19 |
.gradio-container { max-width: 1200px; margin: auto; }
|
|
@@ -26,10 +28,9 @@ def create_dubsync_interface():
|
|
| 26 |
|
| 27 |
# Step 1 upload and process
|
| 28 |
with gr.Row():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
process_btn = gr.Button("π― Start Processing", variant="primary")
|
| 33 |
|
| 34 |
status_box = gr.Textbox(label="π Processing Status", interactive=False)
|
| 35 |
chunk_info_state = gr.State([])
|
|
@@ -37,23 +38,22 @@ def create_dubsync_interface():
|
|
| 37 |
|
| 38 |
# Step 2 (chunks)
|
| 39 |
with gr.Row(visible=False) as edit_section:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
prev_btn = gr.Button("βοΈ Previous")
|
| 43 |
chunk_counter = gr.Textbox(interactive=False)
|
| 44 |
next_btn = gr.Button("Next βΆοΈ")
|
| 45 |
-
nav_row.update(prev_btn, chunk_counter, next_btn)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
# Step 3 (merge)
|
| 59 |
merge_btn = gr.Button("π Merge All Finalized Chunks", variant="primary", visible=False)
|
|
@@ -63,36 +63,61 @@ def create_dubsync_interface():
|
|
| 63 |
def step1_fn(audio_file, target_language):
|
| 64 |
hf_audio = handle_file(audio_file)
|
| 65 |
logger.info("Calling /process_audio_pipeline_step1")
|
| 66 |
-
out = client.predict(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
logger.info("Step1 result: %s", out)
|
| 68 |
|
| 69 |
status_text = out[0]
|
| 70 |
-
chunks = out[1]
|
| 71 |
first = chunks[0] if chunks else {}
|
| 72 |
-
return
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def navigate_fn(idx, chunks, trans, tr, direction):
|
|
|
|
| 78 |
if chunks and 0 <= idx < len(chunks):
|
| 79 |
chunks[idx]['transcription'] = trans
|
| 80 |
chunks[idx]['translation'] = tr
|
| 81 |
-
new_idx = max(0, min(len(chunks)-1, idx + direction))
|
| 82 |
chunk = chunks[new_idx]
|
| 83 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def gen_chunk_fn(idx, chunks, trans, tr):
|
| 86 |
-
chunk = chunks[idx]
|
| 87 |
logger.info("Calling /generate_dubbed_chunk")
|
| 88 |
-
dubbed = client.predict(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
chunks[idx]['dubbed_audio'] = dubbed
|
| 90 |
return dubbed, chunks
|
| 91 |
|
| 92 |
def finalize_fn(idx, chunks):
|
| 93 |
if chunks and chunks[idx].get('dubbed_audio'):
|
| 94 |
chunks[idx]['finalized'] = True
|
| 95 |
-
return chunks, f"{sum(1 for c in chunks if c.get('finalized',False))} of {len(chunks)} chunks finalized"
|
| 96 |
|
| 97 |
def merge_fn(chunks):
|
| 98 |
logger.info("Calling /merge_audio_files")
|
|
@@ -100,20 +125,66 @@ def create_dubsync_interface():
|
|
| 100 |
return out[0], out[1]
|
| 101 |
|
| 102 |
# Bind events
|
| 103 |
-
process_btn.click(
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
return demo
|
| 119 |
|
|
|
|
| 12 |
def create_dubsync_interface():
|
| 13 |
client = Client("Tamiloneto8/Test1", hf_token=token, verbose=True)
|
| 14 |
|
| 15 |
+
target_langs = [
|
| 16 |
+
"Assamese", "Bengali", "Gujarati", "Hindi", "Kannada",
|
| 17 |
+
"Malayalam", "Marathi", "Odia", "Punjabi", "Tamil", "Telugu"
|
| 18 |
+
]
|
| 19 |
|
| 20 |
css = """
|
| 21 |
.gradio-container { max-width: 1200px; margin: auto; }
|
|
|
|
| 28 |
|
| 29 |
# Step 1 upload and process
|
| 30 |
with gr.Row():
|
| 31 |
+
audio_input = gr.Audio(sources=["upload"], type="filepath", label="π΅ Upload Audio File")
|
| 32 |
+
lang_dropdown = gr.Dropdown(target_langs, label="π Target Language")
|
| 33 |
+
process_btn = gr.Button("π― Start Processing", variant="primary")
|
|
|
|
| 34 |
|
| 35 |
status_box = gr.Textbox(label="π Processing Status", interactive=False)
|
| 36 |
chunk_info_state = gr.State([])
|
|
|
|
| 38 |
|
| 39 |
# Step 2 (chunks)
|
| 40 |
with gr.Row(visible=False) as edit_section:
|
| 41 |
+
# Navigation row
|
| 42 |
+
with gr.Row():
|
| 43 |
prev_btn = gr.Button("βοΈ Previous")
|
| 44 |
chunk_counter = gr.Textbox(interactive=False)
|
| 45 |
next_btn = gr.Button("Next βΆοΈ")
|
|
|
|
| 46 |
|
| 47 |
+
original_audio = gr.Audio(label="Original Chunk Audio", type="filepath")
|
| 48 |
+
transcription_text = gr.Textbox(label="Transcription (edit if needed)", lines=2)
|
| 49 |
+
translation_text = gr.Textbox(label="Translation (edit if needed)", lines=2)
|
| 50 |
|
| 51 |
+
with gr.Row():
|
| 52 |
+
gen_btn = gr.Button("π Generate Dubbed Chunk")
|
| 53 |
+
final_btn = gr.Button("βοΈ Finalize Chunk", variant="secondary")
|
| 54 |
|
| 55 |
+
dubbed_audio = gr.Audio(label="Dubbed Chunk Audio", type="filepath")
|
| 56 |
+
progress_text = gr.Textbox(label="Progress", interactive=False)
|
| 57 |
|
| 58 |
# Step 3 (merge)
|
| 59 |
merge_btn = gr.Button("π Merge All Finalized Chunks", variant="primary", visible=False)
|
|
|
|
| 63 |
def step1_fn(audio_file, target_language):
|
| 64 |
hf_audio = handle_file(audio_file)
|
| 65 |
logger.info("Calling /process_audio_pipeline_step1")
|
| 66 |
+
out = client.predict(
|
| 67 |
+
audio_file=hf_audio,
|
| 68 |
+
target_lang=target_language,
|
| 69 |
+
api_name="/process_audio_pipeline_step1"
|
| 70 |
+
)
|
| 71 |
logger.info("Step1 result: %s", out)
|
| 72 |
|
| 73 |
status_text = out[0]
|
| 74 |
+
chunks = out[1] if len(out) > 1 else []
|
| 75 |
first = chunks[0] if chunks else {}
|
| 76 |
+
return (
|
| 77 |
+
status_text,
|
| 78 |
+
chunks,
|
| 79 |
+
0,
|
| 80 |
+
f"Chunk 1 of {len(chunks)}",
|
| 81 |
+
first.get('original_audio'),
|
| 82 |
+
first.get('transcription'),
|
| 83 |
+
first.get('translation'),
|
| 84 |
+
first.get('dubbed_audio'),
|
| 85 |
+
f"0 of {len(chunks)} chunks finalized",
|
| 86 |
+
True, # show edit_section
|
| 87 |
+
True # show merge_btn
|
| 88 |
+
)
|
| 89 |
|
| 90 |
def navigate_fn(idx, chunks, trans, tr, direction):
|
| 91 |
+
# Save edits
|
| 92 |
if chunks and 0 <= idx < len(chunks):
|
| 93 |
chunks[idx]['transcription'] = trans
|
| 94 |
chunks[idx]['translation'] = tr
|
| 95 |
+
new_idx = max(0, min(len(chunks) - 1, idx + direction))
|
| 96 |
chunk = chunks[new_idx]
|
| 97 |
+
return (
|
| 98 |
+
new_idx,
|
| 99 |
+
f"Chunk {new_idx + 1} of {len(chunks)}",
|
| 100 |
+
chunk.get('original_audio'),
|
| 101 |
+
chunk.get('transcription'),
|
| 102 |
+
chunk.get('translation'),
|
| 103 |
+
chunk.get('dubbed_audio'),
|
| 104 |
+
f"{sum(1 for c in chunks if c.get('finalized', False))} of {len(chunks)} chunks finalized"
|
| 105 |
+
)
|
| 106 |
|
| 107 |
def gen_chunk_fn(idx, chunks, trans, tr):
|
|
|
|
| 108 |
logger.info("Calling /generate_dubbed_chunk")
|
| 109 |
+
dubbed = client.predict(
|
| 110 |
+
transcription=trans,
|
| 111 |
+
translation=tr,
|
| 112 |
+
api_name="/generate_dubbed_chunk"
|
| 113 |
+
)
|
| 114 |
chunks[idx]['dubbed_audio'] = dubbed
|
| 115 |
return dubbed, chunks
|
| 116 |
|
| 117 |
def finalize_fn(idx, chunks):
|
| 118 |
if chunks and chunks[idx].get('dubbed_audio'):
|
| 119 |
chunks[idx]['finalized'] = True
|
| 120 |
+
return chunks, f"{sum(1 for c in chunks if c.get('finalized', False))} of {len(chunks)} chunks finalized"
|
| 121 |
|
| 122 |
def merge_fn(chunks):
|
| 123 |
logger.info("Calling /merge_audio_files")
|
|
|
|
| 125 |
return out[0], out[1]
|
| 126 |
|
| 127 |
# Bind events
|
| 128 |
+
process_btn.click(
|
| 129 |
+
step1_fn,
|
| 130 |
+
inputs=[audio_input, lang_dropdown],
|
| 131 |
+
outputs=[
|
| 132 |
+
status_box,
|
| 133 |
+
chunk_info_state,
|
| 134 |
+
current_index_state,
|
| 135 |
+
chunk_counter,
|
| 136 |
+
original_audio,
|
| 137 |
+
transcription_text,
|
| 138 |
+
translation_text,
|
| 139 |
+
dubbed_audio,
|
| 140 |
+
progress_text,
|
| 141 |
+
edit_section,
|
| 142 |
+
merge_btn
|
| 143 |
+
]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
prev_btn.click(
|
| 147 |
+
navigate_fn,
|
| 148 |
+
inputs=[current_index_state, chunk_info_state, transcription_text, translation_text, gr.Number(-1)],
|
| 149 |
+
outputs=[
|
| 150 |
+
current_index_state,
|
| 151 |
+
chunk_counter,
|
| 152 |
+
original_audio,
|
| 153 |
+
transcription_text,
|
| 154 |
+
translation_text,
|
| 155 |
+
dubbed_audio,
|
| 156 |
+
progress_text
|
| 157 |
+
]
|
| 158 |
+
)
|
| 159 |
+
next_btn.click(
|
| 160 |
+
navigate_fn,
|
| 161 |
+
inputs=[current_index_state, chunk_info_state, transcription_text, translation_text, gr.Number(1)],
|
| 162 |
+
outputs=[
|
| 163 |
+
current_index_state,
|
| 164 |
+
chunk_counter,
|
| 165 |
+
original_audio,
|
| 166 |
+
transcription_text,
|
| 167 |
+
translation_text,
|
| 168 |
+
dubbed_audio,
|
| 169 |
+
progress_text
|
| 170 |
+
]
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
gen_btn.click(
|
| 174 |
+
gen_chunk_fn,
|
| 175 |
+
inputs=[current_index_state, chunk_info_state, transcription_text, translation_text],
|
| 176 |
+
outputs=[dubbed_audio, chunk_info_state]
|
| 177 |
+
)
|
| 178 |
+
final_btn.click(
|
| 179 |
+
finalize_fn,
|
| 180 |
+
inputs=[current_index_state, chunk_info_state],
|
| 181 |
+
outputs=[chunk_info_state, progress_text]
|
| 182 |
+
)
|
| 183 |
+
merge_btn.click(
|
| 184 |
+
merge_fn,
|
| 185 |
+
inputs=[chunk_info_state],
|
| 186 |
+
outputs=[final_output, full_audio]
|
| 187 |
+
)
|
| 188 |
|
| 189 |
return demo
|
| 190 |
|