File size: 4,030 Bytes
08db26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02249fb
 
 
 
08db26d
02249fb
08db26d
 
 
 
 
 
 
 
 
 
 
 
 
 
f6e244e
08db26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38f046a
08db26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38f046a
08db26d
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
import pandas as pd
from sentiment_analyser import RandomAnalyser, RoBERTaAnalyser, ChatGPTAnalyser
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix


def plot_bar(value_counts):
    fig, ax = plt.subplots(figsize=(6, 6))
    value_counts.plot.barh(ax=ax)
    ax.bar_label(ax.containers[0])
    plt.title('Frequency of Predictions')
    return fig


def plot_confusion_matrix(y_pred, y_true):
    cm = confusion_matrix(y_true, y_pred, normalize='true')
    fig, ax = plt.subplots(figsize=(6, 6))
    labels = []
    for label in SENTI_MAPPING.keys():
        if (label in y_pred.values) or (label in y_true.values):
            labels.append(label)
    disp = ConfusionMatrixDisplay(confusion_matrix=cm,
                                  display_labels=labels)
    disp.plot(cmap="Blues", values_format=".2f", ax=ax, colorbar=False)
    plt.title("Normalized Confusion Matrix")
    return fig


def classify(num: int):
    samples_df = df.sample(num)
    X = samples_df['Text'].tolist()
    y = samples_df['Label']
    roberta = MODEL_MAPPING[OUR_MODEL]
    y_pred = pd.Series(roberta.predict(X), index=samples_df.index)
    samples_df['Predict'] = y_pred
    bar = plot_bar(y_pred.value_counts())
    cm = plot_confusion_matrix(y_pred, y)
    plt.close()
    return samples_df, bar, cm


def analysis(Text):
    keys = []
    values = []
    for name, model in MODEL_MAPPING.items():
        keys.append(name)
        values.append(SENTI_MAPPING[model.predict([Text])[0]])
    return pd.DataFrame([values], columns=keys)


MODEL_MAPPING = {
    'Random': RandomAnalyser(),
    'RoBERTa': RoBERTaAnalyser(),
    'ChatGPT': RandomAnalyser(),
}

OUR_MODEL = 'RoBERTa'

SENTI_MAPPING = {
    'negative': '😭',
    'neutral': '😶',
    'positive': '🥰'
}

TITLE = "Sentiment Analysis on Software Engineer Texts"
DESCRIPTION = (
    "这里是第16组“睿王和他的五个小跟班”软工三迭代三模型演示页面。"
    "模型链接:[Cloudy1225/stackoverflow-roberta-base-sentiment]"
    "(https://huggingface.co/Cloudy1225/stackoverflow-roberta-base-sentiment) "
)

MAX_SAMPLES = 64

df = pd.read_csv('./SOF4423.csv')

with gr.Blocks(title=TITLE) as demo:
    gr.HTML(f"<H1>{TITLE}</H1>")
    gr.Markdown(DESCRIPTION)
    gr.HTML("<H2>Model Inference</H2>")
    gr.Markdown((
        "在左侧文本框中输入文本并按回车键,右侧将输出情感分析结果。"
        "这里我们展示了三种结果,分别是随机结果、模型结果和 ChatGPT 结果。"
    ))
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(label='Input',
                                    placeholder="Enter a positive or negative sentence here...")
        with gr.Column():
            senti_output = gr.Dataframe(type="pandas", value=[['😋', '😋', '😋']],
                                        headers=list(MODEL_MAPPING.keys()), interactive=False)
    text_input.submit(analysis, inputs=text_input, outputs=senti_output, show_progress=True)

    gr.HTML("<H2>Model Evaluation</H2>")
    gr.Markdown((
        "这里是在 StackOverflow4423 数据集上评估我们的模型。"
        "滑动 Slider,将会从 StackOverflow4423 数据集中抽样出指定数量的样本,预测其情感标签。"
        "并根据预测结果绘制标签分布图和混淆矩阵。"
    ))
    input_models = list(MODEL_MAPPING)
    input_n_samples = gr.Slider(
        minimum=4,
        maximum=MAX_SAMPLES,
        value=8,
        step=4,
        label='Number of samples'
    )

    with gr.Row():
        with gr.Column():
            bar_plot = gr.Plot(label='Predictions Frequency')
        with gr.Column():
            cm_plot = gr.Plot(label='Confusion Matrix')

    with gr.Row():
        dataframe = gr.Dataframe(type="pandas", wrap=True, headers=['Text', 'Label', 'Predict'])

    input_n_samples.change(fn=classify, inputs=input_n_samples, outputs=[dataframe, bar_plot, cm_plot])

demo.launch()