Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
|
5 |
+
# Load the pre-trained embedding model
|
6 |
+
@st.cache_resource # Cache the embedding model to save loading time
|
7 |
+
def load_embedding_model():
|
8 |
+
return SentenceTransformer('neuml/pubmedbert-base-embeddings')
|
9 |
+
|
10 |
+
# Load the MLP model
|
11 |
+
@st.cache_resource # Cache the loaded model
|
12 |
+
def load_mlp_model():
|
13 |
+
with open("MLP.pkl", "rb") as file:
|
14 |
+
return joblib.load(file)
|
15 |
+
|
16 |
+
# Embed text
|
17 |
+
def get_embeddings(title, abstract, embedding_model):
|
18 |
+
# Concatenate title and abstract
|
19 |
+
combined_text = title + " " + abstract
|
20 |
+
return embedding_model.encode(combined_text)
|
21 |
+
|
22 |
+
# Main Streamlit app
|
23 |
+
def main():
|
24 |
+
st.title("MLP Predictor for Titles and Abstracts")
|
25 |
+
|
26 |
+
# Input fields
|
27 |
+
title = st.text_input("Enter the Title:")
|
28 |
+
abstract = st.text_area("Enter the Abstract:")
|
29 |
+
|
30 |
+
# Load models
|
31 |
+
embedding_model = load_embedding_model()
|
32 |
+
mlp_model = load_mlp_model()
|
33 |
+
|
34 |
+
# Predict button
|
35 |
+
if st.button("Predict Label"):
|
36 |
+
if title.strip() == "" or abstract.strip() == "":
|
37 |
+
st.error("Both Title and Abstract are required!")
|
38 |
+
else:
|
39 |
+
# Get embeddings
|
40 |
+
embeddings = get_embeddings(title, abstract, embedding_model)
|
41 |
+
|
42 |
+
# Make prediction
|
43 |
+
prediction = mlp_model.predict([embeddings])[0] # Input should be a 2D array
|
44 |
+
|
45 |
+
# Display result
|
46 |
+
st.success(f"The predicted label is: {prediction}")
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
main()
|