Spaces:
Build error
Build error
import streamlit as st | |
import nltk | |
import pickle | |
import numpy as np | |
from tensorflow.keras.models import load_model | |
from nltk.stem import WordNetLemmatizer | |
# Load the pre-trained model and other data | |
model = load_model("chatbot_model.h5") | |
words = pickle.load(open('words.pkl', 'rb')) | |
classes = pickle.load(open('classes.pkl', 'rb')) | |
lemmatizer = WordNetLemmatizer() | |
# Function to preprocess user input | |
def clean_up_sentence(sentence): | |
sentence_words = nltk.word_tokenize(sentence) | |
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words] | |
return sentence_words | |
# Function to convert input to bag-of-words format | |
def bow(sentence, words, show_details=True): | |
sentence_words = clean_up_sentence(sentence) | |
bag = [0]*len(words) | |
for s in sentence_words: | |
for i, w in enumerate(words): | |
if w == s: | |
bag[i] = 1 | |
if show_details: | |
print(f"found in bag: {w}") | |
return np.array(bag) | |
# Streamlit app | |
def main(): | |
st.title("Healthcare Chatbot") | |
st.write("Welcome to the Healthcare Chatbot! Enter your symptoms below.") | |
user_input = st.text_input("You:") | |
if st.button("Predict"): | |
if user_input.strip() == "": | |
st.write("Bot: Please enter your symptoms.") | |
else: | |
p = bow(user_input, words) | |
res = model.predict(np.array([p]))[0] | |
ERROR_THRESHOLD = 0.25 | |
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD] | |
results.sort(key=lambda x: x[1], reverse=True) | |
for r in results: | |
return_class = classes[r[0]] | |
break | |
st.write("Bot: Based on your symptoms, you might have:", return_class) | |
if __name__ == "__main__": | |
main() | |