Spaces:
Build error
Build error
Added API names
Browse files
app.py
CHANGED
|
@@ -148,8 +148,8 @@ with gr.Blocks() as html_block:
|
|
| 148 |
value="42",
|
| 149 |
label="Output integer:"
|
| 150 |
)
|
| 151 |
-
button_text2int.click(try_text2int, inputs=[textbox_input], outputs=[textbox_output])
|
| 152 |
-
button_text2int_preprocessed.click(try_text2int_preprocessed, inputs=[textbox_input], outputs=[textbox_output])
|
| 153 |
gr.Markdown(r"""
|
| 154 |
## API
|
| 155 |
You can select which function to run using the `fn_index` argument:
|
|
|
|
| 148 |
value="42",
|
| 149 |
label="Output integer:"
|
| 150 |
)
|
| 151 |
+
button_text2int.click(try_text2int, inputs=[textbox_input], outputs=[textbox_output], api_name="text2int")
|
| 152 |
+
button_text2int_preprocessed.click(try_text2int_preprocessed, inputs=[textbox_input], outputs=[textbox_output], api_name="text2int_preprocessed")
|
| 153 |
gr.Markdown(r"""
|
| 154 |
## API
|
| 155 |
You can select which function to run using the `fn_index` argument:
|
app2.py
DELETED
|
@@ -1,157 +0,0 @@
|
|
| 1 |
-
import inspect
|
| 2 |
-
import json
|
| 3 |
-
import logging
|
| 4 |
-
import os
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from gradio import routes
|
| 7 |
-
import spacy # noqa
|
| 8 |
-
from typing import List, Type
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
|
| 11 |
-
load_dotenv()
|
| 12 |
-
|
| 13 |
-
TOKENS2INT_ERROR_INT = 32202
|
| 14 |
-
|
| 15 |
-
log = logging.getLogger()
|
| 16 |
-
|
| 17 |
-
ONES = [
|
| 18 |
-
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
|
| 19 |
-
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
| 20 |
-
"sixteen", "seventeen", "eighteen", "nineteen",
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
# token_mapping = json.load(open('str_mapping.json'))
|
| 24 |
-
CHAR_MAPPING = {
|
| 25 |
-
"-": " ",
|
| 26 |
-
"_": " ",
|
| 27 |
-
}
|
| 28 |
-
CHAR_MAPPING.update((str(i), word) for i, word in enumerate([" " + s + " " for s in ONES]))
|
| 29 |
-
|
| 30 |
-
TOKEN_MAPPING = dict(enumerate([" " + s + " " for s in ONES]))
|
| 31 |
-
|
| 32 |
-
BQ_JSON = os.environ['BQ_JSON']
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def tokenize(text):
|
| 36 |
-
return text.split()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def detokenize(tokens):
|
| 40 |
-
return ' '.join(tokens)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def replace_tokens(tokens, token_mapping=TOKEN_MAPPING):
|
| 44 |
-
return [token_mapping.get(tok, tok) for tok in tokens]
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def replace_chars(text, char_mapping=CHAR_MAPPING):
|
| 48 |
-
return ''.join((char_mapping.get(c, c) for c in text))
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def tokens2int(tokens, numwords={}):
|
| 52 |
-
""" Convert an English str containing number words into an int
|
| 53 |
-
|
| 54 |
-
>>> text2int("nine")
|
| 55 |
-
9
|
| 56 |
-
>>> text2int("forty two")
|
| 57 |
-
42
|
| 58 |
-
>>> text2int("1 2 three")
|
| 59 |
-
123
|
| 60 |
-
"""
|
| 61 |
-
if not numwords:
|
| 62 |
-
|
| 63 |
-
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
|
| 64 |
-
|
| 65 |
-
scales = ["hundred", "thousand", "million", "billion", "trillion"]
|
| 66 |
-
|
| 67 |
-
numwords["and"] = (1, 0)
|
| 68 |
-
for idx, word in enumerate(ONES):
|
| 69 |
-
numwords[word] = (1, idx)
|
| 70 |
-
for idx, word in enumerate(tens):
|
| 71 |
-
numwords[word] = (1, idx * 10)
|
| 72 |
-
for idx, word in enumerate(scales):
|
| 73 |
-
numwords[word] = (10 ** (idx * 3 or 2), 0)
|
| 74 |
-
|
| 75 |
-
current = result = 0
|
| 76 |
-
|
| 77 |
-
for word in tokens:
|
| 78 |
-
if word not in numwords:
|
| 79 |
-
raise Exception("Illegal word: " + word)
|
| 80 |
-
|
| 81 |
-
scale, increment = numwords[word]
|
| 82 |
-
current = current * scale + increment
|
| 83 |
-
if scale > 100:
|
| 84 |
-
result += current
|
| 85 |
-
current = 0
|
| 86 |
-
|
| 87 |
-
return str(result + current)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def text2int(text):
|
| 91 |
-
return tokens2int(tokenize(replace_chars(text)))
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
def try_text2int(text):
|
| 95 |
-
text = str(text)
|
| 96 |
-
try:
|
| 97 |
-
intstr = tokens2int(tokens2int(tokenize(replace_chars(text))))
|
| 98 |
-
except Exception as e:
|
| 99 |
-
log.error(str(e))
|
| 100 |
-
log.error(f'User input: {text}')
|
| 101 |
-
intstr = TOKENS2INT_ERROR_INT
|
| 102 |
-
return str(intstr)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def try_text2int_preprocessed(text):
|
| 106 |
-
text = str(text)
|
| 107 |
-
try:
|
| 108 |
-
tokens = replace_tokens(tokenize(replace_chars(str(text))))
|
| 109 |
-
except Exception as e:
|
| 110 |
-
log.error(str(e))
|
| 111 |
-
tokens = text.split()
|
| 112 |
-
try:
|
| 113 |
-
intstr = tokens2int(tokens)
|
| 114 |
-
except Exception as e:
|
| 115 |
-
log.error(str(e))
|
| 116 |
-
intstr = str(TOKENS2INT_ERROR_INT)
|
| 117 |
-
return intstr
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def get_types(cls_set: List[Type], component: str):
|
| 121 |
-
docset = []
|
| 122 |
-
types = []
|
| 123 |
-
if component == "input":
|
| 124 |
-
for cls in cls_set:
|
| 125 |
-
doc = inspect.getdoc(cls)
|
| 126 |
-
doc_lines = doc.split("\n")
|
| 127 |
-
docset.append(doc_lines[1].split(":")[-1])
|
| 128 |
-
types.append(doc_lines[1].split(")")[0].split("(")[-1])
|
| 129 |
-
else:
|
| 130 |
-
for cls in cls_set:
|
| 131 |
-
doc = inspect.getdoc(cls)
|
| 132 |
-
doc_lines = doc.split("\n")
|
| 133 |
-
docset.append(doc_lines[-1].split(":")[-1])
|
| 134 |
-
types.append(doc_lines[-1].split(")")[0].split("(")[-1])
|
| 135 |
-
return docset, types
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
routes.get_types = get_types
|
| 139 |
-
|
| 140 |
-
with gr.Blocks() as html_block:
|
| 141 |
-
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
| 142 |
-
textbox_input = gr.Textbox(
|
| 143 |
-
value="forty-two",
|
| 144 |
-
label="Input number words:",
|
| 145 |
-
)
|
| 146 |
-
button_text2int = gr.Button("text2int")
|
| 147 |
-
button_text2int_preprocessed = gr.Button("text2int with preprocessing")
|
| 148 |
-
textbox_output = gr.Textbox(
|
| 149 |
-
value="42",
|
| 150 |
-
label="Output integer:"
|
| 151 |
-
)
|
| 152 |
-
button_text2int.click(try_text2int, inputs=[textbox_input], outputs=[textbox_output])
|
| 153 |
-
button_text2int_preprocessed.click(try_text2int_preprocessed, inputs=[textbox_input], outputs=[textbox_output])
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
interface = gr.Interface(fn=try_text2int_preprocessed, inputs=[textbox_input], outputs=[textbox_output])
|
| 157 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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