File size: 1,146 Bytes
38d0ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pyexpat import model
from fastai.text.all import *
import gradio as gr

learner = load_learner('models/sentiment_analysis.pkl')

sent_dict = {
    'pos': 'Positive', 
    'neg': 'Negative',
    'neutral': 'Neutral'
}

def predict_sentiment(input_text):
    map_dict = learner.dls.categorize.vocab.o2i
    predicted_sentiment = learner.predict(input_text)[0]
    predicted_confidence = float(list(learner.predict(input_text)[2])[map_dict[learner.predict(input_text)[0]]])
    flag = False
    threshold = 0.5
    if predicted_confidence < threshold:
        predicted_sentiment = 'neutral'
        flag = True

    # return{
    #     'sentiment': sent_dict[predicted_sentiment],
    #     'confidence': predicted_confidence,
    #     'below_threshold': flag,
    # }
    return str(sent_dict[predicted_sentiment])

inputs = gr.Textbox()
outputs = "text"
title = "Sentiment Analysis"
description = "Gradio demo for Sentiment Analysis. To use it, simply upload your audio, or click one of the examples to load them. Read more at the links below."
gr.Interface(predict_sentiment, inputs, outputs, title=title, description=description).launch()