Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ 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
|
|
@@ -25,6 +29,29 @@ model = keras.models.load_model('model')
|
|
| 25 |
# parameters
|
| 26 |
max_len = 20
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
question = st.text_area('....اسأل سؤال')
|
| 29 |
|
| 30 |
result = model.predict(keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([question]),
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
from tensorflow import keras
|
| 7 |
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
from flask import Flask, request, jsonify
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
|
| 13 |
|
| 14 |
# load dataset
|
|
|
|
| 29 |
# parameters
|
| 30 |
max_len = 20
|
| 31 |
|
| 32 |
+
|
| 33 |
+
@app.route('/chat', methods=['POST'])
|
| 34 |
+
def chat():
|
| 35 |
+
# Retrieve the input text from the request
|
| 36 |
+
question = request.json['text']
|
| 37 |
+
|
| 38 |
+
result = model.predict(keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([question]),
|
| 39 |
+
truncating='post', maxlen=max_len))
|
| 40 |
+
|
| 41 |
+
tag = lbl_encoder.inverse_transform([np.argmax(result)])
|
| 42 |
+
|
| 43 |
+
for i in data['intents']:
|
| 44 |
+
if i['tag'] == tag:
|
| 45 |
+
answer = np.random.choice(i['responses'])
|
| 46 |
+
|
| 47 |
+
# Return the answer as a JSON response
|
| 48 |
+
response = {'answer': answer}
|
| 49 |
+
return jsonify(response)
|
| 50 |
+
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
app.run()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
question = st.text_area('....اسأل سؤال')
|
| 56 |
|
| 57 |
result = model.predict(keras.preprocessing.sequence.pad_sequences(tokenizer.texts_to_sequences([question]),
|