-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
131 lines (101 loc) · 3.96 KB
/
app.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from get_answer import get_answer
#from app import server
#from app import app
#Theme
#https://bootswatch.com/vapor/
#Icons
# https://fontawesome.com/v5.15/icons?d=gallery&p=2
#Use if you want to change the style later or if you wanna make your own css
BS = "https://bootswatch.com/5/vapor/bootstrap.min.css"
FONT_AWESOME = "https://use.fontawesome.com/releases/v5.7.2/css/all.css"
#CUSTOM_STYLE = "/assets/style.css"
external_stylesheets=[BS, FONT_AWESOME]
print("OK")
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
def textbox(text, box="other"):
style = {
"max-width": "55%",
"width": "max-content",
"padding": "10px 15px",
"border-radius": "25px",
}
if box == "self":
style["margin-left"] = "auto"
style["margin-right"] = 0
color = "primary"
inverse = True
elif box == "other":
style["margin-left"] = 0
style["margin-right"] = "auto"
color = "light"
inverse = False
else:
raise ValueError("Incorrect option for `box`.")
return dbc.Card(text, style=style, body=True, color=color, inverse=inverse)
conversation = html.Div(
style={
"width": "80%",
"max-width": "800px",
"height": "70vh",
"margin": "auto",
"overflow-y": "auto",
},
id="display-conversation",
)
controls = dbc.InputGroup(
style={"width": "80%", "max-width": "800px", "margin": "auto"},
children=[
dbc.Input(id="user-input", placeholder="Ask a Factual Question...", type="text"),
dbc.InputGroup(dbc.Button("Submit", id="submit", color="success", outline=True)),
],
)
# Define Layout
app.layout = dbc.Container(
fluid=True,
children=[
html.H1([html.I(className="fab fa-wikipedia-w ml-0"),"iki-QA-Bot ", html.I(className="fas fa-robot ml-0")]),
html.H5("I am a question-answering bot fine-tuned on WikiQA"),
html.H5("Learn more below:"),
dbc.Button(html.Span(["", html.I(className="fab fa-github ml-2")]), href="https://github.com/mparoca/chatbot_D-590", color="secondary"),
dbc.Button(html.Span(["", html.I(className="fab fa-windows ml-2")]), href="https://www.microsoft.com/en-us/research/publication/wikiqa-a-challenge-dataset-for-open-domain-question-answering/", color="info"),
dbc.Button(html.Span(["", html.I(className="fab fa-linkedin ml-2")]), href="https://www.linkedin.com/in/maria-paula-aroca-42a0a5166/"),
html.Hr(),
dcc.Store(id="store-conversation", data="Hello! I am a knowledge-based question answering bot. If you ask me a factual question I will give you the answer. Try asking : what does the president of the usa do?"),
conversation,
controls,
html.Hr(),
],
)
## CALLBACKS
@app.callback(
Output("display-conversation", "children"), [Input("store-conversation", "data")]
)
def update_display(chat_history):
return [
textbox(x, box="self") if i % 2 == 0 else textbox(x, box="other")
for i, x in enumerate(chat_history.split(">"))
]
@app.callback(
[Output("store-conversation", "data"), Output("user-input", "value")],
[Input("submit", "n_clicks"), Input("user-input", "n_submit")],
[State("user-input", "value"), State("store-conversation", "data")],
)
def run_chatbot(n_clicks, n_submit, user_input, chat_history):
if n_clicks == 0:
return "", ""
if user_input is None or user_input == "":
return chat_history, ""
try:
get_answer(user_input)
return chat_history + ">Q: " + user_input + ">A: " + get_answer(user_input), ""
except:
return chat_history + ">Q: " + user_input + ">Sorry, please be more specific, I'm still learning", ""
server = app.server
app.config.suppress_callback_exceptions = True
if __name__ == '__main__':
app.run_server(debug=True)