Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

appv2.py 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  1. import streamlit as st
  2. from PIL import Image
  3. from io import BytesIO
  4. import os
  5. import torch
  6. import pandas as pd
  7. import numpy as np
  8. from mlProject.utils.bertModel import preprocess_text
  9. import os
  10. from dotenv import load_dotenv
  11. from pinecone import Pinecone, ServerlessSpec
  12. import time
  13. from mlProject.utils.common import fetch_item_from_userquery
  14. load_dotenv()
  15. pkey = os.environ.get("PINE_CONE_API_KEY")
  16. #pc = Pinecone(api_key=pkey)
  17. #index = pc.Index('stv1-embeddings')
  18. pc = Pinecone(api_key=pkey)
  19. index = pc.Index("shopping-index")
  20. #sampled_data = pd.read_csv('artifacts/data_ingestion/data_tar_extracted/processed_dataset_target_data_with_captions_only.csv')
  21. device = 'cuda' if torch.cuda.is_available() else 'cpu'
  22. def generate_image_url(path):
  23. return f"/Users/user/Documents/MLProjects/project6/artifacts/data_ingestion/abo-images-small/images/resize/{path}"
  24. def generate_summary(path):
  25. return f"/Users/user/Documents/MLProjects/project6/artifacts/data_ingestion/abo-images-small/images/resize/{path}"
  26. def clear_form():
  27. st.session_state["product_name"] = ""
  28. st.session_state.pop('results', None)
  29. def ShopTalk():
  30. st.title("Product Search Engine")
  31. with st.form("Shop Talk"):
  32. # Use the session state to reset the input field more effectively
  33. if 'reset' not in st.session_state:
  34. st.session_state.reset = False
  35. if st.session_state.reset:
  36. query = st.text_input("Product Name:", value="", key="product_name")
  37. st.session_state.reset = False
  38. else:
  39. query = st.text_input("Product Name:", value="", key="product_name")
  40. search_clicked = st.form_submit_button(label="Search")
  41. clear_clicked = st.form_submit_button(label="Clear", on_click=clear_form)
  42. if search_clicked and query:
  43. st.session_state.results = fetch_item_from_userquery(query)
  44. #query_embedding = fetch_item_from_userquery(query)
  45. if 'results' in st.session_state and st.session_state.results:
  46. for image_url, caption in zip(st.session_state.results[0], st.session_state.results[1]):
  47. col1, col2 = st.columns([1, 3])
  48. with col1:
  49. try:
  50. if image_url:
  51. st.image(image_url, width=150)
  52. except Exception as e: # Catching a general exception to handle any error while loading the image
  53. st.error(f"Failed to load image: {e}")
  54. with col2:
  55. #formatted_caption = caption.replace(", ", "\n") # Replace commas with newline characters
  56. st.write(caption)
  57. if __name__ == "__main__":
  58. ShopTalk()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...