Mpavan45 commited on
Commit
25169b7
·
verified ·
1 Parent(s): 6e91fa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -8
app.py CHANGED
@@ -1,3 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import whisper
3
  import ffmpeg
@@ -6,11 +146,12 @@ 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}"
@@ -27,6 +168,7 @@ def extract_audio(uploaded_file):
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")
@@ -38,12 +180,13 @@ def transcribe_audio(audio_path):
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)
@@ -51,6 +194,7 @@ def format_timestamp(seconds):
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)
@@ -65,26 +209,30 @@ def embed_subtitles(subtitles):
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")
90
  st.title("🎥🎵 Video/Audio Subtitle Generator")
@@ -114,7 +262,7 @@ def main():
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:
@@ -137,4 +285,5 @@ def main():
137
  st.download_button("Download SRT", f, file_name="generated_subtitles.srt", mime="text/plain")
138
 
139
  if __name__ == '__main__':
140
- 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")
90
+ # st.title("🎥🎵 Video/Audio Subtitle Generator")
91
+
92
+ # with st.sidebar:
93
+ # uploaded_file = st.file_uploader("Upload Video/Audio", type=["mp4", "mkv", "mp3", "wav"])
94
+ # query = st.text_input("Search Subtitles")
95
+ # download_btn = st.button("Download Subtitles")
96
+
97
+ # if uploaded_file:
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()
141
  import streamlit as st
142
  import whisper
143
  import ffmpeg
 
146
  import os
147
  import numpy as np
148
  from sentence_transformers import SentenceTransformer
149
+ from chromadb import PersistentClient
 
150
 
151
+ # Initialize models
152
  embed_model = SentenceTransformer('all-MiniLM-L6-v2')
153
 
154
+ # Function to extract audio
155
  def extract_audio(uploaded_file):
156
  audio_path = "temp_audio.wav"
157
  temp_file = f"temp_{uploaded_file.name}"
 
168
  st.error(f"Error extracting audio: {str(e)}")
169
  return None, None
170
 
171
+ # Function to transcribe audio
172
  def transcribe_audio(audio_path):
173
  try:
174
  model = whisper.load_model("base")
 
180
  end_time = format_timestamp(segment['end'])
181
  text = segment['text']
182
  subtitles.append(f"{i + 1}\n{start_time} --> {end_time}\n{text}\n")
183
+
184
  return subtitles
185
  except Exception as e:
186
  st.error(f"Error during transcription: {str(e)}")
187
  return []
188
 
189
+ # Timestamp formatting
190
  def format_timestamp(seconds):
191
  hours = int(seconds // 3600)
192
  minutes = int((seconds % 3600) // 60)
 
194
  millis = int((seconds % 1) * 1000)
195
  return f"{hours:02}:{minutes:02}:{secs:02},{millis:03}"
196
 
197
+ # Embed subtitles
198
  def embed_subtitles(subtitles):
199
  raw_texts = [line.split('\n')[2] for line in subtitles if line.strip()]
200
  embeddings = embed_model.encode(raw_texts)
 
209
 
210
  return df
211
 
212
+ # Save embeddings to ChromaDB
213
  def save_to_chroma(embeddings):
214
+ client = PersistentClient(path="./chroma_db")
215
  collection = client.create_collection(name="subtitles")
216
 
217
  for idx, row in embeddings.iterrows():
218
  collection.add(
219
  documents=[row['subtitle']],
220
  ids=[str(idx)],
221
+ embeddings=[row['embedding'].tolist()] # Convert to list
222
  )
223
  return collection
224
 
225
+ # Search subtitles
226
  def search_subtitles(query, collection):
227
  try:
228
+ query_embedding = embed_model.encode([query]).tolist()
229
+ results = collection.query(query_embeddings=query_embedding, n_results=5)
230
  return results['documents']
231
  except Exception as e:
232
  st.error(f"Error searching subtitles: {str(e)}")
233
  return []
234
 
235
+ # Main app
236
  def main():
237
  st.set_page_config(page_title="Video/Audio Subtitle Generator", layout="wide")
238
  st.title("🎥🎵 Video/Audio Subtitle Generator")
 
262
 
263
  with st.spinner("Embedding and storing subtitles..."):
264
  embeddings = embed_subtitles(subtitles)
265
+
266
  if embeddings.empty:
267
  st.warning("No subtitles generated.")
268
  else:
 
285
  st.download_button("Download SRT", f, file_name="generated_subtitles.srt", mime="text/plain")
286
 
287
  if __name__ == '__main__':
288
+ main()
289
+