Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,61 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
import gradio as gr
|
4 |
-
from groq import Groq
|
5 |
-
|
6 |
-
load_dotenv()
|
7 |
-
|
8 |
-
GROQ_API_KEY=os.getenv("GROQ_API_KEY")
|
9 |
-
|
10 |
-
client = Groq(api_key=GROQ_API_KEY)
|
11 |
-
|
12 |
-
def format_time(seconds):
|
13 |
-
hours = int(seconds // 3600)
|
14 |
-
minutes = int((seconds % 3600) // 60)
|
15 |
-
seconds = int(seconds % 60)
|
16 |
-
milliseconds = int((seconds % 1) * 1000)
|
17 |
-
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
18 |
-
|
19 |
-
def json_to_srt(transcription_json):
|
20 |
-
srt_lines = []
|
21 |
-
for segment in transcription_json:
|
22 |
-
start_time = format_time(segment['start'])
|
23 |
-
end_time = format_time(segment['end'])
|
24 |
-
text = segment['text']
|
25 |
-
srt_line = f"{segment['id']+1}\n{start_time} --> {end_time}\n{text}\n"
|
26 |
-
srt_lines.append(srt_line)
|
27 |
-
return '\n'.join(srt_lines)
|
28 |
-
|
29 |
-
def generate_subtitles(input_file):
|
30 |
-
try:
|
31 |
-
with open(input_file.name, "rb") as file:
|
32 |
-
transcription_json_response = client.audio.transcriptions.create(
|
33 |
-
file=(os.path.basename(input_file.name), file.read()),
|
34 |
-
model="whisper-large-v3",
|
35 |
-
response_format="verbose_json",
|
36 |
-
)
|
37 |
-
transcription_json = transcription_json_response.segments
|
38 |
-
srt_content = json_to_srt(transcription_json)
|
39 |
-
temp_srt_path = os.path.splitext(input_file.name)[0] + ".srt"
|
40 |
-
with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
|
41 |
-
temp_srt_file.write(srt_content)
|
42 |
-
return temp_srt_path
|
43 |
-
except Exception as e:
|
44 |
-
raise gr.Error(f"Error creating SRT file: {e}")
|
45 |
-
|
46 |
-
with gr.Blocks() as demo:
|
47 |
-
gr.Markdown("# Simple Subtitle Maker")
|
48 |
-
input_file = gr.File(label="Upload Audio/Video")
|
49 |
-
transcribe_button = gr.Button("Generate Subtitles")
|
50 |
-
srt_output = gr.File(label="SRT Output File")
|
51 |
-
|
52 |
-
transcribe_button.click(
|
53 |
-
fn=generate_subtitles,
|
54 |
-
inputs=[input_file],
|
55 |
-
outputs=[srt_output],
|
56 |
-
)
|
57 |
-
|
58 |
-
demo.launch(
|
59 |
-
share=False,
|
60 |
-
|
61 |
-
server_port=80,
|
62 |
-
debug=False
|
63 |
)
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import gradio as gr
|
4 |
+
from groq import Groq
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
GROQ_API_KEY=os.getenv("GROQ_API_KEY")
|
9 |
+
|
10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
11 |
+
|
12 |
+
def format_time(seconds):
|
13 |
+
hours = int(seconds // 3600)
|
14 |
+
minutes = int((seconds % 3600) // 60)
|
15 |
+
seconds = int(seconds % 60)
|
16 |
+
milliseconds = int((seconds % 1) * 1000)
|
17 |
+
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
18 |
+
|
19 |
+
def json_to_srt(transcription_json):
|
20 |
+
srt_lines = []
|
21 |
+
for segment in transcription_json:
|
22 |
+
start_time = format_time(segment['start'])
|
23 |
+
end_time = format_time(segment['end'])
|
24 |
+
text = segment['text']
|
25 |
+
srt_line = f"{segment['id']+1}\n{start_time} --> {end_time}\n{text}\n"
|
26 |
+
srt_lines.append(srt_line)
|
27 |
+
return '\n'.join(srt_lines)
|
28 |
+
|
29 |
+
def generate_subtitles(input_file):
|
30 |
+
try:
|
31 |
+
with open(input_file.name, "rb") as file:
|
32 |
+
transcription_json_response = client.audio.transcriptions.create(
|
33 |
+
file=(os.path.basename(input_file.name), file.read()),
|
34 |
+
model="whisper-large-v3",
|
35 |
+
response_format="verbose_json",
|
36 |
+
)
|
37 |
+
transcription_json = transcription_json_response.segments
|
38 |
+
srt_content = json_to_srt(transcription_json)
|
39 |
+
temp_srt_path = os.path.splitext(input_file.name)[0] + ".srt"
|
40 |
+
with open(temp_srt_path, "w", encoding="utf-8") as temp_srt_file:
|
41 |
+
temp_srt_file.write(srt_content)
|
42 |
+
return temp_srt_path
|
43 |
+
except Exception as e:
|
44 |
+
raise gr.Error(f"Error creating SRT file: {e}")
|
45 |
+
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("# Simple Subtitle Maker")
|
48 |
+
input_file = gr.File(label="Upload Audio/Video")
|
49 |
+
transcribe_button = gr.Button("Generate Subtitles")
|
50 |
+
srt_output = gr.File(label="SRT Output File")
|
51 |
+
|
52 |
+
transcribe_button.click(
|
53 |
+
fn=generate_subtitles,
|
54 |
+
inputs=[input_file],
|
55 |
+
outputs=[srt_output],
|
56 |
+
)
|
57 |
+
|
58 |
+
demo.launch(
|
59 |
+
share=False,
|
60 |
+
debug=False
|
|
|
|
|
61 |
)
|