Mpavan45 commited on
Commit
0e0d262
·
verified ·
1 Parent(s): 7b776b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -151
app.py CHANGED
@@ -1,157 +1,89 @@
1
- # import streamlit as st
2
- # import whisper
3
- # import ffmpeg
4
- # import pandas as pd
5
- # import pickle
6
- # import os
7
- # from chromadb.utils import embedding_functions
8
- # from chromadb import Client
9
- # from chromadb.config import Settings
10
-
11
- # def extract_audio(video_file):
12
- # """Extracts audio using ffmpeg."""
13
- # audio_path = "temp_audio.wav"
14
- # ffmpeg.input(video_file).output(audio_path).run(overwrite_output=True)
15
- # return audio_path
16
-
17
- # def transcribe_audio(audio_path):
18
- # """Transcribes audio to text using Whisper."""
19
- # model = whisper.load_model("base")
20
- # result = model.transcribe(audio_path)
21
- # return result['text']
22
-
23
- # def load_embeddings():
24
- # """Loads subtitle embeddings from pkl file."""
25
- # with open('subtitle_embeddings.pkl', 'rb') as f:
26
- # embeddings = pickle.load(f)
27
- # return embeddings
28
-
29
- # def save_to_chroma(embeddings):
30
- # """Stores embeddings in Chroma DB."""
31
- # client = Client(Settings())
32
- # collection = client.create_collection(name="subtitles")
33
- # for idx, row in embeddings.iterrows():
34
- # collection.add(
35
- # documents=[row['subtitle']],
36
- # ids=[str(idx)],
37
- # embeddings=[row['embedding']]
38
- # )
39
- # return collection
40
-
41
- # def search_subtitles(query, collection):
42
- # """Searches for subtitles in Chroma DB."""
43
- # results = collection.query(query_texts=[query], n_results=5)
44
- # return results['documents']
45
-
46
- # def main():
47
- # st.set_page_config(page_title="Video Subtitle Generator", layout="wide")
48
- # st.title("🎥 Video Subtitle Generator")
49
-
50
- # with st.sidebar:
51
- # uploaded_file = st.file_uploader("Upload Video", type=["mp4", "mkv"])
52
- # query = st.text_input("Search Subtitles")
53
- # download_btn = st.button("Download Subtitles")
54
-
55
- # if uploaded_file:
56
- # with st.spinner("Extracting audio..."):
57
- # audio_path = extract_audio(uploaded_file.name)
58
-
59
- # with st.spinner("Generating subtitles..."):
60
- # subtitles = transcribe_audio(audio_path)
61
- # st.success("Subtitles Generated!")
62
-
63
- # # Display the video and subtitles
64
- # st.video(uploaded_file)
65
- # st.text_area("Generated Subtitles", subtitles, height=300)
66
-
67
- # # Load and search embeddings
68
- # embeddings = load_embeddings()
69
- # collection = save_to_chroma(embeddings)
70
-
71
- # if query:
72
- # results = search_subtitles(query, collection)
73
- # st.write("### Matching Subtitles:")
74
- # for sub in results:
75
- # st.write(f"- {sub}")
76
-
77
- # if download_btn:
78
- # with open("generated_subtitles.srt", "w") as f:
79
- # f.write(subtitles)
80
- # st.download_button("Download SRT", "generated_subtitles.srt")
81
-
82
- # if __name__ == '__main__':
83
- # main()
84
  import streamlit as st
85
  import whisper
86
  import ffmpeg
87
  import pandas as pd
88
  import pickle
89
  import os
90
- from chromadb.utils import embedding_functions
 
91
  from chromadb import Client
92
  from chromadb.config import Settings
93
 
 
 
94
  def extract_audio(uploaded_file):
95
- """Extracts audio from video or handles audio file directly."""
96
  audio_path = "temp_audio.wav"
97
-
98
- # Save uploaded file temporarily
99
  temp_file = f"temp_{uploaded_file.name}"
100
  with open(temp_file, "wb") as f:
101
  f.write(uploaded_file.getvalue())
102
 
103
- # Extract audio for video files, keep as-is for audio
104
- if uploaded_file.name.endswith(('.mp4', '.mkv')):
105
- ffmpeg.input(temp_file).output(audio_path).run(overwrite_output=True)
106
- else:
107
- audio_path = temp_file
108
-
109
- return audio_path, temp_file
 
 
110
 
111
  def transcribe_audio(audio_path):
112
- """Transcribes audio to text using Whisper."""
113
  try:
114
  model = whisper.load_model("base")
115
- except Exception:
116
- st.warning("Downloading Whisper model. This may take a while...")
117
- model = whisper.load_model("base")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- result = model.transcribe(audio_path)
120
- return result['text']
121
 
122
- def load_embeddings():
123
- """Loads subtitle embeddings from pkl file."""
124
- if os.path.exists('subtitle_embeddings.pkl'):
125
- with open('subtitle_embeddings.pkl', 'rb') as f:
126
- embeddings = pickle.load(f)
127
- return embeddings
128
- else:
129
- st.error("No embeddings file found.")
130
- return pd.DataFrame()
131
 
132
  def save_to_chroma(embeddings):
133
- """Stores embeddings in Chroma DB."""
134
  client = Client(Settings())
135
-
136
- # Check if collection exists, else create
137
- try:
138
- collection = client.get_collection("subtitles")
139
- except:
140
- collection = client.create_collection(name="subtitles")
141
-
142
  for idx, row in embeddings.iterrows():
143
  collection.add(
144
  documents=[row['subtitle']],
145
  ids=[str(idx)],
146
  embeddings=[row['embedding']]
147
  )
148
-
149
  return collection
150
 
151
  def search_subtitles(query, collection):
152
- """Searches for subtitles in Chroma DB."""
153
- results = collection.query(query_texts=[query], n_results=5)
154
- return results['documents']
 
 
 
155
 
156
  def main():
157
  st.set_page_config(page_title="Video/Audio Subtitle Generator", layout="wide")
@@ -166,43 +98,43 @@ def main():
166
  with st.spinner("Extracting audio..."):
167
  audio_path, temp_file = extract_audio(uploaded_file)
168
 
169
- with st.spinner("Generating subtitles..."):
170
- subtitles = transcribe_audio(audio_path)
171
- st.success("Subtitles Generated!")
 
172
 
173
- # Display the media and subtitles
174
- if uploaded_file.name.endswith(('.mp4', '.mkv')):
175
- st.video(uploaded_file)
176
- else:
177
- st.audio(uploaded_file)
178
 
179
- st.text_area("Generated Subtitles", subtitles, height=300)
 
 
180
 
181
- # Load and search embeddings
182
- embeddings = load_embeddings()
183
- if not embeddings.empty:
184
- collection = save_to_chroma(embeddings)
 
 
 
185
 
186
  if query:
187
  results = search_subtitles(query, collection)
188
  st.write("### Matching Subtitles:")
189
- for sub in results:
190
- st.write(f"- {sub}")
191
-
192
- # Subtitle download option
193
- if download_btn:
194
- srt_content = f"1\n00:00:00,000 --> 00:00:10,000\n{subtitles}\n"
195
-
196
- st.download_button(
197
- label="Download SRT",
198
- data=srt_content.encode('utf-8'),
199
- file_name="generated_subtitles.srt",
200
- mime="text/plain"
201
- )
202
-
203
- # Cleanup temporary files
204
- os.remove(audio_path)
205
- os.remove(temp_file)
206
 
207
  if __name__ == '__main__':
208
  main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import whisper
3
  import ffmpeg
4
  import pandas as pd
5
  import pickle
6
  import os
7
+ import numpy as np
8
+ from sentence_transformers import SentenceTransformer
9
  from chromadb import Client
10
  from chromadb.config import Settings
11
 
12
+ embed_model = SentenceTransformer('all-MiniLM-L6-v2')
13
+
14
  def extract_audio(uploaded_file):
 
15
  audio_path = "temp_audio.wav"
 
 
16
  temp_file = f"temp_{uploaded_file.name}"
17
  with open(temp_file, "wb") as f:
18
  f.write(uploaded_file.getvalue())
19
 
20
+ try:
21
+ if uploaded_file.name.endswith(('.mp4', '.mkv')):
22
+ ffmpeg.input(temp_file).output(audio_path).run(overwrite_output=True)
23
+ else:
24
+ audio_path = temp_file
25
+ return audio_path, temp_file
26
+ except Exception as e:
27
+ st.error(f"Error extracting audio: {str(e)}")
28
+ return None, None
29
 
30
  def transcribe_audio(audio_path):
 
31
  try:
32
  model = whisper.load_model("base")
33
+ result = model.transcribe(audio_path)
34
+
35
+ subtitles = []
36
+ for i, segment in enumerate(result['segments']):
37
+ start_time = format_timestamp(segment['start'])
38
+ end_time = format_timestamp(segment['end'])
39
+ text = segment['text']
40
+ subtitles.append(f"{i + 1}\n{start_time} --> {end_time}\n{text}\n")
41
+
42
+ return subtitles
43
+ except Exception as e:
44
+ st.error(f"Error during transcription: {str(e)}")
45
+ return []
46
+
47
+ def format_timestamp(seconds):
48
+ hours = int(seconds // 3600)
49
+ minutes = int((seconds % 3600) // 60)
50
+ secs = int(seconds % 60)
51
+ millis = int((seconds % 1) * 1000)
52
+ return f"{hours:02}:{minutes:02}:{secs:02},{millis:03}"
53
+
54
+ def embed_subtitles(subtitles):
55
+ raw_texts = [line.split('\n')[2] for line in subtitles if line.strip()]
56
+ embeddings = embed_model.encode(raw_texts)
57
+
58
+ df = pd.DataFrame({
59
+ 'subtitle': raw_texts,
60
+ 'embedding': list(embeddings)
61
+ })
62
 
63
+ with open('subtitle_embeddings.pkl', 'wb') as f:
64
+ pickle.dump(df, f)
65
 
66
+ return df
 
 
 
 
 
 
 
 
67
 
68
  def save_to_chroma(embeddings):
 
69
  client = Client(Settings())
70
+ collection = client.create_collection(name="subtitles")
71
+
 
 
 
 
 
72
  for idx, row in embeddings.iterrows():
73
  collection.add(
74
  documents=[row['subtitle']],
75
  ids=[str(idx)],
76
  embeddings=[row['embedding']]
77
  )
 
78
  return collection
79
 
80
  def search_subtitles(query, collection):
81
+ try:
82
+ results = collection.query(query_texts=[query], n_results=5)
83
+ return results['documents']
84
+ except Exception as e:
85
+ st.error(f"Error searching subtitles: {str(e)}")
86
+ return []
87
 
88
  def main():
89
  st.set_page_config(page_title="Video/Audio Subtitle Generator", layout="wide")
 
98
  with st.spinner("Extracting audio..."):
99
  audio_path, temp_file = extract_audio(uploaded_file)
100
 
101
+ if audio_path:
102
+ with st.spinner("Generating subtitles..."):
103
+ subtitles = transcribe_audio(audio_path)
104
+ st.success("Subtitles Generated!")
105
 
106
+ if uploaded_file.name.endswith(('.mp4', '.mkv')):
107
+ st.video(uploaded_file)
108
+ else:
109
+ st.audio(uploaded_file)
 
110
 
111
+ st.write("### Generated Subtitles:")
112
+ for sub in subtitles:
113
+ st.text(sub)
114
 
115
+ with st.spinner("Embedding and storing subtitles..."):
116
+ embeddings = embed_subtitles(subtitles)
117
+
118
+ if embeddings.empty:
119
+ st.warning("No subtitles generated.")
120
+ else:
121
+ collection = save_to_chroma(embeddings)
122
 
123
  if query:
124
  results = search_subtitles(query, collection)
125
  st.write("### Matching Subtitles:")
126
+ if results:
127
+ for idx, sub in enumerate(results, start=1):
128
+ st.write(f"{idx}. {sub}")
129
+ else:
130
+ st.warning("No matching subtitles found.")
131
+
132
+ if download_btn:
133
+ with open("generated_subtitles.srt", "w") as f:
134
+ f.writelines(subtitles)
135
+
136
+ with open("generated_subtitles.srt", "rb") as f:
137
+ st.download_button("Download SRT", f, file_name="generated_subtitles.srt", mime="text/plain")
 
 
 
 
 
138
 
139
  if __name__ == '__main__':
140
  main()