Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import inspect | |
| from gradio import routes | |
| from typing import List, Type | |
| 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 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 | |
| def hallo(x): | |
| return f"{text2int(x)}" | |
| def hadet(x): | |
| return f"{text2int(x)}" | |
| with gr.Blocks() as blk: | |
| gr.Markdown("# Gradio Blocks (3.0) with REST API") | |
| t = gr.Textbox() | |
| b = gr.Button("Hallo") | |
| a = gr.Button("Hadet") | |
| o = gr.Textbox() | |
| b.click(hallo, inputs=[t], outputs=[o]) | |
| a.click(hadet, inputs=[t], outputs=[o]) | |
| gr.Markdown(""" | |
| ## API | |
| Can select which function to use by passing in `fn_index`: | |
| ```python | |
| import requests | |
| requests.post( | |
| url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 0} | |
| ).json() | |
| requests.post( | |
| url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1} | |
| ).json() | |
| ``` | |
| Or using cURL | |
| ``` | |
| $ curl -X POST https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/ -H 'Content-Type: application/json' -d '{"data": ["Jessie"], "fn_index": 0}' | |
| $ curl -X POST https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/ -H 'Content-Type: application/json' -d '{"data": ["Jessie"], "fn_index": 1}' | |
| ```""") | |
| ifa = gr.Interface(lambda: None, inputs=[t], outputs=[o]) | |
| blk.input_components = ifa.input_components | |
| blk.output_components = ifa.output_components | |
| blk.examples = None | |
| blk.predict_durations = [] | |
| bapp = blk.launch() | |