Lenylvt commited on
Commit
9b27f70
1 Parent(s): db77ec0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
- import io
 
3
 
4
  def text_to_srt(text):
5
  lines = text.split('\n')
@@ -18,14 +19,31 @@ def text_to_srt(text):
18
  def export_file(srt_content):
19
  if srt_content.strip() == "":
20
  return None
21
- return gr.File(content=srt_content.encode('utf-8'), file_name="output.srt")
 
 
 
 
 
 
 
 
 
22
 
23
  with gr.Blocks() as app:
24
  gr.Markdown("### Text to SRT Converter")
25
  text_input = gr.TextArea(label="Enter text in the specified format")
26
- output = gr.TextArea(label="SRT Output")
27
- export_button = gr.Button("Export as SRT")
28
- text_input.change(fn=text_to_srt, inputs=text_input, outputs=output)
29
- export_button.click(fn=export_file, inputs=output, outputs=gr.File(label="Download SRT"))
 
 
 
 
 
 
 
 
30
 
31
  app.launch()
 
1
  import gradio as gr
2
+ import os
3
+ from tempfile import NamedTemporaryFile
4
 
5
  def text_to_srt(text):
6
  lines = text.split('\n')
 
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()