場景與解決的問題
1)用Python做了人工智能模型後,需要快速部署上線
2)隻是示範給客戶使用或者小規模測試
3)可以在一個Web界面操作、示範AI模型的能力
4)希望花最小的代價完成 1)、2)、3)
之前我介紹過這2個工具Plotly, Dash:
免費工具可互動的可視化神器 Plotly Express
給客戶展示,除了PPT,還有更強大的工具,免費
他們的定位是資料展示和互動;當然也可以用來完成本場景,不過今天介紹的神器Gradio,是專門用來快速上線Python人工智能模型的,更友善、更快速
Gradio
- 定位:
部署和分享激動人心的AI應用
- 舉例1,實作一個看圖識物的AI APP:
1. 導入gardio
import gradio as gr
import tensorflow as tf # 用于訓練模型
import numpy as np
import requests # 擷取标簽
2. 定義模型,确定輸入和輸出
inception_net = tf.keras.applications.InceptionV3() # load the model
# Download human-readable labels for ImageNet.
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n") # 擷取标簽
def classify_image(inp):
inp = inp.reshape((-1, 299, 299, 3))
inp = tf.keras.applications.inception_v3.preprocess_input(inp)
prediction = inception_net.predict(inp).flatten()
return {labels[i]: float(prediction[i]) for i in range(1000)}
3. 用gradio關聯輸入和輸出,一鍵建立出APP(預設端口7860)
image = gr.inputs.Image(shape=(299, 299, 3))
label = gr.outputs.Label(num_top_classes=3)
gr.Interface(fn=classify_image, inputs=image, outputs=label, capture_session=True).launch()
4. 運作APP,測效果
運作界面
上傳圖檔可以進行測試,草泥馬英文為Llama
- 舉例2:加載一個已有AnimeGAN模型,把你或者你朋友的照片變成可愛的卡通形象
生成的效果:
有趣吧,關注ThinkInAI,私信可以擷取源碼