Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -51,6 +51,36 @@ def setup_spell_checker():
|
|
51 |
term_index=0, count_index=1)
|
52 |
return sym_spell
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# ----------------------
|
55 |
# Streamlit App
|
56 |
# ----------------------
|
@@ -62,13 +92,14 @@ def main():
|
|
62 |
model, faiss_index = setup_faiss(df)
|
63 |
sym_spell = setup_spell_checker()
|
64 |
|
65 |
-
# User input
|
66 |
query = st.text_input("Describe your symptoms or medical need:")
|
67 |
therapeutic_class = st.selectbox(
|
68 |
"Filter by Therapeutic Class (optional):",
|
69 |
['All'] + sorted(df['Therapeutic Class'].dropna().unique().tolist())
|
70 |
)
|
71 |
|
|
|
72 |
if query:
|
73 |
# Spelling correction
|
74 |
suggestions = sym_spell.lookup(query, Verbosity.CLOSEST, max_edit_distance=2)
|
@@ -80,15 +111,19 @@ def main():
|
|
80 |
query_embedding = model.encode([query])
|
81 |
D, I = faiss_index.search(query_embedding, k=5)
|
82 |
|
83 |
-
#
|
84 |
results = df.iloc[I[0]].copy()
|
85 |
if therapeutic_class != 'All':
|
86 |
results = results[results['Therapeutic Class'] == therapeutic_class]
|
87 |
|
|
|
|
|
|
|
|
|
88 |
# Display results
|
89 |
st.subheader("Recommended Medicines")
|
90 |
for _, row in results.iterrows():
|
91 |
-
with st.expander(f"π {row['name']}"):
|
92 |
cols = st.columns(3)
|
93 |
cols[0].write(f"**Uses:** {row['uses']}")
|
94 |
cols[1].write(f"**Substitutes:** {row['substitutes']}")
|
@@ -98,5 +133,24 @@ def main():
|
|
98 |
cols2[0].write(f"Therapeutic Class: {row['Therapeutic Class']}")
|
99 |
cols2[1].write(f"Habit Forming: {row['Habit Forming']}")
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
if __name__ == "__main__":
|
102 |
main()
|
|
|
51 |
term_index=0, count_index=1)
|
52 |
return sym_spell
|
53 |
|
54 |
+
# ----------------------
|
55 |
+
# Severity Analysis
|
56 |
+
# ----------------------
|
57 |
+
SEVERITY_RANK = {
|
58 |
+
'vomiting': 3, 'nausea': 3, 'diarrhea': 3,
|
59 |
+
'dizziness': 2, 'headache': 2, 'palpitations': 2,
|
60 |
+
'rash': 1, 'itching': 1, 'fatigue': 1
|
61 |
+
}
|
62 |
+
|
63 |
+
def severity_score(side_effects):
|
64 |
+
return sum(SEVERITY_RANK.get(effect.strip().lower(), 0)
|
65 |
+
for effect in side_effects.split(',') if effect.strip())
|
66 |
+
|
67 |
+
# ----------------------
|
68 |
+
# Drug Comparison
|
69 |
+
# ----------------------
|
70 |
+
def compare_drugs(df, drug1, drug2):
|
71 |
+
try:
|
72 |
+
d1 = df[df['name'].str.lower() == drug1.lower()].iloc[0]
|
73 |
+
d2 = df[df['name'].str.lower() == drug2.lower()].iloc[0]
|
74 |
+
|
75 |
+
comparison = pd.DataFrame({
|
76 |
+
'Attribute': ['Uses', 'Substitutes', 'Side Effects', 'Therapeutic Class'],
|
77 |
+
drug1: [d1['uses'], d1['substitutes'], d1['side_effects'], d1['Therapeutic Class']],
|
78 |
+
drug2: [d2['uses'], d2['substitutes'], d2['side_effects'], d2['Therapeutic Class']]
|
79 |
+
})
|
80 |
+
return comparison
|
81 |
+
except IndexError:
|
82 |
+
return pd.DataFrame()
|
83 |
+
|
84 |
# ----------------------
|
85 |
# Streamlit App
|
86 |
# ----------------------
|
|
|
92 |
model, faiss_index = setup_faiss(df)
|
93 |
sym_spell = setup_spell_checker()
|
94 |
|
95 |
+
# User input section
|
96 |
query = st.text_input("Describe your symptoms or medical need:")
|
97 |
therapeutic_class = st.selectbox(
|
98 |
"Filter by Therapeutic Class (optional):",
|
99 |
['All'] + sorted(df['Therapeutic Class'].dropna().unique().tolist())
|
100 |
)
|
101 |
|
102 |
+
# Process query and show results
|
103 |
if query:
|
104 |
# Spelling correction
|
105 |
suggestions = sym_spell.lookup(query, Verbosity.CLOSEST, max_edit_distance=2)
|
|
|
111 |
query_embedding = model.encode([query])
|
112 |
D, I = faiss_index.search(query_embedding, k=5)
|
113 |
|
114 |
+
# Process results
|
115 |
results = df.iloc[I[0]].copy()
|
116 |
if therapeutic_class != 'All':
|
117 |
results = results[results['Therapeutic Class'] == therapeutic_class]
|
118 |
|
119 |
+
# Add severity analysis
|
120 |
+
results['severity'] = results['side_effects'].apply(severity_score)
|
121 |
+
results = results.sort_values('severity', ascending=True)
|
122 |
+
|
123 |
# Display results
|
124 |
st.subheader("Recommended Medicines")
|
125 |
for _, row in results.iterrows():
|
126 |
+
with st.expander(f"π {row['name']} (Severity: {row['severity']})"):
|
127 |
cols = st.columns(3)
|
128 |
cols[0].write(f"**Uses:** {row['uses']}")
|
129 |
cols[1].write(f"**Substitutes:** {row['substitutes']}")
|
|
|
133 |
cols2[0].write(f"Therapeutic Class: {row['Therapeutic Class']}")
|
134 |
cols2[1].write(f"Habit Forming: {row['Habit Forming']}")
|
135 |
|
136 |
+
# Drug comparison section
|
137 |
+
st.subheader("π Drug Comparison Tool")
|
138 |
+
col1, col2 = st.columns(2)
|
139 |
+
drug_list = df['name'].unique().tolist()
|
140 |
+
|
141 |
+
with col1:
|
142 |
+
drug1 = st.selectbox("Select first drug:", drug_list, index=0)
|
143 |
+
with col2:
|
144 |
+
drug2 = st.selectbox("Select second drug:", drug_list, index=1 if len(drug_list) > 1 else 0)
|
145 |
+
|
146 |
+
comparison_df = compare_drugs(df, drug1, drug2)
|
147 |
+
if not comparison_df.empty:
|
148 |
+
st.table(comparison_df.style.set_properties(**{
|
149 |
+
'white-space': 'pre-wrap',
|
150 |
+
'text-align': 'left'
|
151 |
+
}))
|
152 |
+
else:
|
153 |
+
st.warning("One or both selected drugs not found in database")
|
154 |
+
|
155 |
if __name__ == "__main__":
|
156 |
main()
|