Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +42 -31
src/streamlit_app.py
CHANGED
|
@@ -259,41 +259,52 @@ def get_similar_content(content, n_recommendations=5):
|
|
| 259 |
"accept": "application/json"
|
| 260 |
}
|
| 261 |
|
| 262 |
-
#
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
"
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
|
|
|
|
|
|
| 297 |
return []
|
| 298 |
except Exception as e:
|
| 299 |
st.error(f"Error fetching similar content: {str(e)}")
|
|
|
|
| 259 |
"accept": "application/json"
|
| 260 |
}
|
| 261 |
|
| 262 |
+
# URLs for both similarity tables
|
| 263 |
+
similarity_table_urls = [
|
| 264 |
+
"https://mtoft20-potm.hf.space/api/v1/db/data/noco/p9pozkcw81t9aee/mtsx4yp45gi12tm",
|
| 265 |
+
"https://mtoft20-potm.hf.space/api/v1/db/data/noco/p9pozkcw81t9aee/mca47f9wyoubouk"
|
| 266 |
+
]
|
| 267 |
|
| 268 |
+
similar_items = []
|
| 269 |
|
| 270 |
+
# Check both tables for similarities
|
| 271 |
+
for table_url in similarity_table_urls:
|
| 272 |
+
params = {
|
| 273 |
+
"where": f"(title,eq,\"{content['title']}\")"
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
response = requests.get(table_url, headers=headers, params=params)
|
| 277 |
+
|
| 278 |
+
if response.status_code == 200:
|
| 279 |
+
data = response.json()
|
| 280 |
+
if data and len(data.get('list', [])) > 0:
|
| 281 |
+
# Get similar items from stored data
|
| 282 |
+
table_items = json.loads(data['list'][0]['similar_items'])
|
| 283 |
+
similar_items.extend(table_items)
|
| 284 |
+
|
| 285 |
+
if similar_items:
|
| 286 |
+
# Sort by similarity score and limit to requested number
|
| 287 |
+
similar_items.sort(key=lambda x: x['similarity'], reverse=True)
|
| 288 |
+
similar_items = similar_items[:n_recommendations]
|
| 289 |
+
|
| 290 |
+
# Get full content details for each similar item
|
| 291 |
+
similar_content = []
|
| 292 |
+
for item in similar_items:
|
| 293 |
+
# Query main content table for full details
|
| 294 |
+
content_params = {
|
| 295 |
+
"where": f"(title,eq,\"{item['title']}\")"
|
| 296 |
+
}
|
| 297 |
+
content_response = requests.get(NOCODB_URL, headers=headers, params=content_params)
|
| 298 |
|
| 299 |
+
if content_response.status_code == 200:
|
| 300 |
+
content_data = content_response.json()
|
| 301 |
+
if content_data and len(content_data.get('list', [])) > 0:
|
| 302 |
+
content_dict = content_data['list'][0]
|
| 303 |
+
content_dict['similarity'] = f"{item['similarity']:.2%}"
|
| 304 |
+
similar_content.append(content_dict)
|
| 305 |
|
| 306 |
+
return similar_content
|
| 307 |
+
|
| 308 |
return []
|
| 309 |
except Exception as e:
|
| 310 |
st.error(f"Error fetching similar content: {str(e)}")
|