-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
42 lines (34 loc) · 1.34 KB
/
app.py
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
import json
import streamlit as st
import numpy as np
import tensorflow as tf
from keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle
# Load the model, tokenizer, and label encoder
model = load_model('chatbot_model.h5')
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
with open('label_encoder.pickle', 'rb') as handle:
label_encoder = pickle.load(handle)
# Function to predict intents based on user input
def predict_intent(user_input):
user_input_seq = tokenizer.texts_to_sequences([user_input])
user_input_pad = pad_sequences(user_input_seq, maxlen=model.input_shape[1], padding='post')
prediction = model.predict(user_input_pad)
intent = label_encoder.inverse_transform([np.argmax(prediction)])
return intent[0]
# Function to generate responses based on predicted intents
def generate_response(intent):
with open('therapy_data.json', 'r') as f:
data = json.load(f)
responses = [i['responses'] for i in data['intents'] if i['tag'] == intent]
response = np.random.choice(responses[0])
return response
# Streamlit UI
st.title("AI Therapy Chatbot")
user_input = st.text_input("You:", "")
if user_input:
intent = predict_intent(user_input)
response = generate_response(intent)
st.write(f"Chatbot: {response}")