Spaces:
Runtime error
Runtime error
hobs
commited on
Commit
·
20c813e
1
Parent(s):
a09a83f
rename vars and fix docs
Browse files
app.py
CHANGED
|
@@ -2,6 +2,10 @@ import gradio as gr
|
|
| 2 |
import inspect
|
| 3 |
from gradio import routes
|
| 4 |
from typing import List, Type
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def normalize_text(text):
|
|
@@ -49,7 +53,7 @@ def text2int(text, numwords={}):
|
|
| 49 |
result += current
|
| 50 |
current = 0
|
| 51 |
|
| 52 |
-
return result + current
|
| 53 |
|
| 54 |
|
| 55 |
def get_types(cls_set: List[Type], component: str):
|
|
@@ -73,48 +77,38 @@ def get_types(cls_set: List[Type], component: str):
|
|
| 73 |
routes.get_types = get_types
|
| 74 |
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
return f"{text2int(x)}"
|
| 82 |
|
|
|
|
| 83 |
|
| 84 |
-
with gr.Blocks() as blk:
|
| 85 |
-
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
| 86 |
-
t = gr.Textbox()
|
| 87 |
-
b = gr.Button("Hallo")
|
| 88 |
-
a = gr.Button("Hadet")
|
| 89 |
-
o = gr.Textbox()
|
| 90 |
-
b.click(hallo, inputs=[t], outputs=[o])
|
| 91 |
-
a.click(hadet, inputs=[t], outputs=[o])
|
| 92 |
-
gr.Markdown("""
|
| 93 |
-
## API
|
| 94 |
-
Can select which function to use by passing in `fn_index`:
|
| 95 |
```python
|
| 96 |
import requests
|
| 97 |
|
| 98 |
requests.post(
|
| 99 |
-
url="https://
|
| 100 |
-
).json()
|
| 101 |
-
requests.post(
|
| 102 |
-
url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1}
|
| 103 |
).json()
|
| 104 |
```
|
| 105 |
|
| 106 |
-
Or using
|
| 107 |
|
|
|
|
|
|
|
| 108 |
```
|
| 109 |
-
|
| 110 |
-
$ 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}'
|
| 111 |
-
```""")
|
| 112 |
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
-
bapp =
|
|
|
|
| 2 |
import inspect
|
| 3 |
from gradio import routes
|
| 4 |
from typing import List, Type
|
| 5 |
+
"""
|
| 6 |
+
>>> !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}'
|
| 7 |
+
{"data":["123"],"duration":0.00019359588623046875,"average_duration":0.00019359588623046875}
|
| 8 |
+
"""
|
| 9 |
|
| 10 |
|
| 11 |
def normalize_text(text):
|
|
|
|
| 53 |
result += current
|
| 54 |
current = 0
|
| 55 |
|
| 56 |
+
return str(result + current)
|
| 57 |
|
| 58 |
|
| 59 |
def get_types(cls_set: List[Type], component: str):
|
|
|
|
| 77 |
routes.get_types = get_types
|
| 78 |
|
| 79 |
|
| 80 |
+
with gr.Blocks() as html_block:
|
| 81 |
+
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
| 82 |
+
textbox = gr.Textbox()
|
| 83 |
+
button = gr.Button("text2int")
|
| 84 |
+
output = gr.Textbox()
|
| 85 |
+
button.click(text2int, inputs=[textbox], outputs=[output])
|
| 86 |
+
gr.Markdown(r"""
|
| 87 |
|
| 88 |
+
## API
|
|
|
|
| 89 |
|
| 90 |
+
You can select which function to run using the `fn_index` argument:
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
```python
|
| 93 |
import requests
|
| 94 |
|
| 95 |
requests.post(
|
| 96 |
+
url="https://Hobson-gradio-rest-api.hf.space/api/predict/", json={"data": ["one hundred forty-two"], "fn_index": 0}
|
|
|
|
|
|
|
|
|
|
| 97 |
).json()
|
| 98 |
```
|
| 99 |
|
| 100 |
+
Or using `curl`:
|
| 101 |
|
| 102 |
+
```bash
|
| 103 |
+
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}'
|
| 104 |
```
|
| 105 |
+
""")
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
interface = gr.Interface(lambda: None, inputs=[textbox], outputs=[output])
|
| 108 |
|
| 109 |
+
html_block.input_components = interface.input_components
|
| 110 |
+
html_block.output_components = interface.output_components
|
| 111 |
+
html_block.examples = None
|
| 112 |
+
html_block.predict_durations = []
|
| 113 |
|
| 114 |
+
bapp = html_block.launch()
|