yozozaya commited on
Commit
ce74bb4
·
1 Parent(s): 5f9d5c0

test as an API

Browse files
Files changed (1) hide show
  1. app.py +88 -19
app.py CHANGED
@@ -1,19 +1,88 @@
1
- import gradio
2
-
3
- def my_inference_function(name):
4
- return "Hello " + name + "!"
5
-
6
- gradio_interface = gradio.Interface(
7
- fn=my_inference_function,
8
- inputs="text",
9
- outputs="text",
10
- examples=[
11
- ["Jill"],
12
- ["Sam"]
13
- ],
14
- title="REST API with Gradio and Huggingface Spaces",
15
- description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
16
- article="Test 2023"
17
- )
18
-
19
- gradio_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
46
+ requests.post(
47
+ url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 0}
48
+ ).json()
49
+ requests.post(
50
+ url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1}
51
+ ).json()
52
+ ```
53
+
54
+ Or using cURL
55
+
56
+ ```
57
+ $ 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}'
58
+ $ 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}'
59
+ ```""")
60
+
61
+ ifa = gr.Interface(lambda: None, inputs=[t], outputs=[o])
62
+
63
+ blk.input_components = ifa.input_components
64
+ blk.output_components = ifa.output_components
65
+ blk.examples = None
66
+ blk.predict_durations = []
67
+
68
+ bapp = blk.launch()
69
+
70
+ # import gradio
71
+
72
+ # def my_inference_function(name):
73
+ # return "Hello " + name + "!"
74
+
75
+ # gradio_interface = gradio.Interface(
76
+ # fn=my_inference_function,
77
+ # inputs="text",
78
+ # outputs="text",
79
+ # examples=[
80
+ # ["Jill"],
81
+ # ["Sam"]
82
+ # ],
83
+ # title="REST API with Gradio and Huggingface Spaces",
84
+ # description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
85
+ # article="Test 2023"
86
+ # )
87
+
88
+ # gradio_interface.launch()