andromeda123 commited on
Commit
e7f63a4
1 Parent(s): e7d7314

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # !apt install imagemagick
4
+
5
+ # !cat /etc/ImageMagick-6/policy.xml | sed 's/none/read,write/g'> /etc/ImageMagick-6/policy.xml
6
+
7
+ # Place files in this path or modify the paths to point to where the files are
8
+ srtfilename = "subtitles.txt"
9
+ mp4filename = "video.mp4"
10
+
11
+ import sys
12
+ import os
13
+ import subprocess
14
+ import streamlit as st
15
+ from faster_whisper import WhisperModel
16
+ import time
17
+
18
+ def save_uploadedfile(uploadedfile):
19
+ with open(filename,"wb") as f:
20
+ f.write(uploadedfile.getbuffer())
21
+
22
+ def time_to_seconds(time_obj):
23
+ return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
24
+
25
+ def video2mp3(video_file, output_ext="mp3"):
26
+ filename, ext = os.path.splitext(video_file)
27
+ subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
28
+ stdout=subprocess.DEVNULL,
29
+ stderr=subprocess.STDOUT)
30
+ return f"{filename}.{output_ext}"
31
+
32
+ def translate(audio , model):
33
+ options = dict(beam_size=5, best_of=5)
34
+ translate_options = dict(task="translate", **options)
35
+ result,info = model.transcribe(audio_file,**translate_options)
36
+ return result
37
+
38
+ def format_timestamp(time):
39
+ if(time< 0): return "timestamp cannot be negative"
40
+ time_in_ms = round(time* 1000.0)
41
+
42
+ hours = time_in_ms // 3_600_000
43
+ time_in_ms -= hours * 3_600_000
44
+
45
+ minutes = time_in_ms // 60_000
46
+ time_in_ms -= minutes * 60_000
47
+
48
+ seconds = time_in_ms // 1_000
49
+ time_in_ms -= seconds * 1_000
50
+
51
+ return f"{hours}:{minutes:02d}:{seconds:02d},{time_in_ms:03d}"
52
+
53
+ def write_srt(segments,filename):
54
+ index=1
55
+ file1 = open(filename, "w") # append mode
56
+
57
+ for segment in segments:
58
+ file1.write( f"{index}\n"
59
+ f"{format_timestamp(segment.start)} --> "
60
+ f"{format_timestamp(segment.end)}\n"
61
+ f"{segment.text.strip().replace('-->', '->')}\n\n",)
62
+ index+=1
63
+
64
+
65
+
66
+ #############
67
+ #PAGE SET UP
68
+ #############
69
+
70
+ st.set_page_config(page_title="CaptionsCraft",
71
+ page_icon=":pen:",
72
+ layout="wide",
73
+ initial_sidebar_state="expanded"
74
+ )
75
+
76
+
77
+ #########
78
+ #SIDEBAR
79
+ ########
80
+
81
+ st.sidebar.header('Navigate to:')
82
+ nav = st.sidebar.radio('',['Go to homepage', 'Generate subtitles'])
83
+ st.sidebar.write('')
84
+ st.sidebar.write('')
85
+ st.sidebar.write('')
86
+ st.sidebar.write('')
87
+ st.sidebar.write('')
88
+
89
+
90
+ #HOME
91
+ #####
92
+
93
+ if nav == 'Go to homepage':
94
+
95
+ st.markdown("<h1 style='text-align: center; color: white; font-size:28px;'>CaptionCraft</h1>", unsafe_allow_html=True)
96
+ st.markdown("<h3 style='text-align: center; font-size:56px;'<p>&#127916;&#9998;</p></h3>", unsafe_allow_html=True)
97
+ st.markdown("<h3 style='text-align: center; color: grey; font-size:20px;'>Utilizing advanced Whisper AI, it effortlessly converts any language spoken in a video into accurate English subtitles. Bridging communication gaps seamlessly. </h3>", unsafe_allow_html=True)
98
+
99
+ st.markdown('___')
100
+
101
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:18px;'><b>What is this App about?<b></h3>", unsafe_allow_html=True)
102
+ st.write("""This app harnesses the cutting-edge power of the Whisper model to provide you with an unparalleled video subtitle generation experience.
103
+
104
+ \n\nImagine watching a video in a language you don't understand, but with our app, you won't miss a single detail. Whether it's a captivating foreign short film, an informative documentary, or a heartwarming vlog, our app steps in to bridge the linguistic gap.
105
+
106
+ \n\nPowered by Whisper AI, our app listens to the spoken words in the video and expertly converts them into accurate and contextually relevant English subtitles. It's like having your own personal interpreter working in real-time, enabling you to enjoy content from around the world without missing out on any crucial information.""")
107
+
108
+ st.markdown("<h3 style='text-align: left; color:#F63366; font-size:18px;'><b>How to use the app?<b></h3>", unsafe_allow_html=True)
109
+ st.write("""1) Navigate to the 'Generate subtitles' page using navigation bar on the left , and upload the video file.
110
+ \n\n 2) Choose the whisper model size \n\n 3) Upload your file (limit is 500 mb) \n\n 4) Your subtitles.txt file will be downloaded
111
+ \n\n 5) Using the file , subtitles can be imposed on any video using any standard video player application.""")
112
+ st.write("Here is the repo link : [GitHub] (https://github.com/dlopezyse/Synthia)")
113
+
114
+
115
+ if nav == 'Generate subtitles':
116
+ filename="videofile"
117
+
118
+ print("hello")
119
+ st.write("Choose a model size from the following: ")
120
+
121
+ model_size= st.radio("",["no model selected" , "tiny","base","small","medium","large-v2"] , index=0)
122
+
123
+ st.write("")
124
+ st.write("")
125
+ uploaded_file = st.file_uploader("Upload your file here...")
126
+
127
+
128
+ if model_size=="no model selected":
129
+ st.write("Select a model size to continue")
130
+
131
+ else:
132
+ # or run on CPU with INT8
133
+ model = WhisperModel(model_size, device="cpu", compute_type="int8")
134
+
135
+ if uploaded_file:
136
+ save_uploadedfile(uploaded_file)
137
+ print('file saved')
138
+
139
+ input_video = filename
140
+ audio_file = video2mp3(input_video)
141
+
142
+ result = translate(audio_file,model)
143
+
144
+ print('audio translated')
145
+ subtitle_filename='subtitles.txt'
146
+ write_srt(result,subtitle_filename)
147
+
148
+ print('subtitle generated')
149
+
150
+ with open(subtitle_filename, "rb") as file:
151
+ btn = st.download_button(
152
+
153
+ label="Download file",
154
+ data=file,
155
+ file_name="subtitles.txt"
156
+ )
157
+
158
+ st.stop()