File size: 3,313 Bytes
c2f1466
722ab73
c2f1466
 
 
cf7a07e
 
464280b
7192ffe
464280b
cf7a07e
c2f1466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7a07e
6b65fd5
 
447a98e
6b65fd5
 
 
 
c2f1466
 
 
4d99237
c2f1466
 
 
 
 
 
 
 
 
 
 
4d99237
6b65fd5
c2f1466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gradio as gr
from gradio_client import Client as GrClient
import inspect
from gradio import routes
from typing import List, Type
from google_trans_new import google_translator  

import requests, os

gradio_client = GrClient(os.environ.get('GrClient_url'))
translator = google_translator(service_urls=['translate.google.com'])  
# Monkey patch
def get_types(cls_set: List[Type], component: str):
    docset = []
    types = []
    if component == "input":
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[1].split(":")[-1])
            types.append(doc_lines[1].split(")")[0].split("(")[-1])
    else:
        for cls in cls_set:
            doc = inspect.getdoc(cls)
            doc_lines = doc.split("\n")
            docset.append(doc_lines[-1].split(":")[-1])
            types.append(doc_lines[-1].split(")")[0].split("(")[-1])
    return docset, types
routes.get_types = get_types

# App code
def mbti(x):
    t = translator.translate(x, lang_src='ko', lang_dest='en')
    str_trans = re.sub('[-=+,#/\?:^.@*\"β€»~ㆍ!γ€β€˜|\(\)\[\]`\'…》\”\β€œ\’·]', '', t)
    result = gradio_client.predict(
				str_trans,	# str representing input in 'User input' Textbox component
				fn_index=2
    )
    
    return result
    
def chat(x):
    result = gradio_client.predict(
        x,# str representing input in 'User input' Textbox component
		0.9,	# float, representing input in 'Top-p (nucleus sampling)' Slider component
		50,	# int, representing input in 'Top-k (nucleus sampling)' Slider component
		0.7,	# float, representing input in 'Temperature' Slider component
		25,	# int, representing input in 'Max New Tokens' Slider component
		1.2,	# float, representing input in 'repetition_penalty' Slider component
		fn_index=0
    )
    return result

def yn(x):
    result = gradio_client.predict(
				x,	# str representing input in 'User input' Textbox component
				fn_index=1
    )
    return result

    

with gr.Blocks() as blk:
    gr.Markdown("# Gradio Blocks (3.0) with REST API")
    t = gr.Textbox()
    c = gr.Button("mbti")
    b = gr.Button("chat")
    a = gr.Button("yn")
    o = gr.Textbox()
    c.click(mbti, inputs=[t], outputs=[o])
    b.click(chat, inputs=[t], outputs=[o])
    a.click(yn, inputs=[t], outputs=[o])
    gr.Markdown("""
## API
Can select which function to use by passing in `fn_index`:
```python
import requests
requests.post(
    url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 0}
).json()
requests.post(
    url="https://hf.space/embed/versae/gradio-blocks-rest-api/+/api/predict/", json={"data": ["Jessie"], "fn_index": 1}
).json()
```
Or using cURL
```
$ 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}'
$ 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}'
```""")

ifa = gr.Interface(lambda: None, inputs=[t], outputs=[o])

blk.input_components = ifa.input_components
blk.output_components = ifa.output_components
blk.examples = None
blk.predict_durations = []

bapp = blk.launch()