I am building a data product (an NLP chat application) for which I am learning Flask so that the user can have a better UI to interact with my product.
I have written down the following code in Flask to get the user input and stored it in a variable.
main.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('init.html')
@app.route('/handle_data', methods = ['POST', 'GET'])
def handle_data():
userQuestion = request.form['userQuestion']
print(userQuestion)
return render_template('init.html', userQuestion = userQuestion)
if __name__ == '__main__':
app.run()
init.html
<!DOCTYPE HTML>
<html>
<body>
<form action="{{ url_for('handle_data') }}" method="post">
<input type="text" name="userQuestion">
<input type="submit">
</form>
</body>
</html>
I've handled the form data and stored it in a variable userQuestion
. I want to pass this variable to another python script which contains the code of my training model.
doc2vec_main.py
import gensim
import nltk
import numpy
from gensim import models
from gensim import utils
from gensim import corpora
from nltk.stem import PorterStemmer
ps = PorterStemmer()
sentence0 = models.doc2vec.LabeledSentence(words=[u'sampling',u'what',u'is',u'sampling'],tags=["SENT_0"])
sentence1 = models.doc2vec.LabeledSentence(words=[u'sampling',u'tell',u'me',u'about',u'sampling'],tags=["SENT_1"])
sentence2 = models.doc2vec.LabeledSentence(words=[u'elig',u'what',u'is',u'my',u'elig'],tags=["SENT_2"])
sentence3 = models.doc2vec.LabeledSentence(words=[u'eligiblity',u'limit', u'what',u'is',u'my'],tags=["SENT_3"])
sentence4 = models.doc2vec.LabeledSentence(words=[u'eligiblity',u'claim',u'how',u'much',u'can',u'I'],tags=["SENT_4"])
sentence5 = models.doc2vec.LabeledSentence(words=[u'retir',u'eligibility',u'claim',u'i',u'am',u'how',u'much',u'can',u'i'],tags=["SENT_5"])
# ... list of all the training set.
# User inputs a question
document = input("Ask a question:")
tokenized_document = list(gensim.utils.tokenize(document, lowercase = True, deacc = True))
stemmed_document = []
for w in tokenized_document:
stemmed_document.append(ps.stem(w))
sentence19 = models.doc2vec.LabeledSentence(words= stemmed_document, tags=["SENT_19"])
sentences = [sentence0,sentence1,sentence2,sentence3, sentence4, sentence5,sentence6, sentence7, sentence8, sentence9, sentence10, sentence11, sentence12, sentence13, sentence14, sentence15, sentence16, sentence17, sentence18, sentence19]
model = models.Doc2Vec(size=4, alpha=0.25, min_alpha=.025, min_count=1)
model.build_vocab(sentences)
for epoch in range(30):
model.train(sentences, total_examples=model.corpus_count, epochs =
model.iter)
model.alpha -= 0.002
model.min_alpha = model.alpha
model.save("my_model.doc2vec")
model_loaded = models.Doc2Vec.load('my_model.doc2vec')
print (model.docvecs.most_similar(["SENT_19"]))
My problem is I cannot find a way to connect doc2vec_main.py
to main.py
and pass the value of userQuestion
to the document
variable in doc2main.py
script. That is when a user inputs a question in the form and clicks submit, the value of the form get passed down to document
in doc2vec_main.py
and the remaining script runs.
I have searched a lot on the internet but it didn't help. Can you suggest me a way to do it? I'm a complete beginner in Flask so forgive me for any mistake.