Spaces:
Runtime error
Runtime error
Commit
·
84b651f
1
Parent(s):
6cf1d09
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import json
|
|
4 |
import numpy as np
|
5 |
import pinecone
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
-
from concurrent.futures import ThreadPoolExecutor
|
8 |
import time
|
9 |
|
10 |
pinecone.init(api_key='f5112f8c-f27d-4af1-b427-0c0953c113b5', environment='asia-southeast1-gcp')
|
@@ -18,32 +17,39 @@ index = pinecone.Index('ingradientsearch')
|
|
18 |
def load_model():
|
19 |
return SentenceTransformer(r"finetiuned_model")
|
20 |
|
|
|
21 |
|
22 |
-
def
|
23 |
-
loaded_model = load_model()
|
24 |
processed_ingredient = process_string(ingredient)
|
25 |
-
|
26 |
-
result = index.query(xq, top_k=1, includeMetadata=True)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
return match['metadata']['Ingredient'], round(match['score'], 2)
|
31 |
-
return None, None
|
32 |
|
33 |
def get_top_matches(ingredients):
|
|
|
34 |
matches = []
|
35 |
scores = []
|
36 |
|
|
|
37 |
with ThreadPoolExecutor() as executor:
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
return matches, scores
|
46 |
|
|
|
47 |
def main():
|
48 |
st.set_page_config(page_title="Ingredients Matching App", page_icon=":smiley:", layout="wide")
|
49 |
st.title("Ingredients name matching App :smiley:")
|
@@ -73,4 +79,5 @@ def main():
|
|
73 |
st.write(f"Processing time: {end_time - start_time:.2f} seconds")
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
-
main()
|
|
|
|
4 |
import numpy as np
|
5 |
import pinecone
|
6 |
from sentence_transformers import SentenceTransformer
|
|
|
7 |
import time
|
8 |
|
9 |
pinecone.init(api_key='f5112f8c-f27d-4af1-b427-0c0953c113b5', environment='asia-southeast1-gcp')
|
|
|
17 |
def load_model():
|
18 |
return SentenceTransformer(r"finetiuned_model")
|
19 |
|
20 |
+
from concurrent.futures import ThreadPoolExecutor
|
21 |
|
22 |
+
def process_embedding(ingredient, model):
|
|
|
23 |
processed_ingredient = process_string(ingredient)
|
24 |
+
return model.encode([processed_ingredient]).tolist()
|
|
|
25 |
|
26 |
+
def pinecone_query(xq, index, top_k=1, includeMetadata=True):
|
27 |
+
return index.query(xq, top_k=top_k, includeMetadata=includeMetadata)
|
|
|
|
|
28 |
|
29 |
def get_top_matches(ingredients):
|
30 |
+
loaded_model = load_model()
|
31 |
matches = []
|
32 |
scores = []
|
33 |
|
34 |
+
# Generate embeddings in parallel
|
35 |
with ThreadPoolExecutor() as executor:
|
36 |
+
embeddings = list(executor.map(lambda ing: process_embedding(ing, loaded_model), ingredients))
|
37 |
|
38 |
+
# Query Pinecone in parallel
|
39 |
+
results = []
|
40 |
+
with ThreadPoolExecutor() as executor:
|
41 |
+
results = list(executor.map(lambda xq: pinecone_query(xq, index), embeddings))
|
42 |
+
|
43 |
+
# Extract matches and scores
|
44 |
+
for result in results:
|
45 |
+
if result['matches']:
|
46 |
+
match = result['matches'][0]
|
47 |
+
matches.append(match['metadata']['Ingredient'])
|
48 |
+
scores.append(round(match['score'], 2))
|
49 |
|
50 |
return matches, scores
|
51 |
|
52 |
+
|
53 |
def main():
|
54 |
st.set_page_config(page_title="Ingredients Matching App", page_icon=":smiley:", layout="wide")
|
55 |
st.title("Ingredients name matching App :smiley:")
|
|
|
79 |
st.write(f"Processing time: {end_time - start_time:.2f} seconds")
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
+
main()
|
83 |
+
|