Spaces:
Sleeping
Sleeping
Create app.py
Browse filesinitial commit
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def process_srt(file_path, podcast_name, podcast_episode):
|
| 6 |
+
# Get the directory of the current script and go one level up
|
| 7 |
+
script_dir = os.path.dirname(__file__)
|
| 8 |
+
parent_dir = os.path.dirname(script_dir)
|
| 9 |
+
|
| 10 |
+
# Prepare the output path in the 'downloads' directory one level above script_dir
|
| 11 |
+
output_dir = os.path.join(parent_dir, "downloads")
|
| 12 |
+
base_name = os.path.basename(file_path).rsplit('.', 1)[0]
|
| 13 |
+
output_file = os.path.join(output_dir, f"{base_name}.json")
|
| 14 |
+
|
| 15 |
+
# Create the downloads directory if it doesn't exist
|
| 16 |
+
if not os.path.exists(output_dir):
|
| 17 |
+
os.makedirs(output_dir)
|
| 18 |
+
|
| 19 |
+
# Process the SRT file
|
| 20 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 21 |
+
srt_content = file.read()
|
| 22 |
+
|
| 23 |
+
entries = srt_content.strip().split('\n\n')
|
| 24 |
+
transcripts = []
|
| 25 |
+
|
| 26 |
+
for entry in entries:
|
| 27 |
+
lines = entry.split('\n')
|
| 28 |
+
id = int(lines[0])
|
| 29 |
+
timestamp = lines[1]
|
| 30 |
+
timestamp_start, timestamp_end = timestamp.split(" --> ")
|
| 31 |
+
transcript = ' '.join(lines[2:])
|
| 32 |
+
transcripts.append({'podcast_name': podcast_name, 'podcast_episode': podcast_episode, 'line_id': id, 'timestamp_start': timestamp_start, 'timestamp_end': timestamp_end, 'content': transcript})
|
| 33 |
+
|
| 34 |
+
json_data = transcripts
|
| 35 |
+
|
| 36 |
+
# Save the output to the specified JSON file
|
| 37 |
+
with open(output_file, 'w', encoding='utf-8') as outfile:
|
| 38 |
+
json.dump(json_data, outfile, indent=2)
|
| 39 |
+
|
| 40 |
+
return output_file
|
| 41 |
+
|
| 42 |
+
# Create the Gradio interface
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=process_srt,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.File(type='filepath', label='Upload Transcript (.srt)'),
|
| 47 |
+
gr.Textbox(label='Podcast Name'),
|
| 48 |
+
gr.Textbox(label='Podcast Episode')
|
| 49 |
+
],
|
| 50 |
+
outputs='file',
|
| 51 |
+
title='SRT to JSON Converter',
|
| 52 |
+
description='Upload an SRT file and enter the podcast name and episode to convert it to JSON format.'
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
interface.launch(share=True)
|