File size: 2,034 Bytes
9d9c274
9abed3a
18f5113
43dcf13
 
e085476
14f5c56
 
 
24668a8
 
4393529
 
 
9abed3a
18f5113
 
 
 
 
 
 
 
9abed3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a04becc
9abed3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 = {<key>: <value>, <key>: <value>, ..., <key>: <value>}
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}