kai47 commited on
Commit
a4d63e4
·
1 Parent(s): 1f8a8ae

Add application file

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import inspect
3
+ from gradio import routes
4
+ from typing import List, Type
5
+
6
+ # Monkey patch
7
+ def get_types(cls_set: List[Type], component: str):
8
+ docset = []
9
+ types = []
10
+ if component == "input":
11
+ for cls in cls_set:
12
+ doc = inspect.getdoc(cls)
13
+ doc_lines = doc.split("\n")
14
+ docset.append(doc_lines[1].split(":")[-1])
15
+ types.append(doc_lines[1].split(")")[0].split("(")[-1])
16
+ else:
17
+ for cls in cls_set:
18
+ doc = inspect.getdoc(cls)
19
+ doc_lines = doc.split("\n")
20
+ docset.append(doc_lines[-1].split(":")[-1])
21
+ types.append(doc_lines[-1].split(")")[0].split("(")[-1])
22
+ return docset, types
23
+ routes.get_types = get_types
24
+
25
+ # App code
26
+ def hallo(x):
27
+ return f"Hallo, {x}"
28
+
29
+ def hadet(x):
30
+ return f"Hadet, {x}"
31
+
32
+ with gr.Blocks() as blk:
33
+ gr.Markdown("# Gradio Blocks (3.0) with REST API")
34
+ t = gr.Textbox()
35
+ b = gr.Button("Hallo")
36
+ a = gr.Button("Hadet")
37
+ o = gr.Textbox()
38
+ b.click(hallo, inputs=[t], outputs=[o])
39
+ a.click(hadet, inputs=[t], outputs=[o])
40
+ gr.Markdown("""
41
+ ## API
42
+ Can select which function to use by passing in `fn_index`:
43
+ ```python
44
+ import requests
45
+ requests.post(
46
+ url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 0}
47
+ ).json()
48
+ requests.post(
49
+ url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1}
50
+ ).json()
51
+ ```
52
+ Or using cURL
53
+ ```
54
+ $ 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}'
55
+ $ 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}'
56
+ ```""")
57
+
58
+ ifa = gr.Interface(lambda: None, inputs=[t], outputs=[o])
59
+
60
+ blk.input_components = ifa.input_components
61
+ blk.output_components = ifa.output_components
62
+ blk.examples = None
63
+ blk.predict_durations = []
64
+
65
+ bapp = blk.launch()