Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
with gr.Blocks() as app:
|
34 |
-
gr.
|
35 |
-
|
36 |
-
|
37 |
-
output_file = gr.File(label="Download SRT", visible=False)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
if file_path:
|
42 |
-
|
43 |
-
|
44 |
-
output_file.update(visible=False) # Hide if no file
|
45 |
|
46 |
-
|
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)
|