-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (76 loc) · 3.14 KB
/
main.py
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
99
100
101
102
103
import pickle
import chatbot as cb
from keras.models import load_model
import json
import random
import nltk
import streamlit as st
from pydantic import BaseModel
from typing import List
# Downloads needed nltk
nltk.download('punkt')
nltk.download('wordnet')
nltk.download("omw-1.4")
# Loads needed files
model = load_model('chatbot_model.h5')
intents = json.loads(open('intents.json').read())
words = pickle.load(open('words.pkl','rb'))
classes = pickle.load(open('classes.pkl','rb'))
oldMessage = "hi!"
# Message class defined in Pydantic
class Message(BaseModel):
isAi: bool
text: str
# Sets page config for the streamlit page
st.set_page_config(page_title='AiRO', page_icon='🧠', layout="wide")
def local_css():
with open("style.css") as f:
# Customizes the CSS
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
# Loads Custom CSS
local_css()
# Intializes the state variables (the vars that are persistant)
if 'count' not in st.session_state:
st.session_state.count = 0
if 'messages' not in st.session_state:
st.session_state.messages = list()
if 'newMessage' not in st.session_state:
st.session_state.newMessage = ""
def chatBubble(message):
# Initalize columns so the messages are at opposite sides of the page
col1, col2 = st.columns(2)
if message.isAi:
with col1:
# Html bubble message
if message.text != "":
st.markdown(f'<div class="chat"><div class="yours messages"><div class="message last">{message.text}</div></div> <div class="yours messages">', unsafe_allow_html=True)
else:
with col2:
# Html bubble message
if message.text != "":
st.markdown(f'<div id= "chat" class="chat"><div class="mine messages"><div class="message last">{message.text}</div></div> <div class="mine messages">', unsafe_allow_html=True)
# Image on top
# Title below image
st.title("Construction.Site Support")
# Subheader below title
# Initalize button
start = True
with st.container():
# If start then add to count state var, is a count so that when the page updates, the chat still appears
if start:
st.session_state.count += 1
if st.session_state.count > 0:
if st.session_state.newMessage != "":
st.session_state.messages.append(Message(isAi=False, text= st.session_state.newMessage))
aiMessage = Message(isAi=True, text=cb.chatbot_response(st.session_state.newMessage))
st.session_state.messages.append(aiMessage)
#chatBubble(st.session_state.lastMessage)
for message in st.session_state.messages:
chatBubble(message)
#st.markdown("<script language='javascript'>document.getElementById('chat').scrollIntoView();</script>", unsafe_allow_html=True)
# Set newMessage to a text field
#st.session_state.newMessage = st.text_input("Send Message")
st.text_input("Send Message", key="newMessage")
# Initalize button
# If send, send the message and process the text for the chatbot
# Display the chat bubbles