|
import streamlit as st |
|
from keras.models import load_model |
|
from tensorflow.keras.preprocessing.text import tokenizer_from_json |
|
import contractions |
|
import re |
|
import nltk |
|
import numpy as np |
|
from nltk.corpus import stopwords |
|
import json |
|
|
|
|
|
|
|
st.set_page_config(page_title="Mental Health Classification") |
|
|
|
|
|
st.title("Mental Health Classification") |
|
|
|
|
|
nltk.download('stopwords') |
|
|
|
|
|
def load_tokenizer(): |
|
with open('tokenizer.json') as f: |
|
tokenizer_json = json.load(f) |
|
return tokenizer_from_json(tokenizer_json) |
|
|
|
|
|
def preprocess_text(input_text, tokenizer): |
|
text = contractions.fix(input_text) |
|
text = re.sub(r"[^a-z\s]", "", text) |
|
text = text.lower() |
|
|
|
words = text.split() |
|
|
|
stop_words = set(stopwords.words('english')) |
|
words = [word for word in words if word not in stop_words] |
|
clean_text = " ".join(words) |
|
|
|
sequences = tokenizer.texts_to_sequences([clean_text]) |
|
|
|
padded_sequences = np.array(sequences) |
|
return padded_sequences |
|
|
|
class_labels = { |
|
1: "Anxiety", |
|
2: "Normal", |
|
3: "Depression", |
|
4: "Suicidal", |
|
5: "Stress", |
|
6: "Bipolar", |
|
7: "Personality disorder" |
|
} |
|
|
|
def main(): |
|
|
|
input_text = st.text_input("Enter the Mental state here...") |
|
|
|
|
|
submit_button = st.button("Classify") |
|
|
|
if submit_button and input_text: |
|
|
|
model = load_model("mental_healths_model.h5") |
|
tokenizer = load_tokenizer() |
|
|
|
|
|
processed_text = preprocess_text(input_text, tokenizer) |
|
|
|
|
|
response = model.predict(processed_text) |
|
predicted_class = response.argmax(axis=-1)[0] |
|
|
|
|
|
predicted_label = class_labels.get(predicted_class, "Unknown") |
|
|
|
|
|
st.write("Predicted Index:", predicted_class) |
|
st.write("Predicted Mental State:", predicted_label) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|