2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SetUp

pip install gradio

とりあえずGUIを表示させる

import gradio as gr

# 大文字に変換する関数
def to_uppercase(input_text):
    return input_text.upper()

# Gradioインターフェイスを定義する
iface = gr.Interface(fn=to_uppercase, inputs="text", outputs="text")

# インターフェイスを起動する
iface.launch()

Inputの部分を色々と変えてみる

  1. "text" : テキストボックスへの入力を受け取ります。ユーザーが入力する文字列を処理します。
  2. "number" : 数値入力フィールドを作成します。ユーザーが数値を入力します。
  3. "slider" : スライダーを使って数値を選択します。
  4. "checkbox" : チェックボックスを作成します。ユーザーがチェックボックスをオン/オフします。
  5. "radio" : ラジオボタンから選択します。ユーザーが複数の選択肢から一つを選びます。
  6. "dropdown" : ドロップダウンリストから選択します。ユーザーがリストから一つを選びます。
  7. "date" : 日付ピッカーから日付を選択します。
  8. "image" : 画像を入力として受け取ります。ユーザーが画像をアップロードします。
  9. "sketchpad" : ユーザーがスケッチパッドに描くことができます。ユーザーの描画が画像として入力されます。
  10. "webcam" : ユーザーがウェブカメラを通じてリアルタイムの映像を入力します。
  11. "microphone" : ユーザーがマイクを通じてオーディオを録音します。

入力値を複数にしてみる

import gradio as gr

def concatenate_strings(input1, input2):
    return input1 + " " + input2

iface = gr.Interface(
    fn=concatenate_strings, 
    inputs=[gr.inputs.Textbox(), gr.inputs.Textbox()], 
    outputs="text"
)

iface.launch()

簡単なチャットボットを作ってみる

オウムガエシのチャットぼっとを作る

import gradio as gr

conversations = []

def echo_bot(user_input):
    response = user_input  # ボットはユーザーの入力をそのまま返します
    conversations.append({"user": user_input, "bot": response})

    # 会話履歴を組み立てます
    conversation_history = ""
    for conversation in conversations:
        conversation_history += f'User: {conversation["user"]}\n'
        conversation_history += f'Bot: {conversation["bot"]}\n'

    return conversation_history

iface = gr.Interface(
    fn=echo_bot,
    inputs=gr.inputs.Textbox(lines=2, placeholder='Type something here...'),
    outputs='text'
)

iface.launch()
2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?