File size: 1,780 Bytes
1b67f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from keras.models import load_model
from tensorflow.keras.preprocessing.text import tokenizer_from_json
import contractions
import re
from nltk.corpus import stopwords
import json

# Set page configuration
st.set_page_config(page_title="Mental Health Classification")

# Page title
st.title("Mental Health Classification")

# Load the tokenizer (make sure the tokenizer file is in the correct path)
def load_tokenizer():
    with open('tokenizer.json') as f:
        tokenizer_json = json.load(f)
    return tokenizer_from_json(tokenizer_json)

# Preprocess text function
def preprocess_text(input_text, tokenizer):
    text = contractions.fix(input_text)
    text = re.sub(r"[^a-z\s]", "", text)
    text = text.lower()
    # Tokenize the words
    words = text.split()
    # Remove stopwords
    stop_words = set(stopwords.words('english'))
    words = [word for word in words if word not in stop_words]
    clean_text = " ".join(words)
    # Convert to sequences
    sequences = tokenizer.texts_to_sequences([clean_text])
    return sequences

def main():
    # Text input for mental state
    input_text = st.text_input("Enter the Mental state here...")

    # Submit button
    submit_button = st.button("Classify")

    if submit_button and input_text:
        # Load the model and tokenizer
        model = load_model("mental_health_model.h5")
        tokenizer = load_tokenizer()

        # Preprocess the input text
        processed_text = preprocess_text(input_text, tokenizer)

        # Make prediction
        response = model.predict(processed_text)
        predicted_class = response.argmax(axis=-1) 
        # Display the prediction result
        st.write("Predicted Mental State:", predicted_class)

if __name__ == "__main__":
    main()