amoldwalunj commited on
Commit
c50d017
·
1 Parent(s): bfbf1d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -35
app.py CHANGED
@@ -2,61 +2,70 @@ import streamlit as st
2
  import pandas as pd
3
  import json
4
  import numpy as np
5
- #from fuzzywuzzy import fuzz
6
-
7
  import pinecone
8
  from sentence_transformers import SentenceTransformer
 
9
 
10
  pinecone.init(api_key='f5112f8c-f27d-4af1-b427-0c0953c113b5', environment='asia-southeast1-gcp')
11
 
12
- #model = SentenceTransformer('all-mpnet-base-v2',device='cpu')
13
-
14
- #
15
-
16
  def process_string(s):
17
  return s.lower().replace('&', 'and')
18
 
 
19
 
 
 
 
20
 
21
- index = pinecone.Index('ingradientsearch')
22
 
 
 
 
 
 
23
 
 
 
 
 
24
 
 
 
 
25
 
26
- #@st.cache(allow_output_mutation=True)
27
- @st.cache_data
28
- def load_model():
29
- return SentenceTransformer(r"finetiuned_model")
 
 
 
 
 
30
 
31
  def main():
32
  st.set_page_config(page_title="Ingredients Matching App", page_icon=":smiley:", layout="wide")
33
  st.title("Ingredients name matching App :smiley:")
34
 
35
  st.header("Matches using embeddings (semantic search)")
36
- st.write("Enter an ingredient name:")
37
- st.write("e.g. Chicken")
38
- input_string = st.text_input("")
39
-
40
- input_string = process_string(input_string)
41
-
42
- if st.button("Enter"):
43
- st.write("Top 5 matches using semantic search:")
44
-
45
- loaded_model = load_model() # Load the model using the cached function
46
- xq = loaded_model.encode([input_string]).tolist()
47
- result = index.query(xq, top_k=5, includeMetadata=True)
48
-
49
- Ingredient=[]
50
- #Group=[]
51
- score=[]
52
- for matches in result['matches']:
53
- Ingredient.append(matches['metadata']['Ingredient'])
54
- score.append(matches['score'])
55
-
56
- final_result= pd.DataFrame(list(zip(Ingredient, score)),
57
- columns =['Ingredient','score' ])
58
-
59
- st.dataframe(final_result)
60
 
61
  if __name__ == "__main__":
62
  main()
 
2
  import pandas as pd
3
  import json
4
  import numpy as np
 
 
5
  import pinecone
6
  from sentence_transformers import SentenceTransformer
7
+ from concurrent.futures import ThreadPoolExecutor
8
 
9
  pinecone.init(api_key='f5112f8c-f27d-4af1-b427-0c0953c113b5', environment='asia-southeast1-gcp')
10
 
 
 
 
 
11
  def process_string(s):
12
  return s.lower().replace('&', 'and')
13
 
14
+ index = pinecone.Index('ingradientsearch')
15
 
16
+ @st.cache_data
17
+ def load_model():
18
+ return SentenceTransformer(r"finetiuned_model")
19
 
 
20
 
21
+ def get_match_score(ingredient):
22
+ loaded_model = load_model()
23
+ processed_ingredient = process_string(ingredient)
24
+ xq = loaded_model.encode([processed_ingredient]).tolist()
25
+ result = index.query(xq, top_k=1, includeMetadata=True)
26
 
27
+ if result['matches']:
28
+ match = result['matches'][0]
29
+ return match['metadata']['Ingredient'], round(match['score'], 2)
30
+ return None, None
31
 
32
+ def get_top_matches(ingredients):
33
+ matches = []
34
+ scores = []
35
 
36
+ with ThreadPoolExecutor() as executor:
37
+ results = list(executor.map(get_match_score, ingredients))
38
+
39
+ for match, score in results:
40
+ if match is not None and score is not None:
41
+ matches.append(match)
42
+ scores.append(score)
43
+
44
+ return matches, scores
45
 
46
  def main():
47
  st.set_page_config(page_title="Ingredients Matching App", page_icon=":smiley:", layout="wide")
48
  st.title("Ingredients name matching App :smiley:")
49
 
50
  st.header("Matches using embeddings (semantic search)")
51
+ st.write("Enter the JSON input:")
52
+ json_input = st.text_area("")
53
+
54
+ if st.button("Process"):
55
+ with st.spinner("Processing..."):
56
+ try:
57
+ input_data = json.loads(json_input)
58
+ for menu_item in input_data:
59
+ ingredients = menu_item.get("ingredients", [])
60
+ matches, scores = get_top_matches(ingredients)
61
+ menu_item["Ingradients_matched"] = matches
62
+ menu_item["scores"] = scores
63
+
64
+ st.write("Processed JSON:")
65
+ #st.write(json.dumps(input_data, indent=2))
66
+ st.write("<pre>" + json.dumps(input_data, indent=4) + "</pre>", unsafe_allow_html=True)
67
+ except json.JSONDecodeError:
68
+ st.error("Invalid JSON input. Please check and try again.")
 
 
 
 
 
 
69
 
70
  if __name__ == "__main__":
71
  main()