Spaces:
Sleeping
Sleeping
Update app.py
Browse files1123456456789101112131415161718192021789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import gradioimport gradio as grimport gradio_clientimport inspectfrom gradio import routesfrom typing import List, Type
gradio_client = GrClient("https://ldhldh-polyglot-ko-1-3b-peft-demo.hf.space/")
def my_inference_function(name): return "Hello " + name + "!"
gradio_interface = gradio.Interface( fn=my_inference_function, inputs="text", outputs="text", examples=[ ["Jill"], ["Sam"] ], title="REST API with Gradio and Huggingface Spaces", 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.", article="© Tom Söderlund 2022")gradio_interface.launch()# Monkey patchdef 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, typesroutes.get_types = get_types
# App codedef mbti(x):
return 0 def chat(x): result = gradio_client.predict( user_input,# 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( text, # str representing input in 'User input' Textbox component fn_index=2 ) 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("""## APICan select which function to use by passing in `fn_index`:```pythonimport requestsrequests.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_componentsblk.output_components = ifa.output_componentsblk.examples = Noneblk.predict_durations = []
bapp = blk.launch()app.py+85-19