tensorkelechi commited on
Commit
37655b3
·
verified ·
1 Parent(s): 14784b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import moonshine
2
+ import streamlit as st
3
+ import time, os
4
+ from pytube import YouTube
5
+ from pydub import AudioSegment
6
+
7
+
8
+ os.environ["KERAS_BACKEND"] = "jax"
9
+
10
+ st.set_page_config(page_title="vidkit_v2")
11
+
12
+ st.title("vidkit")
13
+ st.write("App for video/audio transcription(Youtube, mp4, mp3)")
14
+ st.write("[built to solve a personal problem, might be useful to others too :)]")
15
+
16
+
17
+ def youtube_video_downloader(url: str):
18
+
19
+ yt_vid = YouTube(url)
20
+
21
+ title = yt_vid.title
22
+ vid_dld = (
23
+ yt_vid.streams.filter(progressive=True, file_extension="mp4")
24
+ .order_by("resolution")
25
+ .desc()
26
+ .first()
27
+ )
28
+ vid_dld = vid_dld.download()
29
+
30
+ return vid_dld, title
31
+
32
+
33
+ def audio_extraction(video_file: str):
34
+ audio = AudioSegment.from_file(video_file, format="mp4")
35
+ audio_path = "audio.wav"
36
+ audio.export(audio_path, format="wav")
37
+
38
+ return audio_path
39
+
40
+
41
+ def audio_processing(mp3_audio):
42
+ audio = AudioSegment.from_file(mp3_audio, format="mp3")
43
+ wav_file = "audio_file.wav"
44
+ audio = audio.export(wav_file, format="wav")
45
+ return wav_file
46
+
47
+
48
+ def transcriber_pass(processed_audio):
49
+ stime = time.time()
50
+
51
+ # transcribe with moonshine
52
+ text_extract = moonshine.transcribe(processed_audio, "moonshine/tiny")
53
+
54
+ time_taken = time.time() - stime
55
+ st.write(f'transcribed in {time_taken}s')
56
+
57
+ return text_extract[0]
58
+
59
+
60
+ # Streamlit UI
61
+ youtube_url_tab, file_select_tab, audio_file_tab = st.tabs(
62
+ ["Youtube URL", "Video file", "Audio file"]
63
+ )
64
+
65
+ with youtube_url_tab:
66
+ url = st.text_input("Enter the Youtube url")
67
+
68
+ try:
69
+ yt_video, title = youtube_video_downloader(url)
70
+ if url:
71
+ if st.button("Transcribe", key="yturl"):
72
+ with st.spinner("Transcribing..."):
73
+ with st.spinner("Extracting audio..."):
74
+ audio = audio_extraction(yt_video)
75
+ ytvideo_transcript = transcriber_pass(audio)
76
+
77
+ st.write(f"Video title: {title}")
78
+ st.write("___")
79
+
80
+ st.markdown(
81
+ f"""
82
+ <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
83
+ <p> -> {ytvideo_transcript}</p>
84
+ </div>
85
+ """,
86
+ unsafe_allow_html=True,
87
+ )
88
+
89
+ except Exception as e:
90
+ st.error(e)
91
+
92
+
93
+ # Video file transcription
94
+
95
+ with file_select_tab:
96
+ uploaded_video_file = st.file_uploader("Upload video file", type="mp4")
97
+
98
+ try:
99
+ if uploaded_video_file:
100
+ if st.button("Transcribe", key="vidfile"):
101
+ with st.spinner("Transcribing..."):
102
+ with st.spinner("Extracting audio..."):
103
+ audio = audio_extraction(uploaded_video_file)
104
+
105
+ video_transcript = transcriber_pass(audio)
106
+ st.success(f"Transcription successful")
107
+ st.markdown(
108
+ f"""
109
+ <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
110
+ <p> -> {video_transcript}</p>
111
+ </div>
112
+ """,
113
+ unsafe_allow_html=True,
114
+ )
115
+
116
+ except Exception as e:
117
+ st.error(e)
118
+
119
+
120
+ # Audio file transcription
121
+ with audio_file_tab:
122
+ audio_file = st.file_uploader("Upload audio file", type="mp3")
123
+
124
+ try:
125
+ # ensure audio file is present
126
+ if audio_file:
127
+ if st.button("Transcribe", key="audiofile"):
128
+ with st.spinner("Transcribing..."):
129
+ processed_audio = audio_processing(audio_file) # extract audio/preprocess
130
+ audio_transcript = transcriber_pass(processed_audio)
131
+ st.success(f"Transcription successful")
132
+ st.markdown(
133
+ f"""
134
+ <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
135
+ <p> -> {audio_transcript}</p>
136
+ </div>
137
+ """,
138
+ unsafe_allow_html=True,
139
+ )
140
+
141
+ except Exception as e:
142
+ st.error(e)
143
+
144
+
145
+ # Footer
146
+ st.write("")
147
+ st.write("")
148
+ st.write("")
149
+
150
+ st.markdown(
151
+ """
152
+ <div style="text-align: center; padding: 1rem;">
153
+ Project by <a href="https://github.com/kelechi-c" target="_blank" style="color: white; font-weight: bold; text-decoration: none;">
154
+ tensor_kelechi</a>
155
+ </div>
156
+ """,
157
+ unsafe_allow_html=True,
158
+ )
159
+
160
+ # Arigato :)