Upload 2 files
Browse files- app.py +162 -0
- corpus_embeddings.pickle +3 -0
app.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
from pymongo import MongoClient
|
4 |
+
import pandas as pd
|
5 |
+
from sentence_transformers import SentenceTransformer, util
|
6 |
+
import requests
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import matplotlib.image as mpimg
|
9 |
+
from io import BytesIO
|
10 |
+
import urllib.parse
|
11 |
+
import math
|
12 |
+
|
13 |
+
sbert_model = SentenceTransformer('paraphrase-multilingual-mpnet-base-v2')
|
14 |
+
|
15 |
+
try:
|
16 |
+
client = MongoClient('mongodb://192.168.1.103:27017/')
|
17 |
+
|
18 |
+
print("---Connenction Successful---")
|
19 |
+
|
20 |
+
Recommendation_elderly = client['Recommendation_elderly']
|
21 |
+
|
22 |
+
healthcare_articles = Recommendation_elderly['token']
|
23 |
+
|
24 |
+
except:
|
25 |
+
raise KeyError('Connection Fail')
|
26 |
+
|
27 |
+
data = healthcare_articles.find()
|
28 |
+
data = pd.DataFrame(list(data))
|
29 |
+
data = data.drop_duplicates(subset=['url'])
|
30 |
+
data = data[data['title'] != '']
|
31 |
+
data = data.reset_index().drop(columns=['index'])
|
32 |
+
data = data.reset_index().drop(columns=['_id','index'])
|
33 |
+
|
34 |
+
with open('corpus_embeddings.pickle', 'rb') as file:
|
35 |
+
corpus_embeddings = pickle.load(file)
|
36 |
+
|
37 |
+
def personal_check(age,weight,height,gender):
|
38 |
+
|
39 |
+
#age check
|
40 |
+
if age >= 60:
|
41 |
+
age = 'ผู้สูงอายุ'
|
42 |
+
else:
|
43 |
+
age = 'วัยทำงาน'
|
44 |
+
|
45 |
+
#gender check
|
46 |
+
if gender == 'Female':
|
47 |
+
gender = 'ผู้หญิง สตรี'
|
48 |
+
else:
|
49 |
+
gender = 'ผู้ชาย'
|
50 |
+
|
51 |
+
#bmi check
|
52 |
+
height_meters = height / 100
|
53 |
+
|
54 |
+
bmi = weight / (height_meters ** 2)
|
55 |
+
|
56 |
+
if bmi >= 30:
|
57 |
+
bmi = 'อ้วนมาก'
|
58 |
+
elif bmi >= 23 and bmi <30:
|
59 |
+
bmi = 'อ้วน'
|
60 |
+
elif bmi >= 18.5 and bmi <23:
|
61 |
+
bmi = ''
|
62 |
+
else:
|
63 |
+
bmi = 'ผอม'
|
64 |
+
|
65 |
+
return age,gender,bmi
|
66 |
+
|
67 |
+
def sbert_search(queries):
|
68 |
+
|
69 |
+
global sbert_model,corpus_embeddings,data
|
70 |
+
|
71 |
+
index_lst = []
|
72 |
+
score_lst = []
|
73 |
+
|
74 |
+
for query in queries:
|
75 |
+
query_embedding = sbert_model.encode(query, convert_to_tensor=True)
|
76 |
+
hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=15)
|
77 |
+
hits = hits[0]
|
78 |
+
for hit in hits:
|
79 |
+
index_lst.append(hit['corpus_id'])
|
80 |
+
score_lst.append(hit['score'])
|
81 |
+
|
82 |
+
sbert_searched = data.iloc[index_lst]
|
83 |
+
sbert_searched['score'] = score_lst
|
84 |
+
sbert_searched = sbert_searched[['url','title','score','banner']]
|
85 |
+
|
86 |
+
return sbert_searched
|
87 |
+
|
88 |
+
def visualize_articles_images(title,banner):
|
89 |
+
# Calculate the number of rows and columns for the grid
|
90 |
+
num_images = len(banner)
|
91 |
+
num_rows = math.ceil(num_images / 3)
|
92 |
+
num_cols = min(num_images, 3)
|
93 |
+
fp = 'angsana.ttc'
|
94 |
+
|
95 |
+
# Create a grid of subplots
|
96 |
+
fig, axs = plt.subplots(num_rows, num_cols, figsize=(20, 20))
|
97 |
+
|
98 |
+
# Iterate over the image URLs
|
99 |
+
for i, url in enumerate(banner):
|
100 |
+
# Calculate the subplot position
|
101 |
+
row = i // num_cols
|
102 |
+
col = i % num_cols
|
103 |
+
axs[row, col].set_title(title.iloc[i],fontname='Tahoma',fontsize=16)
|
104 |
+
|
105 |
+
if str(url) == 'nan':
|
106 |
+
continue
|
107 |
+
|
108 |
+
else:
|
109 |
+
try:
|
110 |
+
# Encode the URL using UTF-8
|
111 |
+
encoded_url = urllib.parse.quote(url, safe=':/')
|
112 |
+
|
113 |
+
# Download the image
|
114 |
+
response = requests.get(encoded_url)
|
115 |
+
img = mpimg.imread(BytesIO(response.content), format='jpg')
|
116 |
+
|
117 |
+
# Calculate the subplot position
|
118 |
+
row = i // num_cols
|
119 |
+
col = i % num_cols
|
120 |
+
|
121 |
+
# Plot the image
|
122 |
+
axs[row, col].imshow(img)
|
123 |
+
axs[row, col].axis('off')
|
124 |
+
except:
|
125 |
+
continue
|
126 |
+
finally:
|
127 |
+
pass
|
128 |
+
|
129 |
+
return fig
|
130 |
+
|
131 |
+
def main():
|
132 |
+
#header
|
133 |
+
st.title("---ระบบแนะนำบทความสุขภาพ---")
|
134 |
+
st.subheader("ให้คะแนนบทความหน่อยนะครับ:smile:")
|
135 |
+
|
136 |
+
#personal information input
|
137 |
+
age = st.slider("อายุ", 0, 100, 25)
|
138 |
+
weight = st.number_input("น้ำหนัก (Kg.): ",30,120,step=1,value=30)
|
139 |
+
height = st.number_input("ส่วนสูง (cm.): ",100,250,step=1,value=120)
|
140 |
+
gender = st.selectbox('เพศ',('ชาย', 'หญิง'))
|
141 |
+
food_allergy = st.selectbox('แพ้อาหาร?',('ไม่แพ้', 'แพ้อาหาร'))
|
142 |
+
drug_allergy = st.selectbox('แพ้ยา?',('ไม่แพ้', 'แพ้ยา'))
|
143 |
+
congentital_disease = st.text_input('โรคประจำตัวของคุณ')
|
144 |
+
|
145 |
+
# Add a button
|
146 |
+
if st.button("Click me"):
|
147 |
+
age,gender,bmi = personal_check(age,weight,height,gender)
|
148 |
+
|
149 |
+
if food_allergy == 'ไม่แพ้':
|
150 |
+
food_allergy = ''
|
151 |
+
if drug_allergy == 'ไม่แพ้':
|
152 |
+
drug_allergy = ''
|
153 |
+
|
154 |
+
queries = [gender+age+bmi+food_allergy+drug_allergy+congentital_disease]
|
155 |
+
|
156 |
+
sbert_searched = sbert_search(queries)
|
157 |
+
|
158 |
+
st.write(f"{queries}")
|
159 |
+
st.pyplot(visualize_articles_images(sbert_searched['title'],sbert_searched['banner']))
|
160 |
+
|
161 |
+
if __name__ == "__main__":
|
162 |
+
main()
|
corpus_embeddings.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:df1b3b8a5bbcb56feefb6fc9f10377ca717a161fa4a45358f66c3266f9c05e93
|
3 |
+
size 14656907
|