-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
98 lines (73 loc) · 2.75 KB
/
app.py
File metadata and controls
98 lines (73 loc) · 2.75 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 28 18:11:06 2020
@author: Jfitz
"""
import numpy as np
import pandas as pd
import preprocessor as p
from tensorflow.keras.models import load_model
import joblib
from pathlib import Path
from PIL import Image
import streamlit as st
#paths
img_path = Path.joinpath(Path.cwd(),'images')
artifacts_path = Path.joinpath(Path.cwd(),'model_artifacts')
datasets_path = Path.joinpath(Path.cwd(),'dataset')
#load images
center = Image.open(Path.joinpath(img_path,'center.jpg'))
federer_image = Image.open(Path.joinpath(img_path,'federer.jpg'))
nadal =Image.open(Path.joinpath(img_path,'Nadal.jpg'))
#load artefacts
model = load_model(Path.joinpath(artifacts_path,'model-v1.h5'))
tokenizer_t = joblib.load(Path.joinpath(artifacts_path,'tokenizer_t.pkl'))
vocab = joblib.load(Path.joinpath(artifacts_path,'vocab.pkl'))
df2 = pd.read_csv(Path.joinpath(datasets_path,'response.csv'))
def get_pred(model,encoded_input):
pred = np.argmax(model.predict(encoded_input))
return pred
def bot_precausion(df_input,pred):
words = df_input.questions[0].split()
if len([w for w in words if w in vocab])==0 :
pred = 1
return pred
def get_response(df2,pred):
upper_bound = df2.groupby('labels').get_group(pred).shape[0]
r = np.random.randint(0,upper_bound)
responses = list(df2.groupby('labels').get_group(pred).response)
return responses[r]
def bot_response(response,):
return response
def botResponse(user_input,is_startup=True):
df_input = user_input
df_input = p.remove_stop_words_for_input(p.tokenizer,df_input,'questions')
encoded_input = p.encode_input_text(tokenizer_t,df_input,'questions')
pred = get_pred(model,encoded_input)
pred = bot_precausion(df_input,pred)
response = get_response(df2,pred)
response = bot_response(response)
if is_startup:
response = "Hi, I'm happy to have you here \nI have a lot to discuss about tennis"
is_startup = False
return response
else:
return response
def get_text():
input_text = st.text_input("You: ","type here")
df_input = pd.DataFrame([input_text],columns=['questions'])
return df_input
st.sidebar.title("BoTennis")
st.title("""
BoTennis
BoTennis is an NLP conversational chatbot trainned on tennis corpus using CNN achitecture
""")
st.image(center,width=700)
st.sidebar.image(federer_image)
st.sidebar.image(nadal,width=350)
user_input = get_text()
if str(user_input['questions']) =="type here":
response = botResponse(user_input)
else:
response = botResponse(user_input,is_startup=False)
st.text_area("Bot:", value=response, height=200, max_chars=None, key=None)