File size: 6,645 Bytes
f2abd03
 
 
8eb0c1a
 
f2abd03
 
551aa70
f2abd03
 
 
 
802b789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
551aa70
501420e
551aa70
 
 
 
 
 
 
 
802b789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2abd03
 
 
 
 
802b789
 
 
 
 
 
f2abd03
 
8561ed2
484e9ae
 
8eb0c1a
802b789
f2abd03
8eb0c1a
f2abd03
 
 
8561ed2
f2abd03
 
8561ed2
f2abd03
 
 
 
8eb0c1a
f2abd03
 
 
8561ed2
 
f2abd03
802b789
 
 
f2abd03
8eb0c1a
 
 
 
 
 
f2abd03
 
 
 
8eb0c1a
 
 
 
 
 
 
 
 
f2abd03
 
 
 
 
 
 
 
 
 
802b789
 
 
 
 
 
 
 
 
f2abd03
802b789
 
8eb0c1a
f2abd03
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from typing import Callable
import gradio as gr

from src.scraper.generic_scraper import GenericScraper

if gr.NO_RELOAD:
    import numpy as np
    from src.model import BaseTransferLearningModel


DEVICE = 'cpu'
MODELS = [
    # (
    #     'bert-model_1950',
    #     lambda: BaseTransferLearningModel(
    #         'bert-base-uncased',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/bert-model_1950.pt',
    #     ),
    # ),
    # (
    #     'bert-model_2000',
    #     lambda: BaseTransferLearningModel(
    #         'bert-base-uncased',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/bert-model_2000.pt',
    #     ),
    # ),
    # (
    #     'deberta-base-model_1100',
    #     lambda: BaseTransferLearningModel(
    #         'microsoft/deberta-base',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/deberta-base-model_4400.pt',
    #     ),
    # ),
    # (
    #     'deberta-base-model_2000',
    #     lambda: BaseTransferLearningModel(
    #         'microsoft/deberta-base',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/deberta-base-model_8000.pt',
    #     ),
    # ),
    # (
    #     'deberta-v3-base-model_1700',
    #     lambda: BaseTransferLearningModel(
    #         'microsoft/deberta-v3-base',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/deberta-v3-base-model_3400.pt',
    #     ),
    # ),
    (
        'deberta-v3-base-model_2000',
        lambda: BaseTransferLearningModel(
            'microsoft/deberta-v3-base',
            [('linear', ['in', 'out']), ('softmax')],
            2,
            device=DEVICE,
            state_dict='src/ckpt/deberta-v3-base-model_4000.pt',
        ),
    ),
    # (
    #     'distilbert-model_1850',
    #     lambda: BaseTransferLearningModel(
    #         'distilbert-base-uncased',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/distilbert-model_1850.pt',
    #     ),
    # ),
    # (
    #     'distilbert-model_2000',
    #     lambda: BaseTransferLearningModel(
    #         'distilbert-base-uncased',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/distilbert-model_2000.pt',
    #     ),
    # ),
    # (
    #     'roberta-base-model_1250',
    #     lambda: BaseTransferLearningModel(
    #         'FacebookAI/roberta-base',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/roberta-base-model_1250.pt',
    #     ),
    # ),
    # (
    #     'roberta-base-model_2000',
    #     lambda: BaseTransferLearningModel(
    #         'FacebookAI/roberta-base',
    #         [('linear', ['in', 'out']), ('softmax')],
    #         2,
    #         device=DEVICE,
    #         state_dict='src/ckpt/roberta-base-model_2000.pt',
    #     ),
    # ),
]


class WebUI:

    def __init__(
        self,
        models: list[(str, Callable)] = [],
        device: str = 'cpu',
        debug: bool = False,
    ) -> None:
        self.models = models
        self.device = device
        self.is_ready = False
        self.model = self.models[0][1]()
        self.is_ready = True
        self.scraper = GenericScraper()
        self.debug = debug

    def _change_model(self, idx: int) -> str:
        if gr.NO_RELOAD:
            try:
                print(self.models[idx])
                self.is_ready = False
                del self.model
                self.model = self.models[idx][1]()
                self.is_ready = True
                print('done loading')
            except Exception as e:
                print(e)
                gr.Error(e)
            return self.models[idx][0]

    def _predict(self, text: str) -> str:
        print(text)
        if self.is_ready == False:
            return 'Model is not yet ready!'
        output = self.model.predict(text, self.device).detach().cpu().numpy()[0]
        if self.debug:
            return f'Fake: {output[0]:.10f}, Real: {output[1]:.10f}'
        return f'We think that this is a {"fake" if output[0] > output[1] else "real"} news article with {max(output[0], output[1]) * 100:.2f}% certainty.'

    def _scrape(self, url: str) -> str:
        try:
            return self.scraper.scrape(url)
        except Exception as e:
            return str(e)

    def get_ui(self) -> None:
        with gr.Blocks() as ui:
            with gr.Row():
                with gr.Column():
                    t_url = gr.Textbox(label='URL')
                    with gr.Row():
                        btn_scrape_reset = gr.ClearButton(
                            value='Reset',
                            components=[
                                t_url,
                            ],
                        )
                        btn_scrape = gr.Button(value='Get From URL', variant='primary')
                    t_inp = gr.Textbox(label='Input')
                    with gr.Row():
                        btn_reset = gr.ClearButton(
                            value='Reset',
                            components=[
                                t_inp,
                            ],
                        )
                        btn_submit = gr.Button(value='Submit', variant='primary')
                with gr.Column():
                    if self.debug:
                        ddl_model = gr.Dropdown(
                            label='Model',
                            choices=[model[0] for model in self.models],
                            value=self._change_model(0),
                            type='index',
                            interactive=True,
                            filterable=True,
                        )
                    t_out = gr.Textbox(label='Output')
            if self.debug:
                ddl_model.change(fn=self._change_model, inputs=ddl_model)
            btn_scrape.click(fn=self._scrape, inputs=t_url, outputs=t_inp)
            btn_submit.click(fn=self._predict, inputs=t_inp, outputs=t_out)
        return ui


webui = WebUI(models=MODELS, device=DEVICE).get_ui()

if __name__ == '__main__':
    webui.launch()