Phaneendrabayi commited on
Commit
9d5d079
·
verified ·
1 Parent(s): cc7a7ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -6,7 +6,7 @@ import os
6
  import pickle
7
  import chromadb
8
  from pydub import AudioSegment
9
- from sentence_transformers import SentenceTransformer
10
 
11
  # Initialize ChromaDB
12
  chroma_client = chromadb.PersistentClient(path="./chroma_db")
@@ -68,19 +68,15 @@ def transcribe_audio(audio_file):
68
  plain_subtitles.append(text)
69
  return "\n".join(subtitles), " ".join(plain_subtitles)
70
 
71
- # Perform Search Using ChromaDB and Pickle Data
72
- def search_with_chromadb(query):
73
- query_embedding = embedder.encode([query]).tolist()
74
- results = collection.query(query_embeddings=query_embedding, n_results=5)
75
- results_from_chroma = results['documents'][0] if results['documents'] else []
76
-
77
- results_from_pickle = []
78
- for item in subtitle_data:
79
- if isinstance(item, dict) and 'text' in item and query.lower() in item['text'].lower():
80
- results_from_pickle.append(item['text'])
81
-
82
- combined_results = results_from_chroma[:5] + results_from_pickle[:5]
83
- return combined_results[:5]
84
 
85
  # UI Layout with Tabs
86
  tab1, tab2 = st.tabs(["Upload & Generate Subtitles", "Search Subtitles"])
@@ -113,9 +109,9 @@ with tab1:
113
 
114
  with tab2:
115
  st.subheader("Search Subtitles")
116
- search_word = st.text_input("Search for a word using ChromaDB")
117
  if search_word.strip():
118
- top_results = search_with_chromadb(search_word)
119
 
120
  st.write("### Top 5 Search Results from Transcribed Text & Pickle Data")
121
  if not top_results:
 
6
  import pickle
7
  import chromadb
8
  from pydub import AudioSegment
9
+ from sentence_transformers import SentenceTransformer, util
10
 
11
  # Initialize ChromaDB
12
  chroma_client = chromadb.PersistentClient(path="./chroma_db")
 
68
  plain_subtitles.append(text)
69
  return "\n".join(subtitles), " ".join(plain_subtitles)
70
 
71
+ # Perform Word Search with Embeddings Using Cosine Similarity
72
+ def search_word_with_embeddings(word, transcribed_text):
73
+ search_embedding = embedder.encode(word, convert_to_tensor=True)
74
+ data_texts = [str(item.get('text', '')) if isinstance(item, dict) else str(item) for item in subtitle_data]
75
+ text_embeddings = embedder.encode([transcribed_text] + data_texts, convert_to_tensor=True)
76
+ cosine_scores = util.pytorch_cos_sim(search_embedding, text_embeddings)[0]
77
+ top_indices = np.argsort(cosine_scores.cpu().numpy())[::-1][:5]
78
+ top_results = [data_texts[i] for i in top_indices]
79
+ return top_results
 
 
 
 
80
 
81
  # UI Layout with Tabs
82
  tab1, tab2 = st.tabs(["Upload & Generate Subtitles", "Search Subtitles"])
 
109
 
110
  with tab2:
111
  st.subheader("Search Subtitles")
112
+ search_word = st.text_input("Search for a word using Embeddings and Cosine Similarity")
113
  if search_word.strip():
114
+ top_results = search_word_with_embeddings(search_word, transcribed_text)
115
 
116
  st.write("### Top 5 Search Results from Transcribed Text & Pickle Data")
117
  if not top_results: