2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【macOS】IBM Cloud Virtual Server for VPCにGradioアプリケーションをデプロイする

Last updated at Posted at 2025-03-18

はじめに

今回は、IBM Cloud のサービスである IBM Cloud Virtual Server for VPC の上にGradio で作成したWeb アプリケーションをデプロイする手順を備忘録として記事にアウトプットします。

IBM Cloud Virtual Server for VPC はIBM TechZone で払い出しています。

環境

手順

Virtual Server にアクセス

ローカルのVSCode のターミナルでVirtual Server にアクセスします。

  1. TechZone の環境詳細にあるDownload SSH key から証明書をダウンロードする
    techzone2.png

  2. 証明書をターミナルで実行するディレクトリに配置する

  3. ターミナルで以下のコマンドを実行してアクセス権限を設定する

    ターミナル
    chmod 600 <証明書のファイル名>
    
  4. VSCode を立ち上げる

  5. VSCode の拡張機能、Remote - SSH をインストールする
    vscode9.png

  6. 左下の>< マークをクリックする
    vscode10.png

  7. ホストに接続する… をクリックする
    vscode11.png

  8. +新規SSH ホストを追加する… をクリックする
    vscode12.png

  9. ssh itzuser@<PublicIP> -p <SSHPort> -i ~/<証明書のファイル名> を入力してEnter Key をクリックする
    vscode13.png

  10. /User/<ディレクトリ名>/.ssh/config をクリックする(任意のディレクトリを選択できます)
    vscode14.png

  11. 「接続」をクリックする
    vscode15.png

  12. 完了
    vscode16.png

Python3.12 をインストール

ターミナルを立ち上げる。

  1. システムを更新する

    ターミナル
    sudo apt update && sudo apt upgrade -y
    
  2. 必要なパッケージをインストールする

    ターミナル
    sudo apt install -y software-properties-common
    
  3. deadsnakes PPA を追加する

    ターミナル
    sudo add-apt-repository ppa:deadsnakes/ppa -y
    sudo apt update
    
  4. Python 3.12 をインストールする

    ターミナル
    sudo apt install -y python3.12
    
  5. バージョンの確認

    ターミナル
    python3.12 --version
    

pip をインストール

以下を実行して、pip をインストールする

ターミナル
sudo apt install -y python3.12-venv python3.12-dev python3-pip

Web アプリケーションを配置

  1. VSCode 上で新規フォルダーを作成する

vscode17.png

  1. ターミナルを起動する
    vscode18.png

  2. 以下を実行して、作成したフォルダに移動する

    ターミナル
    cd gradio
    
  3. ローカルにあるWeb アプリケーションファイルをドラッグ&ドロップで配置する
    vscode19.png

    app.py の中身はこちら

    app.py
    import random
    import gradio as gr
    
    def get_foturne(your_name):
        fortune_lists = ['大吉', '', '小吉', '', '末吉']
        fortune_result = random.choice(fortune_lists)
        return your_name + "さんの今日の運勢は・・・" + fortune_result + "です"
    
    demo = gr.Interface(fn=get_foturne,
                        inputs="text",
                        outputs="text")
    demo.launch()
    

Web アプリケーションを実行

gradio のパッケージをインストールする

  1. 以下を実行して、gradio をインストールする

    ターミナル
    pip install gradio
    
  2. 以下を実行して、gradio がインストールされたか確認

    ターミナル
    pip show gradio
    

    以下のような出力であればOK

    出力
    Name: gradio
    Version: 5.20.1
    Summary: Python library for easily interacting with trained machine learning models
    Home-page: 
    Author: 
    Author-email: Abubakar Abid <gradio-team@huggingface.co>, Ali Abid <gradio-team@huggingface.co>, Ali Abdalla <gradio-team@huggingface.co>, Dawood Khan <gradio-team@huggingface.co>, Ahsen Khaliq <gradio-team@huggingface.co>, Pete Allen <gradio-team@huggingface.co>, Ömer Faruk Özdemir <gradio-team@huggingface.co>, Freddy A Boulton <gradio-team@huggingface.co>, Hannah Blair <gradio-team@huggingface.co>
    License: 
    Location: /home/itzuser/.local/lib/python3.10/site-packages
    Requires: aiofiles, anyio, fastapi, ffmpy, gradio-client, groovy, httpx, huggingface-hub, jinja2, markupsafe, numpy, orjson, packaging, pandas, pillow, pydantic, pydub, python-multipart, pyyaml, ruff, safehttpx, semantic-version, starlette, tomlkit, typer, typing-extensions, uvicorn
    Required-by:
    

起動

ターミナル
python3 test.py

vscode20.png

上のように、URLが発行されればOK!

リンクを開いてみると…
vscode21.png

リンクを他の人に配布する

Virtual Server にデプロイしたWeb アプリケーションのリンクを他の人に共有するための手順です。

  1. demo.launch()() の中でサーバーとポートの指定をする

    app.pyの最終行
    demo.launch(server_name="0.0.0.0", server_port=8080)
    #開いていることが確認できた8080を指定
    
  2. 起動

    ターミナル
    python3 test.py
    

    vscode22.png

  3. http://<PublicIP>:8080/ をブラウザで開く
    vscode23.png

2
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?