File size: 3,395 Bytes
459027d
 
 
 
20c813e
 
 
 
459027d
6cb7f39
a09a83f
 
 
 
6cb7f39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a09a83f
6cb7f39
 
 
 
 
 
 
 
 
 
20c813e
6cb7f39
 
459027d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cb7f39
 
459027d
 
6cb7f39
20c813e
 
 
 
 
 
 
459027d
20c813e
6cb7f39
20c813e
459027d
 
 
 
 
20c813e
459027d
 
 
20c813e
459027d
20c813e
 
459027d
20c813e
459027d
20c813e
459027d
20c813e
 
 
 
459027d
20c813e
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
import gradio as gr
import inspect
from gradio import routes
from typing import List, Type
"""
>>> !curl -X POST https://Hobson-gradio-rest-api.hf.space/api/predict/ -H 'Content-Type: application/json' -d '{"data": ["one hundred twenty-three"], "fn_index": 0}'
{"data":["123"],"duration":0.00019359588623046875,"average_duration":0.00019359588623046875}
"""


def normalize_text(text):
    return text.replace('-', ' ')


def text2int(text, 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:
        units = [
            "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
            "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
            "sixteen", "seventeen", "eighteen", "nineteen",
        ]

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

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

        numwords["and"] = (1, 0)
        for idx, word in enumerate(units):
            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
    text = normalize_text(text)
    for word in text.split():
        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 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


with gr.Blocks() as html_block:
    gr.Markdown("# Gradio Blocks (3.0) with REST API")
    textbox = gr.Textbox()
    button = gr.Button("text2int")
    output = gr.Textbox()
    button.click(text2int, inputs=[textbox], outputs=[output])
    gr.Markdown(r"""

## API

You can select which function to run using the `fn_index` argument:

```python
import requests

requests.post(
    url="https://Hobson-gradio-rest-api.hf.space/api/predict/", json={"data": ["one hundred forty-two"], "fn_index": 0}
).json()
```

Or using `curl`:

```bash
curl -X POST https://Hobson-gradio-rest-api.hf.space/api/predict/ -H 'Content-Type: application/json' -d '{"data": ["one hundred forty-two"], "fn_index": 0}'
```
""")

interface = gr.Interface(lambda: None, inputs=[textbox], outputs=[output])

html_block.input_components = interface.input_components
html_block.output_components = interface.output_components
html_block.examples = None
html_block.predict_durations = []

bapp = html_block.launch()