I have programmed a ChatBot with Spacy in Python, which I now want to integrate somehow into my website (with HTML and JavaScript), as I have made an interface in HTML for the bot. But my webserver doesn’t support Python so I have to do it somehow via JavaScript or something. My code:
import spacy
import random
# load NLP model
nlp = spacy.load("en_core_news_sm")
nlp.max_length = 10000000
# load text from file
with open("deutsch_corpus.txt", encoding='utf-8') as f:
text = f.read()
# parse text with NLP model
doc = nlp(text)
# define function to generate bot response
def generate_response(user_input):
# parse user input with NLP model
user_doc = nlp(user_input)
# check if the user input contains information that should be stored
remember = False
remember_text = ""
for sent in user_doc.sents:
if any(word in sent.text.lower() for word in ["I am", "my friend", "my girlfriend", "I was", "I have", "I have", "my mother is", "my mother is", "I would be", "I find", "my name is"]):
remember = True
remember_text = sent.text
break
# If the user input contains information to remember, store it in the memory file
if remember:
remember_text = remember_text.lower()
with open("german_corpus.txt", "a", encoding='utf-8') as f:
f.write(remember_text.replace("ich bin", "Du bist").replace("mein", "Dein").replace("meine", "Deine").replace("meine mutter ist", "deine Mutter ist").replace("meine freundin ist", "deine Freundin ist"). replace("would have", "would have").replace("have", "have").replace("have", "have").replace("I would be", "you would be").replace("I find", "you find").replace("my name", "your name is").replace("my name is", "your name is") + "n")
return "Ok, I'll remember that!"
# search for sentences in the corpus that are similar to the user input
similarity_scores = []
for sentence in doc.sents:
score = sentence.similarity(user_doc)
similarity_scores.append((sentence, score))
# sort sentences by similarity score
similarity_scores.sort(key=lambda x: x[1], reverse=True)
# if no similar sentence is found, a random answer is returned
if similarity_scores[0][1] < 0.5:
return "Sorry, I don't understand."
# Return most similar sentence as bot response.
return similarity_scores[0][0].text
… and my HTML Code:
<!DOCTYPE html>
<html lang="de">
<head>
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TalkyAI</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">KI trainieren</a></li>
<li><a href="#" class="active">Chat</a></li>
<li><a href="#">Account</a></li>
</ul>
<div class="logout-btn"><a href="#">Abmelden</a></div>
</nav>
</header>
<main>
<div id="chat-container">
<div id="chat-output"></div>
<div id="chat-input">
<input type="text" id="message-input" placeholder="Gib deine Nachricht ein...">
<button id="send-btn">Senden</button>
</div>
</div>
</main>
<script>
</script>
</body>
</html>
I already tried to translate my code to JavaScript, which all didn’t really work (NLP, Tensorflow, etc.) and my code doesn’t work like it does in Python.
2
Answers
As of now, spaCy seems to support Python only. spacy-js just exposes a Javascript interface but still uses Python under the hood. I can’t seem to find any options other than creating a Python API for your webserver, which seems to be out of the question.
One way to integrate your Spacy ChatBot into your website is to create a server-side API with Flask (a Python web framework) and make AJAX requests to the API from your website’s JavaScript code.