Spaces:
Running
Running
ASL
#1
by
Agrannya
- opened
.gitignore
CHANGED
@@ -1,4 +1,2 @@
|
|
1 |
.DS_Store
|
2 |
-
.env
|
3 |
-
__pycache__/*
|
4 |
-
gensim-data/*
|
|
|
1 |
.DS_Store
|
2 |
+
.env
|
|
|
|
.python-version
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
3.11.13
|
|
|
|
__pycache__/asl_gloss.cpython-311.pyc
ADDED
Binary file (14.3 kB). View file
|
|
__pycache__/document_parsing.cpython-311.pyc
ADDED
Binary file (15.2 kB). View file
|
|
__pycache__/document_parsing.cpython-313.pyc
ADDED
Binary file (10.6 kB). View file
|
|
__pycache__/vectorizer.cpython-311.pyc
ADDED
Binary file (7.07 kB). View file
|
|
vectorizer.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gensim
|
2 |
import gensim.downloader
|
3 |
-
from gensim.models import KeyedVectors
|
4 |
import numpy as np
|
5 |
import pandas as pd
|
6 |
import os
|
@@ -19,16 +18,8 @@ class Vectorizer:
|
|
19 |
"""
|
20 |
Returns a KeyedVector object loaded from gensim
|
21 |
"""
|
22 |
-
model_path = os.path.join(os.getcwd(), 'gensim-data', 'GoogleNews-vectors-negative300.bin.gz')
|
23 |
try:
|
24 |
-
print(f"Loading model from {model_path}")
|
25 |
-
kv = KeyedVectors.load_word2vec_format(model_path, binary=True)
|
26 |
-
print("Word2Vec model loaded successfully as KeyedVectors object.")
|
27 |
-
return kv
|
28 |
-
except FileNotFoundError:
|
29 |
-
print(f"Error: Model file not found at {model_path}. Trying to download...")
|
30 |
kv = gensim.downloader.load(model_name) # returns a keyedvector
|
31 |
-
print("Word2Vec model loaded successfully as KeyedVectors object.")
|
32 |
return kv
|
33 |
except Exception as e:
|
34 |
print(f"Unable to load embedding model from gensim: {e}")
|
@@ -52,24 +43,10 @@ class Vectorizer:
|
|
52 |
|
53 |
def encode(self, word):
|
54 |
print(f"encoding {word}")
|
55 |
-
if self.kv is None:
|
56 |
-
print("KeyedVectors not loaded")
|
57 |
-
return None
|
58 |
-
if word in self.kv.key_to_index:
|
59 |
return self.kv[word]
|
60 |
else:
|
61 |
print(f"Error: {word} is not in the KeyedVector's vocabulary")
|
62 |
-
# Try to find closest match
|
63 |
-
try:
|
64 |
-
closest_matches = self.kv.most_similar(word, topn=3)
|
65 |
-
if closest_matches:
|
66 |
-
closest_word = closest_matches[0][0]
|
67 |
-
print(f"Using closest match '{closest_word}' for '{word}'")
|
68 |
-
return self.kv[closest_word]
|
69 |
-
else:
|
70 |
-
print(f"No similar words found for '{word}'")
|
71 |
-
except Exception as e:
|
72 |
-
print(f"Error finding similar words: {e}")
|
73 |
return None
|
74 |
|
75 |
def encode_and_format(self, word):
|
@@ -84,11 +61,10 @@ class Vectorizer:
|
|
84 |
try:
|
85 |
await self.ensure_supabase_initialized()
|
86 |
query_embedding = self.encode(query)
|
87 |
-
|
88 |
if query_embedding is None:
|
89 |
return {
|
90 |
"match": False,
|
91 |
-
"error": f"'{query}' not in vocabulary
|
92 |
}
|
93 |
|
94 |
query_embedding = query_embedding.tolist()
|
@@ -154,11 +130,8 @@ def load_filtered_kv(model_name='word2vec-google-news-300', vocab=None):
|
|
154 |
async def main():
|
155 |
vectorizer = Vectorizer()
|
156 |
|
157 |
-
# Test exact word match
|
158 |
vector = vectorizer.encode("test")
|
159 |
print(vector)
|
160 |
-
|
161 |
-
# Test words not in vocabulary with closest match fallback
|
162 |
result = await vectorizer.vector_query_from_supabase("dog")
|
163 |
print(result)
|
164 |
result = await vectorizer.vector_query_from_supabase("cat")
|
|
|
1 |
import gensim
|
2 |
import gensim.downloader
|
|
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
import os
|
|
|
18 |
"""
|
19 |
Returns a KeyedVector object loaded from gensim
|
20 |
"""
|
|
|
21 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
kv = gensim.downloader.load(model_name) # returns a keyedvector
|
|
|
23 |
return kv
|
24 |
except Exception as e:
|
25 |
print(f"Unable to load embedding model from gensim: {e}")
|
|
|
43 |
|
44 |
def encode(self, word):
|
45 |
print(f"encoding {word}")
|
46 |
+
if self.kv is not None and word in self.kv.key_to_index:
|
|
|
|
|
|
|
47 |
return self.kv[word]
|
48 |
else:
|
49 |
print(f"Error: {word} is not in the KeyedVector's vocabulary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
return None
|
51 |
|
52 |
def encode_and_format(self, word):
|
|
|
61 |
try:
|
62 |
await self.ensure_supabase_initialized()
|
63 |
query_embedding = self.encode(query)
|
|
|
64 |
if query_embedding is None:
|
65 |
return {
|
66 |
"match": False,
|
67 |
+
"error": f"'{query}' not in vocabulary"
|
68 |
}
|
69 |
|
70 |
query_embedding = query_embedding.tolist()
|
|
|
130 |
async def main():
|
131 |
vectorizer = Vectorizer()
|
132 |
|
|
|
133 |
vector = vectorizer.encode("test")
|
134 |
print(vector)
|
|
|
|
|
135 |
result = await vectorizer.vector_query_from_supabase("dog")
|
136 |
print(result)
|
137 |
result = await vectorizer.vector_query_from_supabase("cat")
|