|
|
import json |
|
|
import random |
|
|
import pickle |
|
|
import numpy as np |
|
|
import streamlit as st |
|
|
from tensorflow import keras |
|
|
from sklearn.preprocessing import LabelEncoder |
|
|
|
|
|
|
|
|
|
|
|
with open("intents.json", encoding="utf8") as file: |
|
|
data = json.load(file) |
|
|
|
|
|
|
|
|
with open('tokenizer.pickle', 'rb') as handle: |
|
|
tokenizer = pickle.load(handle) |
|
|
|
|
|
|
|
|
with open('label_encoder.pickle', 'rb') as enc: |
|
|
lbl_encoder = pickle.load(enc) |
|
|
|
|
|
|
|
|
model = keras.models.load_model('model') |
|
|
|
|
|
|
|
|
max_len = 20 |
|
|
|
|
|
question = st.text_area('.... HelBot اسأل') |
|
|
|
|
|
if question == '': |
|
|
st.write('') |
|
|
|
|
|
|
|
|
else: |
|
|
result = model.predict(keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([question]), |
|
|
truncating='post', maxlen=max_len)) |
|
|
|
|
|
|
|
|
idk = ['عذراً، لا يمكنني الإجابة عن السؤال المطروح', 'عذراً، لم أفهم سؤالك'] |
|
|
|
|
|
if result[0][np.argmax(result)] < 0.65: |
|
|
st.write(np.random.choice(idk)) |
|
|
|
|
|
else: |
|
|
tag = lbl_encoder.inverse_transform([np.argmax(result)]) |
|
|
|
|
|
for i in data['intents']: |
|
|
if i['tag'] == tag: |
|
|
out = np.random.choice(i['responses']) |
|
|
st.write(out) |