qqwjq1981 commited on
Commit
4831c39
·
verified ·
1 Parent(s): 3734b02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -9
app.py CHANGED
@@ -166,12 +166,43 @@ def mock_analytics():
166
  "Instagram": {"Views": random.randint(500, 3000), "Engagement Rate": f"{random.uniform(10, 20):.2f}%"},
167
  }
168
 
169
- def update_translations(file, edited_translations):
 
 
 
 
 
 
 
 
 
 
 
 
170
  output_video_path = "output_video.mp4"
171
- updated_translations = json.loads(edited_translations)
172
 
173
- add_transcript_to_video(file.name, translated_json, output_video_path)
174
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  return output_video_path
176
 
177
  # Core functionalities
@@ -226,10 +257,19 @@ def build_interface():
226
  with gr.Column(scale=5):
227
 
228
  gr.Markdown("## Edit Translations")
229
- editable_translations = gr.Textbox(
230
- label="Edit Translated Texts (JSON Format)",
231
- lines=10,
232
- interactive=True
 
 
 
 
 
 
 
 
 
233
  )
234
  save_changes_button = gr.Button("Save Changes")
235
  processed_video_output = gr.File(label="Download Processed Video", interactive=True) # Download button
@@ -254,7 +294,7 @@ def build_interface():
254
 
255
  save_changes_button.click(
256
  update_translations,
257
- inputs=[file_input, editable_translations],
258
  outputs=[processed_video_output]
259
  )
260
 
 
166
  "Instagram": {"Views": random.randint(500, 3000), "Engagement Rate": f"{random.uniform(10, 20):.2f}%"},
167
  }
168
 
169
+ import json
170
+
171
+ def update_translations(file, edited_table):
172
+ """
173
+ Update translations from the Gradio editable table and process the video.
174
+
175
+ Parameters:
176
+ file: Uploaded video file object.
177
+ edited_table: Updated translations from the Gradio editable table (list of dictionaries).
178
+
179
+ Returns:
180
+ output_video_path: The path to the video with updated translations.
181
+ """
182
  output_video_path = "output_video.mp4"
 
183
 
184
+ try:
185
+ # Ensure edited_table is in the expected format
186
+ if not isinstance(edited_table, list):
187
+ raise ValueError("Edited table must be a list of dictionaries.")
188
+
189
+ # Validate and preprocess each entry in the edited_table
190
+ for entry in edited_table:
191
+ if not all(key in entry for key in ["start", "original", "translated", "end"]):
192
+ raise ValueError("Each entry must contain 'start', 'original', 'translated', and 'end' keys.")
193
+ if not isinstance(entry["start"], (float, int)):
194
+ raise ValueError("'start' must be a number.")
195
+ if not isinstance(entry["end"], (float, int)):
196
+ raise ValueError("'end' must be a number.")
197
+ if not isinstance(entry["original"], str) or not isinstance(entry["translated"], str):
198
+ raise ValueError("'original' and 'translated' must be strings.")
199
+
200
+ # Call the function to add subtitles to the video
201
+ add_transcript_to_video(file.name, edited_table, output_video_path)
202
+
203
+ except Exception as e:
204
+ raise ValueError(f"Error updating translations: {e}")
205
+
206
  return output_video_path
207
 
208
  # Core functionalities
 
257
  with gr.Column(scale=5):
258
 
259
  gr.Markdown("## Edit Translations")
260
+
261
+ # Editable JSON Data
262
+ editable_table = gr.Dataframe(
263
+ value=[
264
+ {"start": entry["start"], "original": entry["original"], "translated": entry["translated", "end": entry["end"]]}
265
+ for entry in editable_translations
266
+ ],
267
+ headers=["start", "original", "translated", "end"],
268
+ datatype=["number", "str", "str", "number"],
269
+ row_count=len(translated_json),
270
+ col_count=4,
271
+ interactive=[False, True, True, False], # Control editability
272
+ label="Edit Translations",
273
  )
274
  save_changes_button = gr.Button("Save Changes")
275
  processed_video_output = gr.File(label="Download Processed Video", interactive=True) # Download button
 
294
 
295
  save_changes_button.click(
296
  update_translations,
297
+ inputs=[file_input, editable_table],
298
  outputs=[processed_video_output]
299
  )
300