如何将Python集成到我的JavaScript代码中?

huangapple go评论108阅读模式
英文:

How can I integrate python into my javascript code?

问题

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:

  1. import spacy
  2. import random
  3. # load NLP model
  4. nlp = spacy.load("en_core_news_sm")
  5. nlp.max_length = 10000000
  6. # load text from file
  7. with open("deutsch_corpus.txt", encoding='utf-8') as f:
  8. text = f.read()
  9. # parse text with NLP model
  10. doc = nlp(text)
  11. # define function to generate bot response
  12. def generate_response(user_input):
  13. # parse user input with NLP model
  14. user_doc = nlp(user_input)
  15. # check if the user input contains information that should be stored
  16. remember = False
  17. remember_text = ""
  18. for sent in user_doc.sents:
  19. 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"]):
  20. remember = True
  21. remember_text = sent.text
  22. break
  23. # If the user input contains information to remember, store it in the memory file
  24. if remember:
  25. remember_text = remember_text.lower()
  26. with open("german_corpus.txt", "a", encoding='utf-8') as f:
  27. 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")
  28. return "Ok, I'll remember that!"
  29. # search for sentences in the corpus that are similar to the user input
  30. similarity_scores = []
  31. for sentence in doc.sents:
  32. score = sentence.similarity(user_doc)
  33. similarity_scores.append((sentence, score))
  34. # sort sentences by similarity score
  35. similarity_scores.sort(key=lambda x: x[1], reverse=True)
  36. # if no similar sentence is found, a random answer is returned
  37. if similarity_scores[0][1] < 0.5:
  38. return "Sorry, I don't understand."
  39. # Return the most similar sentence as a bot response.
  40. return similarity_scores[0][0].text

... and my HTML Code:

  1. <!DOCTYPE html>
  2. <html lang="de">
  3. <head>
  4. <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>TalkyAI</title>
  7. </head>
  8. <body>
  9. <header>
  10. <nav>
  11. <ul>
  12. <li><a href="#">KI trainieren</a></li>
  13. <li><a href="#" class="active">Chat</a></li>
  14. <li><a href="#">Account</a></li>
  15. </ul>
  16. <div class="logout-btn"><a href="#">Abmelden</a></div>
  17. </nav>
  18. </header>
  19. <main>
  20. <div id="chat-container">
  21. <div id="chat-output"></div>
  22. <div id="chat-input">
  23. <input type="text" id="message-input" placeholder="Gib deine Nachricht ein...">
  24. <button id="send-btn">Senden</button>
  25. </div>
  26. </div>
  27. </main>
  28. <script>
  29. </script>
  30. </body>
  31. </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.

英文:

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:

  1. import spacy
  2. import random
  3. # load NLP model
  4. nlp = spacy.load(&quot;en_core_news_sm&quot;)
  5. nlp.max_length = 10000000
  6. # load text from file
  7. with open(&quot;deutsch_corpus.txt&quot;, encoding=&#39;utf-8&#39;) as f:
  8. text = f.read()
  9. # parse text with NLP model
  10. doc = nlp(text)
  11. # define function to generate bot response
  12. def generate_response(user_input):
  13. # parse user input with NLP model
  14. user_doc = nlp(user_input)
  15. # check if the user input contains information that should be stored
  16. remember = False
  17. remember_text = &quot;&quot;
  18. for sent in user_doc.sents:
  19. if any(word in sent.text.lower() for word in [&quot;I am&quot;, &quot;my friend&quot;, &quot;my girlfriend&quot;, &quot;I was&quot;, &quot;I have&quot;, &quot;I have&quot;, &quot;my mother is&quot;, &quot;my mother is&quot;, &quot;I would be&quot;, &quot;I find&quot;, &quot;my name is&quot;]):
  20. remember = True
  21. remember_text = sent.text
  22. break
  23. # If the user input contains information to remember, store it in the memory file
  24. if remember:
  25. remember_text = remember_text.lower()
  26. with open(&quot;german_corpus.txt&quot;, &quot;a&quot;, encoding=&#39;utf-8&#39;) as f:
  27. f.write(remember_text.replace(&quot;ich bin&quot;, &quot;Du bist&quot;).replace(&quot;mein&quot;, &quot;Dein&quot;).replace(&quot;meine&quot;, &quot;Deine&quot;).replace(&quot;meine mutter ist&quot;, &quot;deine Mutter ist&quot;).replace(&quot;meine freundin ist&quot;, &quot;deine Freundin ist&quot;). replace(&quot;would have&quot;, &quot;would have&quot;).replace(&quot;have&quot;, &quot;have&quot;).replace(&quot;have&quot;, &quot;have&quot;).replace(&quot;I would be&quot;, &quot;you would be&quot;).replace(&quot;I find&quot;, &quot;you find&quot;).replace(&quot;my name&quot;, &quot;your name is&quot;).replace(&quot;my name is&quot;, &quot;your name is&quot;) + &quot;\n&quot;)
  28. return &quot;Ok, I&#39;ll remember that!&quot;
  29. # search for sentences in the corpus that are similar to the user input
  30. similarity_scores = []
  31. for sentence in doc.sents:
  32. score = sentence.similarity(user_doc)
  33. similarity_scores.append((sentence, score))
  34. # sort sentences by similarity score
  35. similarity_scores.sort(key=lambda x: x[1], reverse=True)
  36. # if no similar sentence is found, a random answer is returned
  37. if similarity_scores[0][1] &lt; 0.5:
  38. return &quot;Sorry, I don&#39;t understand.&quot;
  39. # Return most similar sentence as bot response.
  40. return similarity_scores[0][0].text

... and my HTML Code:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;de&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta http-equiv=&quot;Content-Security-Policy&quot; content=&quot;upgrade-insecure-requests&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;TalkyAI&lt;/title&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;header&gt;
  10. &lt;nav&gt;
  11. &lt;ul&gt;
  12. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;KI trainieren&lt;/a&gt;&lt;/li&gt;
  13. &lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;active&quot;&gt;Chat&lt;/a&gt;&lt;/li&gt;
  14. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Account&lt;/a&gt;&lt;/li&gt;
  15. &lt;/ul&gt;
  16. &lt;div class=&quot;logout-btn&quot;&gt;&lt;a href=&quot;#&quot;&gt;Abmelden&lt;/a&gt;&lt;/div&gt;
  17. &lt;/nav&gt;
  18. &lt;/header&gt;
  19. &lt;main&gt;
  20. &lt;div id=&quot;chat-container&quot;&gt;
  21. &lt;div id=&quot;chat-output&quot;&gt;&lt;/div&gt;
  22. &lt;div id=&quot;chat-input&quot;&gt;
  23. &lt;input type=&quot;text&quot; id=&quot;message-input&quot; placeholder=&quot;Gib deine Nachricht ein...&quot;&gt;
  24. &lt;button id=&quot;send-btn&quot;&gt;Senden&lt;/button&gt;
  25. &lt;/div&gt;
  26. &lt;/div&gt;
  27. &lt;/main&gt;
  28. &lt;script&gt;
  29. &lt;/script&gt;
  30. &lt;/body&gt;
  31. &lt;/html&gt;

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.

答案1

得分: 1

将您的Spacy聊天机器人集成到您的网站中的一种方法是使用Flask(一个Python Web框架)创建服务器端API,并从您网站的JavaScript代码中对API进行AJAX请求。

  1. from flask import Flask, request, jsonify
  2. import spacy
  3. app = Flask(__name__)
  4. @app.route('/api/chatbot', methods=['POST'])
  5. def chatbot():
  6. #您的代码在此处
  7. return jsonify({'response': response})
  8. if __name__ == '__main__':
  9. app.run(debug=True)

请注意,上述代码部分未被翻译。

英文:

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.

  1. from flask import Flask, request, jsonify
  2. import spacy
  3. app = Flask(__name__)
  4. @app.route(&#39;/api/chatbot&#39;, methods=[&#39;POST&#39;])
  5. def chatbot():
  6. #your code here
  7. return jsonify({&#39;response&#39;: response})
  8. if __name__ == &#39;__main__&#39;:
  9. app.run(debug=True)

答案2

得分: 0

目前,spaCy似乎只支持Python。spacy-js仅提供了JavaScript接口,但在底层仍然使用Python。我似乎找不到其他选项,除了为您的Web服务器创建一个Python API,这似乎是不可行的。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月10日 00:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定