File size: 7,891 Bytes
459027d
d625a73
8c4b92d
c25d79d
90009ee
 
d625a73
8c4b92d
9b8bd50
90009ee
 
9b8bd50
 
459027d
8c4b92d
 
 
6cb7f39
d625a73
 
 
 
 
 
 
 
 
 
 
aafc5b3
d625a73
aafc5b3
d625a73
c25d79d
 
d625a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cb7f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d625a73
6cb7f39
 
 
 
 
 
 
d625a73
 
6cb7f39
 
 
 
 
 
 
 
 
20c813e
6cb7f39
 
d625a73
 
 
 
8c4b92d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d625a73
 
459027d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cb7f39
 
bf32265
 
90009ee
 
 
 
 
 
 
bf32265
05b4410
bf32265
05b4410
 
 
 
 
bf32265
05b4410
bf32265
05b4410
bf32265
05b4410
 
 
 
 
 
bf32265
05b4410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf32265
90009ee
 
 
 
bf32265
 
 
90009ee
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import inspect
import json
import logging
import os
from typing import List, Type

import gradio as gr
import spacy  # noqa
from dotenv import load_dotenv
from gradio import routes
from transformers import pipeline

load_dotenv()

TOKENS2INT_ERROR_INT = 32202

log = logging.getLogger()

ONES = [
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
    "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen",
]

# token_mapping = json.load(open('str_mapping.json'))
CHAR_MAPPING = {
    "-": " ",
    "_": " ",
}
CHAR_MAPPING.update((str(i), word) for i, word in enumerate([" " + s + " " for s in ONES]))

TOKEN_MAPPING = dict(enumerate([" " + s + " " for s in ONES]))

BQ_JSON = os.environ['BQ_JSON']


def tokenize(text):
    return text.split()


def detokenize(tokens):
    return ' '.join(tokens)


def replace_tokens(tokens, token_mapping=TOKEN_MAPPING):
    return [token_mapping.get(tok, tok) for tok in tokens]


def replace_chars(text, char_mapping=CHAR_MAPPING):
    return ''.join((char_mapping.get(c, c) for c in text))


def tokens2int(tokens, numwords={}):
    """ Convert an English str containing number words into an int
    >>> text2int("nine")
    9
    >>> text2int("forty two")
    42
    >>> text2int("1 2 three")
    123
    """
    if not numwords:

        tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

        scales = ["hundred", "thousand", "million", "billion", "trillion"]

        numwords["and"] = (1, 0)
        for idx, word in enumerate(ONES):
            numwords[word] = (1, idx)
        for idx, word in enumerate(tens):
            numwords[word] = (1, idx * 10)
        for idx, word in enumerate(scales):
            numwords[word] = (10 ** (idx * 3 or 2), 0)

    current = result = 0

    for word in tokens:
        if word not in numwords:
            raise Exception("Illegal word: " + word)

        scale, increment = numwords[word]
        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0

    return str(result + current)


def text2int(text):
    return tokens2int(tokenize(replace_chars(text)))


def try_text2int(text):
    text = str(text)
    try:
        intstr = tokens2int(tokens2int(tokenize(replace_chars(text))))
    except Exception as e:
        log.error(str(e))
        log.error(f'User input: {text}')
        intstr = TOKENS2INT_ERROR_INT
    return str(intstr)


def try_text2int_preprocessed(text):
    text = str(text)
    try:
        tokens = replace_tokens(tokenize(replace_chars(str(text))))
    except Exception as e:
        log.error(str(e))
        tokens = text.split()
    try:
        intstr = tokens2int(tokens)
    except Exception as e:
        log.error(str(e))
        intstr = str(TOKENS2INT_ERROR_INT)
    return intstr


def get_types(cls_set: List[Type], component: str):
    docset = []
    types = []
    if component == "input":
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[1].split(":")[-1])
            types.append(doc_lines[1].split(")")[0].split("(")[-1])
    else:
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[-1].split(":")[-1])
            types.append(doc_lines[-1].split(")")[0].split("(")[-1])
    return docset, types


routes.get_types = get_types

sentiment = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")


def get_sentiment(text):
    return sentiment(text)


with gr.Blocks() as html_block:
    gr.Markdown("# Rori - Mathbot")

    with gr.Tab("Text to integer"):
        inputs_text2int = [
            gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
                    value="forty two"),
        ]

        outputs_text2int = gr.Textbox(label="Output integer")

        button_text2int = gr.Button("text2int")

        button_text2int.click(
            fn=try_text2int,
            inputs=inputs_text2int,
            outputs=outputs_text2int,
            api_name="text2int",
        )

        examples_text2int = [
            "one thousand forty seven",
            "one hundred",
        ]

        gr.Examples(examples=examples_text2int, inputs=inputs_text2int)

        gr.Markdown(r"""

        ## API
        ```python
        import requests

        requests.post(
            url="https://tangibleai-mathtext.hf.space/run/text2int", json={"data": ["one hundred forty five"]}
        ).json()
        ```

        Or using `curl`:

        ```bash
        curl -X POST https://tangibleai-mathtext.hf.space/run/text2int -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'
        ```
        {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")

    with gr.Tab("Text to integer preprocessed"):
        inputs_text2int_preprocessed = [
            gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
                    value="forty two"),
        ]

        outputs_text2int_preprocessed = gr.Textbox(label="Output integer")

        button_text2int = gr.Button("text2int preprocessed")

        button_text2int.click(
            fn=try_text2int_preprocessed,
            inputs=inputs_text2int_preprocessed,
            outputs=outputs_text2int_preprocessed,
            api_name="text2int_preprocessed",
        )

        examples_text2int_preprocessed = [
            "one thousand forty seven",
            "one hundred",
        ]

        gr.Examples(examples=examples_text2int_preprocessed, inputs=inputs_text2int_preprocessed)

        gr.Markdown(r"""

        ## API
        ```python
        import requests

        requests.post(
            url="https://tangibleai-mathtext.hf.space/run/text2int_preprocessed", json={"data": ["one hundred forty five"]}
        ).json()
        ```

        Or using `curl`:

        ```bash
        curl -X POST https://tangibleai-mathtext.hf.space/run/text2int_preprocessed -H 'Content-Type: application/json' -d '{"data": ["one hundred forty five"]}'
        ```
        {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")

    with gr.Tab("Sentiment Analysis"):
        inputs_sentiment = [
            gr.Text(placeholder="Type a number as text or a sentence", label="Text to process",
                    value="I really like it!"),
        ]

        outputs_sentiment = gr.Textbox(label="Sentiment result")

        button_sentiment = gr.Button("sentiment analysis")

        button_sentiment.click(
            get_sentiment,
            inputs=inputs_sentiment,
            outputs=outputs_sentiment,
            api_name="sentiment-analysis"
        )

        examples_sentiment = [
            ["Totally agree!"],
            ["Sorry, I can not accept this!"],
        ]

        gr.Examples(examples=examples_sentiment, inputs=inputs_sentiment)

        gr.Markdown(r"""
    
        ## API
        ```python
        import requests
        
        requests.post(
            url="https://tangibleai-mathtext.hf.space/run/sentiment-analysis", json={"data": ["You are right!"]}
        ).json()
        ```
        
        Or using `curl`:
        
        ```bash
        curl -X POST https://tangibleai-mathtext.hf.space/run/sentiment-analysis -H 'Content-Type: application/json' -d '{"data": ["You are right!"]}'
        ```
        {bq_json}""" + f"{json.loads(BQ_JSON)['type']}")

# interface = gr.Interface(lambda x: x, inputs=["text"], outputs=["text"])
# html_block.input_components = interface.input_components
# html_block.output_components = interface.output_components
# html_block.examples = None

html_block.predict_durations = []

html_block.launch()