Spaces:
Running
Running
add compare checkboxes
Browse files
app.py
CHANGED
@@ -52,14 +52,27 @@ def sort_dict_by_value(d: dict, ascending: bool=True):
|
|
52 |
"""
|
53 |
return sorted(d.items(), key=lambda x: x[1], reverse=not ascending)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# initialize connection to pinecone (get API key at app.pinecone.io)
|
56 |
pinecone.init(
|
57 |
api_key=PINECONE_API_KEY,
|
58 |
environment="us-central1-gcp" # may be different, check at app.pinecone.io
|
59 |
)
|
60 |
index = pinecone.Index(INDEX_NAME)
|
61 |
-
stats = test_pinecone()
|
62 |
-
print(f"Pinecone DB stats: {stats}")
|
63 |
|
64 |
### Main
|
65 |
# st.set_page_config(layout="centered")
|
@@ -70,16 +83,23 @@ css='''
|
|
70 |
'''
|
71 |
st.markdown(css, unsafe_allow_html=True)
|
72 |
|
|
|
|
|
|
|
73 |
# Create a text input field
|
74 |
query = st.text_input("What are you looking for?")
|
75 |
|
76 |
# Create a button
|
77 |
if st.button('Submit'):
|
78 |
|
|
|
|
|
|
|
|
|
79 |
# ### call OpenAI text-embedding
|
80 |
res = openai.Embedding.create(model=EMBEDDING_MODEL, input=[query], api_key=OPENAI_API_KEY)
|
81 |
xq = res['data'][0]['embedding']
|
82 |
-
out = query_pinecone(xq, top_k=
|
83 |
|
84 |
### candidates
|
85 |
metadata = {match['metadata']['product_id']: match['metadata'] for match in out['matches']}
|
@@ -105,35 +125,64 @@ if st.button('Submit'):
|
|
105 |
for result in results
|
106 |
}
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
st.header("Results")
|
109 |
st.divider()
|
110 |
|
111 |
# display matched results
|
112 |
-
for pid, match_score in above_thr_sorted:
|
113 |
-
|
114 |
-
if pid not in results:
|
115 |
continue
|
116 |
|
117 |
-
|
|
|
118 |
col_icon, col_info, col_compare = st.columns([2, 6, 1])
|
119 |
|
120 |
with col_icon:
|
121 |
st.image(result["logo"])
|
122 |
|
123 |
with col_info:
|
|
|
124 |
st.markdown(f"""match score: { round(100 * match_score, 2) }
|
125 |
<br>
|
126 |
**{result['name']}**
|
127 |
<br>
|
128 |
-
_Asking Price:_ {
|
129 |
<br>
|
130 |
-
_Category:_ {
|
131 |
<br>
|
132 |
-
_Location:_ {
|
133 |
""", unsafe_allow_html=True)
|
134 |
|
135 |
st.markdown(f"""**_Description:_** {result['description'][:MAX_LENGTH_DESC]}...[more]({result['url']})
|
136 |
""")
|
137 |
|
138 |
with col_compare:
|
139 |
-
st.checkbox('compare', key=pid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
"""
|
53 |
return sorted(d.items(), key=lambda x: x[1], reverse=not ascending)
|
54 |
|
55 |
+
def init_session_state():
|
56 |
+
if 'display_results' not in st.session_state:
|
57 |
+
st.session_state['display_results'] = False
|
58 |
+
|
59 |
+
if 'count_checked' not in st.session_state:
|
60 |
+
st.session_state['count_checked'] = 0
|
61 |
+
|
62 |
+
def callback_count_checked():
|
63 |
+
st.session_state['count_checked'] = 0
|
64 |
+
for key in list(st.session_state.keys()):
|
65 |
+
if (key.split('__')[0] == 'cb_compare') and (st.session_state[key] == True):
|
66 |
+
st.session_state['count_checked'] += 1
|
67 |
+
|
68 |
# initialize connection to pinecone (get API key at app.pinecone.io)
|
69 |
pinecone.init(
|
70 |
api_key=PINECONE_API_KEY,
|
71 |
environment="us-central1-gcp" # may be different, check at app.pinecone.io
|
72 |
)
|
73 |
index = pinecone.Index(INDEX_NAME)
|
74 |
+
# stats = test_pinecone()
|
75 |
+
# print(f"Pinecone DB stats: {stats}")
|
76 |
|
77 |
### Main
|
78 |
# st.set_page_config(layout="centered")
|
|
|
83 |
'''
|
84 |
st.markdown(css, unsafe_allow_html=True)
|
85 |
|
86 |
+
# initialize state
|
87 |
+
init_session_state()
|
88 |
+
|
89 |
# Create a text input field
|
90 |
query = st.text_input("What are you looking for?")
|
91 |
|
92 |
# Create a button
|
93 |
if st.button('Submit'):
|
94 |
|
95 |
+
# initialize
|
96 |
+
st.session_state.clear()
|
97 |
+
init_session_state()
|
98 |
+
|
99 |
# ### call OpenAI text-embedding
|
100 |
res = openai.Embedding.create(model=EMBEDDING_MODEL, input=[query], api_key=OPENAI_API_KEY)
|
101 |
xq = res['data'][0]['embedding']
|
102 |
+
out = query_pinecone(xq, top_k=3, include_metadata=True)
|
103 |
|
104 |
### candidates
|
105 |
metadata = {match['metadata']['product_id']: match['metadata'] for match in out['matches']}
|
|
|
125 |
for result in results
|
126 |
}
|
127 |
|
128 |
+
### For test
|
129 |
+
# print(f"above_thr_sorted: {above_thr_sorted}")
|
130 |
+
# print(f"results: {results}")
|
131 |
+
# print(f"metadata: {metadata}")
|
132 |
+
|
133 |
+
# # TEST ONLY
|
134 |
+
# above_thr_sorted = [('2086773', 0.800059378), ('1951083', 0.797319531), ('1998714', 0.795623)]
|
135 |
+
# results = {'1951083': {'productid': '1951083', 'name': '2 for 1 Turn-key Business Opportunity in Lynnwood, Washington - BizBuySell', 'category': 'Other', 'alternatename': None, 'url': 'https://www.bizbuysell.com/Business-Opportunity/2-for-1-Turn-key-Business-Opportunity/1951083/', 'logo': 'https://images.bizbuysell.com/shared/listings/195/1951083/87198e08-a191-4d97-a33b-9e9f40fa02f4-W768.jpg', 'description': 'Your chance to own a successful Korean traditional KBBQ grill restaurant and Korean dive bar. Owner is retiring after 19 years of business. This Korean BBQ restaurant utilizes a traditional grill called "Sot Ttu Kkeong" widely found in Korea. There are 10 separate grilling tables with a unique hood system to eliminate odors immediately. The bar next door may be able to extend hours into the summer. With one shared full kitchen, the new owner will be able to maximize business and potentially earn double income.'}, '1998714': {'productid': '1998714', 'name': 'Portland CPA Firm in Portland, Oregon - BizBuySell', 'category': 'Accounting and Tax Practices', 'alternatename': None, 'url': 'https://www.bizbuysell.com/Business-Opportunity/Portland-CPA-Firm/1998714/', 'logo': 'https://images.bizbuysell.com/shared/listings/199/1998714/cd02bdb9-32c9-409d-b82e-d0531c12eb39-W768.jpg', 'description': 'OR1002: UPDATED :The seller of this Portland CPA firm is approaching retirement and ready to sell the firm. The firm has a great reputation, has good systems in place, is paperless, and has a great staff. The mix of services offers a consistent stream of cash flow to the owner. The seller is seeking a CPA buyer. The office space is available for continued lease after the sale. Revenues for sale include:7% Accounting, bookkeeping and payroll services26% Income tax preparation services for individual clients35% Income tax preparation services for business and other clients28% Audits and reviews4% Consulting services'}, '2086773': {'productid': '2086773', 'name': 'Asian Grocery Supermarket, 1 owner for 29 years in Salem, Oregon - BizBuySell', 'category': 'Grocery Stores and Supermarkets', 'alternatename': None, 'url': 'https://www.bizbuysell.com/Business-Real-Estate-For-Sale/Asian-Grocery-Supermarket-1-owner-for-29-years/2086773/', 'logo': 'https://images.bizbuysell.com/shared/listings/208/2086773/861f6ba6-a994-4e90-9c62-0a593dae2a31-W768.jpg', 'description': 'Great location, well established and profitable supermarket.We have been the sole owner for almost 29 years, so business boasts of a great reputation.'}}
|
136 |
+
# metadata = {'2086773': {'asking_price': 1000000.0, 'asking_price_currency': 'USD', 'building_status': 'Established', 'category': 'Grocery Stores and Supermarkets', 'chunk_type': 'profile', 'city': 'Salem', 'document': '# Listing Profile\n \nAsking Price (USD): 1000000 \n\nReason for Selling: Retire ', 'listing_type': 'Retail', 'location': 'Salem, OR', 'main_category': 'Grocery Stores and Supermarkets', 'offer_type': 'Offer', 'offers__available_from__address__locality': 'Salem', 'offers__available_from__address__region': 'Oregon', 'offers__available_from__address__type': 'PostalAddress', 'offers__available_from__type': 'Place', 'product_id': '2086773', 'similar_pids': ['2074401', '2087795', '2068650'], 'state_code': 'OR'}, '1951083': {'asking_price': 200000.0, 'asking_price_currency': 'USD', 'category': 'Other', 'chunk_type': 'profile', 'city': 'Lynnwood', 'document': '# Listing Profile\n \nAsking Price (USD): 200000 \n\nReason for Selling: Retiring ', 'location': 'Lynnwood, WA', 'main_category': 'Other', 'offer_type': 'Offer', 'offers__available_from__address__locality': 'Lynnwood', 'offers__available_from__address__region': 'Washington', 'offers__available_from__address__type': 'PostalAddress', 'offers__available_from__type': 'Place', 'product_id': '1951083', 'similar_pids': ['2113741', '2033980', '2034855'], 'state_code': 'WA'}, '1998714': {'asking_price': 900000.0, 'asking_price_currency': 'USD', 'category': 'Accounting and Tax Practices', 'chunk_type': 'profile', 'city': 'Portland', 'document': '# Listing Profile\n \nAsking Price (USD): 900000 \n\nReason for Selling: Approaching retirement ', 'fin__gross_revenue': 958000.0, 'location': 'Portland, OR', 'main_category': 'Accounting and Tax Practices', 'offer_type': 'Offer', 'offers__available_from__address__locality': 'Portland', 'offers__available_from__address__region': 'Oregon', 'offers__available_from__address__type': 'PostalAddress', 'offers__available_from__type': 'Place', 'product_id': '1998714', 'similar_pids': ['2026155', '2066311'], 'state_code': 'OR'}}
|
137 |
+
|
138 |
+
# update
|
139 |
+
st.session_state['above_thr_sorted'] = above_thr_sorted
|
140 |
+
st.session_state['results'] = results
|
141 |
+
st.session_state['metadata'] = metadata
|
142 |
+
|
143 |
+
st.session_state['display_results'] = True
|
144 |
+
|
145 |
+
if st.session_state['display_results']:
|
146 |
+
|
147 |
st.header("Results")
|
148 |
st.divider()
|
149 |
|
150 |
# display matched results
|
151 |
+
for pid, match_score in st.session_state['above_thr_sorted']:
|
152 |
+
|
153 |
+
if pid not in st.session_state['results']:
|
154 |
continue
|
155 |
|
156 |
+
metadata_pid = st.session_state['metadata'][pid]
|
157 |
+
result = st.session_state['results'][pid]
|
158 |
col_icon, col_info, col_compare = st.columns([2, 6, 1])
|
159 |
|
160 |
with col_icon:
|
161 |
st.image(result["logo"])
|
162 |
|
163 |
with col_info:
|
164 |
+
# TODO: make asking price display $xxx,xxx
|
165 |
st.markdown(f"""match score: { round(100 * match_score, 2) }
|
166 |
<br>
|
167 |
**{result['name']}**
|
168 |
<br>
|
169 |
+
_Asking Price:_ {metadata_pid.get('asking_price', 'N/A')}
|
170 |
<br>
|
171 |
+
_Category:_ {metadata_pid.get('category', 'N/A')}
|
172 |
<br>
|
173 |
+
_Location:_ {metadata_pid.get('location', 'N/A')}
|
174 |
""", unsafe_allow_html=True)
|
175 |
|
176 |
st.markdown(f"""**_Description:_** {result['description'][:MAX_LENGTH_DESC]}...[more]({result['url']})
|
177 |
""")
|
178 |
|
179 |
with col_compare:
|
180 |
+
st.checkbox('compare', key=f"cb_compare__{pid}", on_change=callback_count_checked)
|
181 |
+
|
182 |
+
if st.session_state['count_checked'] > 0:
|
183 |
+
# display summary tab
|
184 |
+
print(f'display extra tab')
|
185 |
+
|
186 |
+
st.header("DEBUG")
|
187 |
+
st.divider()
|
188 |
+
st.json(st.session_state)
|