<jupyter_start><jupyter_text>- Note that the whole dataset does not need to be stemmed again and again- I will store the stemmed data and then as and when I get a query I would append the stemmed query at the end of the data set after each prediction<jupyter_code>trainer = Train_Diagnosis()
trainer.train_model()
# get predictions needs to be a separate function as it just needs to get the 
# predictions 
from flask import Flask, request, jsonify
from flask_cors import CORS 

## 1. prediction 
## moreover, model and vectorizer need not be loaded again and again

# build 1 end point
app = Flask(__name__)
CORS(app)


def load_from_pickle(file):
    loaded = pickle.load(open(file,'rb'))
    return loaded

# got the models     
vectorizer = load_from_pickle('models/vectorizer.pkl')
model = load_from_pickle('models/model.pkl')

trainer = Train_Diagnosis()
ailments = trainer.get_ailments(1)
diagnoser = Predictions(model,'data/trial_data.csv')
# parameter 1 -> 
    # processes the query given by the site and make prediction   
    
@app.route('/process',methods = ['GET'])
def get_diagnosis():
    q = request.args.get('query')
    processed = diagnoser.process_query(q)
    
    # now transform
    query = [processed]
    query = vectorizer.transform(query)
    
    # and predict 
    preds = model.predict_proba(query)
    res = list(np.argsort(preds))[0]
    res = res[::-1][:3] # top 3 
    ailment_top = ailments[res[0]]
    
    # append record to the data
    diagnoser.append_query(query,ailment_top)
    
    #gather predictions 
    predictions = []
    for k in res: 
        predictions.append(ailments[k])

# parameter point 2 -> 
    # re-trains the model on the acquired data and 
    # reloads model and vectorizer 
    
    # 0 -> do not train 
    # 1 -> train again 
    train = int(request.args.get('train')) # ->
    if(train is not None and train == 1):
        # means I need to train along with this query 
        trainer.train_model()
        #at this point load the vectorizer and model again with the new queries
        vectorizer = load_from_pickle('models/vectorizer.pkl')
        model = load_from_pickle('models/model.pkl')
    
    return jsonify(predictions)


if __name__=='__main__':
    app.run(port = 5000, debug = True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09525880817746847, 'fraction_numerical': 0.007394519356241845, 'mean_word_length': 3.291044776119403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '37953', 'license': 'MIT', 'n_tokens_mistral': 741, 'n_tokens_neox': 704, 'n_words': 231, 'path': 'EDA/Diagnosis.ipynb', 'repo_name': 'TheGupta2012/backend-MedicalDiagnosis', 'size': '2299'}