Text_to_SRT-API / app.py
Lenylvt's picture
Update app.py
854dfbc verified
raw
history blame
1.19 kB
import gradio as gr
import io
def text_to_srt(text):
lines = text.split('\n')
srt_content = ""
for i, line in enumerate(lines):
if line.strip() == "":
continue
try:
times, content = line.split(']', 1)
start, end = times[1:].split(' -> ')
srt_content += f"{i+1}\n{start.replace('.', ',')} --> {end.replace('.', ',')}\n{content.strip()}\n\n"
except ValueError:
continue # Skip lines that don't match the expected format
return srt_content
def export_file(srt_content):
if srt_content.strip() == "":
return None
# Convert string to bytes and then to a file-like object
bytes_io = io.BytesIO(srt_content.encode('utf-8'))
return bytes_io, "output.srt"
with gr.Blocks() as app:
gr.Markdown("### Text to SRT Converter")
text_input = gr.TextArea(label="Enter text in the specified format")
output = gr.TextArea(label="SRT Output")
export_button = gr.Button("Export as SRT")
text_input.change(fn=text_to_srt, inputs=text_input, outputs=output)
export_button.click(fn=export_file, inputs=output, outputs=gr.File(label="Download SRT"))
app.launch()