Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,87 @@
|
|
| 1 |
-
import gradio
|
| 2 |
import gradio_client
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import gradio_client
|
| 3 |
+
import inspect
|
| 4 |
+
from gradio import routes
|
| 5 |
+
from typing import List, Type
|
| 6 |
|
| 7 |
+
# Monkey patch
|
| 8 |
+
def get_types(cls_set: List[Type], component: str):
|
| 9 |
+
docset = []
|
| 10 |
+
types = []
|
| 11 |
+
if component == "input":
|
| 12 |
+
for cls in cls_set:
|
| 13 |
+
doc = inspect.getdoc(cls)
|
| 14 |
+
doc_lines = doc.split("\n")
|
| 15 |
+
docset.append(doc_lines[1].split(":")[-1])
|
| 16 |
+
types.append(doc_lines[1].split(")")[0].split("(")[-1])
|
| 17 |
+
else:
|
| 18 |
+
for cls in cls_set:
|
| 19 |
+
doc = inspect.getdoc(cls)
|
| 20 |
+
doc_lines = doc.split("\n")
|
| 21 |
+
docset.append(doc_lines[-1].split(":")[-1])
|
| 22 |
+
types.append(doc_lines[-1].split(")")[0].split("(")[-1])
|
| 23 |
+
return docset, types
|
| 24 |
+
routes.get_types = get_types
|
| 25 |
+
|
| 26 |
+
# App code
|
| 27 |
+
def mbti(x):
|
| 28 |
+
|
| 29 |
+
return 0
|
| 30 |
+
|
| 31 |
+
def chat(x):
|
| 32 |
+
result = gradio_client.predict(
|
| 33 |
+
user_input,# str representing input in 'User input' Textbox component
|
| 34 |
+
0.9, # float, representing input in 'Top-p (nucleus sampling)' Slider component
|
| 35 |
+
50, # int, representing input in 'Top-k (nucleus sampling)' Slider component
|
| 36 |
+
0.7, # float, representing input in 'Temperature' Slider component
|
| 37 |
+
25, # int, representing input in 'Max New Tokens' Slider component
|
| 38 |
+
1.2, # float, representing input in 'repetition_penalty' Slider component
|
| 39 |
+
fn_index=0
|
| 40 |
+
)
|
| 41 |
+
return result
|
| 42 |
+
|
| 43 |
+
def yn(x):
|
| 44 |
+
result = gradio_client.predict(
|
| 45 |
+
text, # str representing input in 'User input' Textbox component
|
| 46 |
+
fn_index=2
|
| 47 |
+
)
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
with gr.Blocks() as blk:
|
| 53 |
+
gr.Markdown("# Gradio Blocks (3.0) with REST API")
|
| 54 |
+
t = gr.Textbox()
|
| 55 |
+
c = gr.Button("mbti")
|
| 56 |
+
b = gr.Button("chat")
|
| 57 |
+
a = gr.Button("yn")
|
| 58 |
+
o = gr.Textbox()
|
| 59 |
+
c.click(mbti, inputs=[t], outputs=[o])
|
| 60 |
+
b.click(chat, inputs=[t], outputs=[o])
|
| 61 |
+
a.click(yn, inputs=[t], outputs=[o])
|
| 62 |
+
gr.Markdown("""
|
| 63 |
+
## API
|
| 64 |
+
Can select which function to use by passing in `fn_index`:
|
| 65 |
+
```python
|
| 66 |
+
import requests
|
| 67 |
+
requests.post(
|
| 68 |
+
url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 0}
|
| 69 |
+
).json()
|
| 70 |
+
requests.post(
|
| 71 |
+
url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1}
|
| 72 |
+
).json()
|
| 73 |
+
```
|
| 74 |
+
Or using cURL
|
| 75 |
+
```
|
| 76 |
+
$ 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}'
|
| 77 |
+
$ 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}'
|
| 78 |
+
```""")
|
| 79 |
+
|
| 80 |
+
ifa = gr.Interface(lambda: None, inputs=[t], outputs=[o])
|
| 81 |
+
|
| 82 |
+
blk.input_components = ifa.input_components
|
| 83 |
+
blk.output_components = ifa.output_components
|
| 84 |
+
blk.examples = None
|
| 85 |
+
blk.predict_durations = []
|
| 86 |
+
|
| 87 |
+
bapp = blk.launch()
|