Spaces:
Sleeping
Sleeping
add app file
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import BertForSequenceClassification, BertTokenizer
|
3 |
+
from sklearn.preprocessing import LabelEncoder
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load model and label encoder
|
8 |
+
@st.cache(allow_output_mutation=True)
|
9 |
+
def load_model_and_label_encoder():
|
10 |
+
fine_tuned_model = BertForSequenceClassification.from_pretrained('./fine_tuned_model')
|
11 |
+
label_encoder = LabelEncoder()
|
12 |
+
label_encoder.classes_ = np.load('./label_encoder_classes.npy', allow_pickle=True)
|
13 |
+
tokenizer = BertTokenizer.from_pretrained("./tokenizer") # Load tokenizer from local file
|
14 |
+
return fine_tuned_model, label_encoder, tokenizer
|
15 |
+
|
16 |
+
def predict(symptom, fine_tuned_model, label_encoder, tokenizer):
|
17 |
+
user_input_encoding = tokenizer(symptom, padding=True, truncation=True, return_tensors='pt', max_length=512, return_attention_mask=True, return_token_type_ids=True)
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = fine_tuned_model(**user_input_encoding)
|
21 |
+
probabilities = torch.nn.functional.softmax(logits.logits, dim=1).numpy()[0]
|
22 |
+
predicted_labels = np.argsort(-probabilities)[:5]
|
23 |
+
predicted_diseases = label_encoder.inverse_transform(predicted_labels)
|
24 |
+
predicted_probabilities = probabilities[predicted_labels]
|
25 |
+
|
26 |
+
predictions = [{'disease': disease, 'probability': probability * 100} for disease, probability in zip(predicted_diseases, predicted_probabilities)]
|
27 |
+
return predictions
|
28 |
+
|
29 |
+
def main():
|
30 |
+
st.title("Disease Prediction App")
|
31 |
+
|
32 |
+
# Load model and label encoder
|
33 |
+
fine_tuned_model, label_encoder, tokenizer = load_model_and_label_encoder()
|
34 |
+
|
35 |
+
# Input
|
36 |
+
symptom = st.text_input("Enter symptom:", "")
|
37 |
+
|
38 |
+
# Predict
|
39 |
+
if st.button("Predict"):
|
40 |
+
if symptom:
|
41 |
+
predictions = predict(symptom, fine_tuned_model, label_encoder, tokenizer)
|
42 |
+
st.write("Top 5 Predictions:")
|
43 |
+
for prediction in predictions:
|
44 |
+
st.write(f"Disease: {prediction['disease']}, Probability: {prediction['probability']:.2f}%")
|
45 |
+
|
46 |
+
if __name__ == '__main__':
|
47 |
+
main()
|