Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from tensorflow import keras
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# load dataset
|
| 11 |
+
with open("static/AI/intents.json", encoding="utf8") as file:
|
| 12 |
+
data = json.load(file)
|
| 13 |
+
|
| 14 |
+
# load tokenizer object
|
| 15 |
+
with open('static/AI/tokenizer.pickle', 'rb') as handle:
|
| 16 |
+
tokenizer = pickle.load(handle)
|
| 17 |
+
|
| 18 |
+
# load label encoder object
|
| 19 |
+
with open('static/AI/label_encoder.pickle', 'rb') as enc:
|
| 20 |
+
lbl_encoder = pickle.load(enc)
|
| 21 |
+
|
| 22 |
+
# load pretrained model
|
| 23 |
+
model = keras.models.load_model('static/AI/model')
|
| 24 |
+
|
| 25 |
+
# parameters
|
| 26 |
+
max_len = 20
|
| 27 |
+
|
| 28 |
+
question = st.textarea('اسأل سؤال....')
|
| 29 |
+
|
| 30 |
+
result = model.predict(keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([question]),
|
| 31 |
+
truncating='post', maxlen=max_len))
|
| 32 |
+
|
| 33 |
+
tag = lbl_encoder.inverse_transform([np.argmax(result)])
|
| 34 |
+
|
| 35 |
+
for i in data['intents']:
|
| 36 |
+
if i['tag'] == tag:
|
| 37 |
+
out = np.random.choice(i['responses'])
|
| 38 |
+
st.json(out)
|