File size: 1,791 Bytes
9abed3a
 
43dcf13
 
e085476
14f5c56
 
 
24668a8
 
4393529
 
 
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
rom flask import Flask, jsonify, render_template, request, make_response
import transformers
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>}
dictOfModels = {"BERT" : transformers.pipeline('sentiment-analysis', model="nlptown/bert-base-multilingual-uncased-sentiment")}
# 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['RoBERTa') # 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}