from flask import Flask, jsonify, render_template, request, make_response import transformers from huggingface_hub import cached_download import torch from torch import nn import re import numpy as np import pandas as pd from collections import OrderedDict # import requests # from bs4 import BeautifulSoup app = Flask(__name__) # create a python dictionary for your models d = {: , : , ..., : } model_url = "nlptown/bert-base-multilingual-uncased-sentiment" model_path = cached_download(model_url) tokenizer = AutoTokenizer.from_pretrained(model_path) # Load model using transformers.pipeline model = transformers.pipeline(task="sentiment-analysis", model=model_path, tokenizer=tokenizer) dictOfModels = {"BERT" : model} # create a list of keys to use them in the select part of the html code listOfKeys = [] for key in dictOfModels : listOfKeys.append(key) def get_prediction(message,model): # inference results = model(message) return results @app.route('/', methods=['GET']) def get(): # in the select we will have each key of the list in option return render_template("home.html", len = len(listOfKeys), listOfKeys = listOfKeys) @app.route('/', methods=['POST']) def predict(): message = "This is good movies" #request.form['message'] # choice of the model results = get_prediction(message, dictOfModels['BERT']) # get_prediction(message, dictOfModels['request.form.get("model_choice")']) print(f'User selected model : {request.form.get("model_choice")}') my_prediction = f'The feeling of this text is {results[0]["label"]} with probability of {results[0]["score"]*100}%.' return render_template('result.html', text = f'{message}', prediction = my_prediction) # @app.route('/') # def home(): # print(1) # return {'key':"Hello HuggingFace! Successfully deployed. "} # # model = load_checkpoint('checkpoint.pth') # # print(2) # # res = sample(model, obj.maxlen, 'ap') # # print(3) # # return {'key':res}