ajeetkumar01 commited on
Commit
1b67f7c
1 Parent(s): 0f0cf5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from keras.models import load_model
3
+ from tensorflow.keras.preprocessing.text import tokenizer_from_json
4
+ import contractions
5
+ import re
6
+ from nltk.corpus import stopwords
7
+ import json
8
+
9
+ # Set page configuration
10
+ st.set_page_config(page_title="Mental Health Classification")
11
+
12
+ # Page title
13
+ st.title("Mental Health Classification")
14
+
15
+ # Load the tokenizer (make sure the tokenizer file is in the correct path)
16
+ def load_tokenizer():
17
+ with open('tokenizer.json') as f:
18
+ tokenizer_json = json.load(f)
19
+ return tokenizer_from_json(tokenizer_json)
20
+
21
+ # Preprocess text function
22
+ def preprocess_text(input_text, tokenizer):
23
+ text = contractions.fix(input_text)
24
+ text = re.sub(r"[^a-z\s]", "", text)
25
+ text = text.lower()
26
+ # Tokenize the words
27
+ words = text.split()
28
+ # Remove stopwords
29
+ stop_words = set(stopwords.words('english'))
30
+ words = [word for word in words if word not in stop_words]
31
+ clean_text = " ".join(words)
32
+ # Convert to sequences
33
+ sequences = tokenizer.texts_to_sequences([clean_text])
34
+ return sequences
35
+
36
+ def main():
37
+ # Text input for mental state
38
+ input_text = st.text_input("Enter the Mental state here...")
39
+
40
+ # Submit button
41
+ submit_button = st.button("Classify")
42
+
43
+ if submit_button and input_text:
44
+ # Load the model and tokenizer
45
+ model = load_model("mental_health_model.h5")
46
+ tokenizer = load_tokenizer()
47
+
48
+ # Preprocess the input text
49
+ processed_text = preprocess_text(input_text, tokenizer)
50
+
51
+ # Make prediction
52
+ response = model.predict(processed_text)
53
+ predicted_class = response.argmax(axis=-1)
54
+ # Display the prediction result
55
+ st.write("Predicted Mental State:", predicted_class)
56
+
57
+ if __name__ == "__main__":
58
+ main()