-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (54 loc) · 2.32 KB
/
app.py
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
import streamlit as st
import joblib
import pandas as pd
from bs4 import BeautifulSoup
import re
import nltk
from nltk.corpus import stopwords
# Download stopwords if not already done
nltk.download('stopwords')
# Load stopwords
stop = stopwords.words('english')
# Load the models
tfidf_vectorizer = joblib.load('tfidfvectorizer.joblib')
model = joblib.load('passmodel.joblib')
# Load the training data to get the unique conditions
df_train = pd.read_csv('./data/drugsComTrain_raw.tsv', sep='\t')
df_train = df_train[(df_train['condition']=='Birth Control') | (df_train['condition']=='Depression') | (df_train['condition']=='High Blood Pressure')|(df_train['condition']=='Diabetes, Type 2')]
# Function to clean and preprocess the text
def review_to_words(raw_review):
review_text = BeautifulSoup(raw_review, 'html.parser').get_text()
letters_only = re.sub('[^a-zA-Z]', ' ', review_text)
words = letters_only.lower().split()
meaningful_words = [w for w in words if not w in stop]
return ' '.join(meaningful_words)
# Function for extracting top drugs
def top_drugs_extractor(condition):
df_top = df_train[(df_train['rating']>=9)&(df_train['usefulCount']>=100)].sort_values(by = ['rating', 'usefulCount'], ascending = [False, False])
drug_lst = df_top[df_top['condition']==condition]['drugName'].head(3).tolist()
return drug_lst
# Streamlit web application
st.set_page_config(
page_title="Patient's Condition Classification and Drug Recommendation",
page_icon="🩺",
layout="centered"
)
st.title("Enter Medical Issue to Get Drug Recommendation")
# User input
user_input = st.text_area("Enter your review here:")
if st.button("Predict"):
if user_input:
# Preprocess the input
clean_input = review_to_words(user_input)
# Transform the input
tfidf_input = tfidf_vectorizer.transform([clean_input])
# Predict the condition
prediction = model.predict(tfidf_input)[0]
# Get the top 3 drug recommendations
top_drugs = top_drugs_extractor(prediction)
st.write(f"Condition: {prediction}")
st.write("Top 3 Suggested Drugs:")
for i, drug in enumerate(top_drugs, 1):
st.write(f"{i}. {drug}")
else:
st.write("Please enter a review to predict the condition and get drug recommendations.")