Upload 2 files
Browse files- main.py +63 -0
- requirements.txt +59 -0
main.py
ADDED
@@ -0,0 +1,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 |
+
server_name="0.0.0.0",
|
61 |
+
server_port=80,
|
62 |
+
debug=False
|
63 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.4.0
|
4 |
+
certifi==2024.7.4
|
5 |
+
charset-normalizer==3.3.2
|
6 |
+
click==8.1.7
|
7 |
+
contourpy==1.2.1
|
8 |
+
cycler==0.12.1
|
9 |
+
distro==1.9.0
|
10 |
+
fastapi==0.112.0
|
11 |
+
ffmpy==0.4.0
|
12 |
+
filelock==3.15.4
|
13 |
+
fonttools==4.53.1
|
14 |
+
fsspec==2024.6.1
|
15 |
+
gradio==4.40.0
|
16 |
+
gradio_client==1.2.0
|
17 |
+
groq==0.9.0
|
18 |
+
h11==0.14.0
|
19 |
+
httpcore==1.0.5
|
20 |
+
httpx==0.27.0
|
21 |
+
huggingface-hub==0.24.5
|
22 |
+
idna==3.7
|
23 |
+
importlib_resources==6.4.0
|
24 |
+
Jinja2==3.1.4
|
25 |
+
kiwisolver==1.4.5
|
26 |
+
markdown-it-py==3.0.0
|
27 |
+
MarkupSafe==2.1.5
|
28 |
+
mdurl==0.1.2
|
29 |
+
numpy==2.0.1
|
30 |
+
orjson==3.10.6
|
31 |
+
packaging==24.1
|
32 |
+
pandas==2.2.2
|
33 |
+
pillow==10.4.0
|
34 |
+
pydantic==2.8.2
|
35 |
+
pydantic_core==2.20.1
|
36 |
+
pydub==0.25.1
|
37 |
+
Pygments==2.18.0
|
38 |
+
pyparsing==3.1.2
|
39 |
+
python-dateutil==2.9.0.post0
|
40 |
+
python-dotenv==1.0.1
|
41 |
+
python-multipart==0.0.9
|
42 |
+
pytz==2024.1
|
43 |
+
PyYAML==6.0.1
|
44 |
+
requests==2.32.3
|
45 |
+
rich==13.7.1
|
46 |
+
ruff==0.5.6
|
47 |
+
semantic-version==2.10.0
|
48 |
+
shellingham==1.5.4
|
49 |
+
six==1.16.0
|
50 |
+
sniffio==1.3.1
|
51 |
+
starlette==0.37.2
|
52 |
+
tomlkit==0.12.0
|
53 |
+
tqdm==4.66.4
|
54 |
+
typer==0.12.3
|
55 |
+
typing_extensions==4.12.2
|
56 |
+
tzdata==2024.1
|
57 |
+
urllib3==2.2.2
|
58 |
+
uvicorn==0.30.5
|
59 |
+
websockets==12.0
|