Lenylvt commited on
Commit
a5f2ef0
·
verified ·
1 Parent(s): 9b27f70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -16,34 +16,28 @@ def text_to_srt(text):
16
  continue # Skip lines that don't match the expected format
17
  return srt_content
18
 
19
- def export_file(srt_content):
20
- if srt_content.strip() == "":
21
- return None
22
- # Write SRT content to a temporary file
23
- with NamedTemporaryFile(delete=False, suffix=".srt", mode='w+', dir="./") as tmp_file:
24
- tmp_file.write(srt_content)
25
- return tmp_file.name # Return the path for Gradio to handle
26
-
27
- # Use a stateful text component to hold the path to the temporary SRT file
28
- def update_output(text_input):
29
  srt_content = text_to_srt(text_input)
30
- file_path = export_file(srt_content)
31
- return file_path
 
 
 
 
 
32
 
33
  with gr.Blocks() as app:
34
- gr.Markdown("### Text to SRT Converter")
35
- text_input = gr.TextArea(label="Enter text in the specified format")
36
- export_btn = gr.Button("Export as SRT")
37
- output_file = gr.File(label="Download SRT", visible=False) # Initially hidden
38
 
39
- # Update to use a state variable for holding the SRT file path
40
- def on_export_click(_, file_path):
41
  if file_path:
42
- output_file.update(value=file_path, visible=True) # Make the download link visible
43
- else:
44
- output_file.update(visible=False) # Hide if no file
45
 
46
- text_input.change(fn=update_output, inputs=text_input, outputs=output_file, show_progress=True)
47
- export_btn.click(fn=on_export_click, inputs=[export_btn, text_input], outputs=output_file)
48
 
49
- app.launch()
 
16
  continue # Skip lines that don't match the expected format
17
  return srt_content
18
 
19
+ def convert_and_prepare_file(text_input):
 
 
 
 
 
 
 
 
 
20
  srt_content = text_to_srt(text_input)
21
+ if not srt_content.strip():
22
+ return None # No valid content to create a file
23
+ # Write SRT content to a temporary file
24
+ temp_file = NamedTemporaryFile(delete=False, suffix=".srt", mode='w+', dir="./")
25
+ temp_file.write(srt_content)
26
+ temp_file.close()
27
+ return temp_file.name # Return the path for Gradio to handle
28
 
29
  with gr.Blocks() as app:
30
+ with gr.Row():
31
+ text_input = gr.TextArea(label="Enter text in the specified format")
32
+ export_btn = gr.Button("Export as SRT")
33
+ output_file = gr.File(label="Download SRT", interactive=True, visible=False)
34
 
35
+ def on_export_click(text_input):
36
+ file_path = convert_and_prepare_file(text_input)
37
  if file_path:
38
+ return file_path, True # Return the path and make the file visible
39
+ return None, False # If no file was created, return None and keep the file component hidden
 
40
 
41
+ export_btn.click(fn=on_export_click, inputs=text_input, outputs=[output_file, output_file.visible])
 
42
 
43
+ app.launch(share=True)