Gradio Agents & MCP 黑客马拉松
获奖者Gradio Agents & MCP 黑客马拉松
获奖者表格数据科学是机器学习应用最广泛的领域,其问题范围从客户细分到客户流失预测。在表格数据科学工作流的各个阶段,向利益相关者或客户沟通工作可能很繁琐;这阻碍了数据科学家专注于重要的事情,例如数据分析和模型构建。数据科学家最终可能花费数小时构建一个接收数据框并返回绘图的仪表盘,或者返回数据集中的预测或聚类图。在本指南中,我们将介绍如何使用 `gradio` 来改进你的数据科学工作流。我们还将讨论如何使用 `gradio` 和 skops 仅用一行代码构建界面!
请确保你已经安装了 `gradio` Python 包。
我们将看看如何创建一个简单的用户界面,该界面根据产品信息预测故障。
import gradio as gr
import pandas as pd
import joblib
import datasets
inputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(4,"dynamic"), label="Input Data", interactive=1)]
outputs = [gr.Dataframe(row_count = (2, "dynamic"), col_count=(1, "fixed"), label="Predictions", headers=["Failures"])]
model = joblib.load("model.pkl")
# we will give our dataframe as example
df = datasets.load_dataset("merve/supersoaker-failures")
df = df["train"].to_pandas()
def infer(input_dataframe):
return pd.DataFrame(model.predict(input_dataframe))
gr.Interface(fn = infer, inputs = inputs, outputs = outputs, examples = [[df.head(2)]]).launch()
让我们分解上述代码。
现在我们将创建一个最小数据可视化仪表盘的示例。你可以在相关空间中找到更全面的版本。
import gradio as gr
import pandas as pd
import datasets
import seaborn as sns
import matplotlib.pyplot as plt
df = datasets.load_dataset("merve/supersoaker-failures")
df = df["train"].to_pandas()
df.dropna(axis=0, inplace=True)
def plot(df):
plt.scatter(df.measurement_13, df.measurement_15, c = df.loading,alpha=0.5)
plt.savefig("scatter.png")
df['failure'].value_counts().plot(kind='bar')
plt.savefig("bar.png")
sns.heatmap(df.select_dtypes(include="number").corr())
plt.savefig("corr.png")
plots = ["corr.png","scatter.png", "bar.png"]
return plots
inputs = [gr.Dataframe(label="Supersoaker Production Data")]
outputs = [gr.Gallery(label="Profiling Dashboard", columns=(1,3))]
gr.Interface(plot, inputs=inputs, outputs=outputs, examples=[df.head(100)], title="Supersoaker Failures Analysis Dashboard").launch()
我们将使用与训练模型相同的数据集,但这次我们将制作一个仪表盘来可视化它。
`skops` 是一个基于 `huggingface_hub` 和 `sklearn` 构建的库。随着 `skops` 最近与 `gradio` 的集成,你可以仅用一行代码构建表格数据界面!
import gradio as gr
# title and description are optional
title = "Supersoaker Defective Product Prediction"
description = "This model predicts Supersoaker production line failures. Drag and drop any slice from dataset or edit values as you wish in below dataframe component."
gr.load("huggingface/scikit-learn/tabular-playground", title=title, description=description).launch()
使用 `skops` 推送到 Hugging Face Hub 的 `sklearn` 模型包含一个 `config.json` 文件,该文件包含带有列名、正在解决的任务(可以是 `tabular-classification` 或 `tabular-regression`)的示例输入。Gradio 根据任务类型构建 `Interface`,并使用列名和示例输入来构建它。你可以参考 skops 关于在 Hub 上托管模型的文档,了解如何使用 `skops` 将模型推送到 Hub。