Skip to content

Commit fdd65d1

Browse files
committed
combine grammar and language models
1 parent 30dbe98 commit fdd65d1

File tree

5 files changed

+14
-20
lines changed

5 files changed

+14
-20
lines changed

chatbot/chat.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,10 @@ def get_bot_response(chat_history):
9292
""" Get response from the bot """
9393

9494
if current_app.config['LOAD_GRAMMAR_MODEL']:
95-
pass
96-
# chat_history = current_app.grammar_correction.add_correction_to_chat_history(chat_history)
95+
# pass
96+
chat_history = current_app.grammar_correction.add_correction_to_chat_history(chat_history)
9797
chat_history = current_app.language_model.add_response_to_chat_history(chat_history)
98-
# import sys
99-
# print("-----------------\nChat History\n\n", chat_history, file=sys.stdout)
100-
# print("-----------------\nChat History\n\n", chat_history.get_as_prompt_with_dialog(), file=sys.stdout)
101-
98+
10299

103100
return chat_history
104101

@@ -157,13 +154,16 @@ def add_user_message(self, text: str):
157154
message = ChatMessage(sender='user', text=text)
158155
self.add_message(message)
159156

160-
def add_bot_message(self, text: str):
161-
message = ChatMessage(sender='bot', text=text)
157+
def add_bot_message(self, text: str, correction: bool=False):
158+
message = ChatMessage(sender='bot', text=text, correction=correction)
162159
self.add_message(message)
163160

164161
def add_message(self, chat_message: ChatMessage):
165162
self.messages.append(chat_message)
166163

164+
def get_last_message_text(self) -> ChatMessage:
165+
return self.messages[-1].text
166+
167167
def get_as_prompt_with_dialog(self, limit_messages: int=10):
168168
""" Returns the chat history in a Dialog format
169169

chatbot/dashboard.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def dashboard():
1919

2020

2121
featured_prompts = {key: current_app.prompts[key] for key in
22-
['general_chat_beginner', 'scenario_restaurant', 'persona_shakespeare']
22+
['general_chat_intermediate', 'scenario_restaurant', 'persona_shakespeare']
2323
}
2424

2525
return render_template('dashboard/dashboard.html',

chatbot/grammar_correction.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,14 @@ def add_correction_to_chat_history(self, chat_history):
3737
Append the message to the user to the chat history.
3838
Return the corrected sentence.
3939
"""
40-
last_user_input = chat_history[-1].get('text')
40+
last_user_input = chat_history.get_last_message_text()
4141
corrected_sentence, correction_message = self.grammar_correction(last_user_input)
4242
error_types = self.get_edits(last_user_input, corrected_sentence)
4343
relevant_error = any(error not in self.ignore_errors for error in error_types) # check if there is an error in the sentence which is not in the ignore list
4444
token_sort_ratio = fuzz.token_sort_ratio(corrected_sentence, last_user_input) # calculate token similarity (ignoring punctuation and casing)
4545

4646
if correction_message and relevant_error and token_sort_ratio != 100:
47-
chat_history.append(
48-
{
49-
'sender': 'bot',
50-
'text': correction_message,
51-
'correction': True
52-
}
53-
)
47+
chat_history.add_bot_message(text=correction_message, correction=True)
5448

5549
return chat_history
5650

chatbot/templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!doctype html>
22
<html lang="en">
3-
<title>{% block title %}{% endblock %}Chatbot</title>
3+
<title>{% block title %}{% endblock %}Nova AI</title>
44
<head>
55
<meta charset="utf-8" />
66
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
@@ -24,7 +24,7 @@
2424
<!-- Responsive navbar-->
2525
<nav class="navbar navbar-expand-lg navbar-light">
2626
<div class="container px-5">
27-
<a class="navbar-brand" href="{{ url_for('dashboard.dashboard') if g.user else url_for('index') }}">Home</a>
27+
<a class="navbar-brand" href="{{ url_for('dashboard.dashboard') if g.user else url_for('index') }}">Nova AI</a>
2828
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
2929
<div class="collapse navbar-collapse" id="navbarSupportedContent">
3030
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def post_install_commands():
3838

3939

4040
setup(name='chatbot',
41-
version = '0.0.2',
41+
version = '0.1.0',
4242
license='MIT',
4343
author='Aloïs Villa, Frank Schlosser',
4444
description='A language learning chatbot',

0 commit comments

Comments
 (0)