test new approach
Browse files
app.py
CHANGED
@@ -1,88 +1,89 @@
|
|
1 |
-
import gradio
|
2 |
-
import inspect
|
3 |
-
from gradio import routes
|
4 |
-
from typing import List, Type
|
5 |
|
6 |
-
|
7 |
-
|
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 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
return f"Hadet, {x}"
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
|
70 |
-
#
|
|
|
|
|
|
|
71 |
|
72 |
-
#
|
73 |
-
# return "Hello " + name + "!"
|
74 |
|
75 |
-
#
|
76 |
-
#
|
77 |
-
#
|
78 |
-
#
|
79 |
-
|
80 |
-
#
|
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 |
|
|