File size: 2,813 Bytes
1a44e19
 
 
 
1e18c59
1a44e19
 
 
 
 
 
1e18c59
 
1a44e19
1e18c59
880d29c
1a44e19
2ab3c91
1e18c59
392fb47
 
 
1a44e19
 
 
 
392fb47
1a44e19
 
 
1e18c59
 
 
1a44e19
1e18c59
880d29c
1a44e19
1b5593d
1e18c59
1b5593d
 
392fb47
1b5593d
 
 
 
1e18c59
1a44e19
 
880d29c
 
 
1e18c59
 
 
 
 
392fb47
 
 
b65cb21
880d29c
 
 
 
1e18c59
6a9f3f1
880d29c
b65cb21
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
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import tensorflow as tf
from sklearn.preprocessing import LabelEncoder
import spacy
import csv
from timeit import default_timer as timer
from helper_functions import preprocess_single_sentence

with open('examples.txt', 'r') as f:
    example_list = [example.strip() for example in f.readlines()]

with open('label_names.txt', 'r') as f:
    labels = [label.strip() for label in f.readlines()]

encoder = LabelEncoder()
encoder.fit(labels)

model = tf.keras.models.load_model('models/gru_model.keras')

nlp = spacy.load("en_core_web_sm")

def predict(text, flagged=False):
    """
    Make predictions on the given text using the trained model.
    Args:
        text (str): The text to make predictions on.
        flagged (bool): Indicates whether the input was flagged.
    Returns:
        list: A list of predictions.
    """
    start_time = timer()
    processed_text = preprocess_single_sentence(text)
    text_tensor = tf.expand_dims(processed_text, 0)

    probability = model.predict(text_tensor)
    pred_label_with_prob = {labels[i]: float(probability[0][i]) for i in range(len(labels))}
    pred_time = round(timer() - start_time, 5)
    field_names = ['Text', 'Label']

    with open('new_text.txt', 'a') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=field_names)

        # Write header if file is empty
        if csvfile.tell() == 0:
            writer.writeheader()
        writer.writerow({'Text': text, 'Label': encoder.inverse_transform([probability.argmax()])[0]})

    return pred_label_with_prob, pred_time

# Gradio App
inputs = gr.Textbox(lines=5, label="Enter text",
                    placeholder="i like to have the same breathless feeling as a reader eager to see what will happen next")
outputs = [
    gr.Label(num_top_classes=len(labels), label="Predictions"),
    gr.Number(label="Prediction time (s)"),
]
title = 'Sentiment Analysis 🤣😱😡😢'
description = "The sentiment analysis model is a deep learning-based natural language processing (NLP) model designed to analyze and classify the sentiment expressed in text data. It is trained to understand the emotional tone of text and categorize it into predefined sentiment categories such as <b>anger, fear, sadness, and joy.<b><br><br>\
<b>WARNING:</b> Your input will be saved for improving model accuracy. If you believe the model's prediction is incorrect, please click the 'Flag' button.<br>\
<b>Tip:</b> If you assume the output is incorrect, click the 'Flag' button to notify us."

demo = gr.Interface(fn=predict,
                    inputs=inputs,
                    outputs=outputs,
                    title=title,
                    description=description,
                    examples=example_list,
                    allow_flagging=True) 
demo.launch(share= True)