2
0

More than 1 year has passed since last update.

gradioのgr.Interfaceについて

Posted at

はじめに

gradioを使えば簡単な機械学習のデモアプリが作れます.
公式のドキュメント
https://www.gradio.app/docs/
公式のクイックスタート
https://gradio.app/quickstart/

上記のサイトを見れば分かると思いますが,ここでは自分の解釈を書いていきます.

目次

  1. gr.Interfaceについて
  2. 複数入力のとき
  3. 参考文献

gr.Interfaceについて

gradioを扱う上で一番大事なところかつ,ここさえ分かっていればOKなところです.
必要な引数が,fn,inputs,outputsの3つあります.

下のコードは,入力が画像,出力がラベルである,画像の識別問題です.(公式サイトにあるコード)
今回は識別はせずに固定値を出力しているようにしています.

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
demo.launch()

簡単に書くと,fnという関数にinputsの値を引数として渡して,その出力をoutputsとするよみたいな感じです.
そのとき,inputsの型とoutputsの型を事前に決めておいてねという感じです.
型が決まらないと,UIの方の入力ボックスが決まらないからですね.

def fn(inputs):
    return outputs

outputs = fn(inputs)

複数入力のとき

入力が複数のときは,inputs=[]にするといいです.
出力が複数あるときも同じくoutputs=[]でいいです.
inputsの中身順番と,fnの引数の順番は通じているから,合わせるようにしないといけません.

import gradio as gr

def greet(firstname, lastname):
    name = firstname + ' ' +lastname
    return name, len(firstname + lastname)

demo = gr.Interface(
    fn=greet,
    inputs=["text", "text"],
    outputs=["text", "text"],
)
demo.launch()

参考文献

No 内容 URL
1 公式のドキュメント https://www.gradio.app/docs/
2 公式のクイックスタート https://gradio.app/quickstart/
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0