yozozaya commited on
Commit
ecb9008
·
1 Parent(s): e005139

test new approach

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