| import gradio as gr |
| import pandas as pd |
| import matplotlib.pyplot as plt |
| import tempfile |
| import os |
|
|
| def analyze(file): |
| |
| df = pd.read_csv(file.name) |
|
|
| |
| stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string() |
|
|
| |
| img_path = os.path.join(tempfile.gettempdir(), "plot.png") |
| plt.figure(figsize=(6, 4)) |
| plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7) |
| plt.xlabel("Income") |
| plt.ylabel("Spending Score") |
| plt.title("Income vs Spending Score") |
| plt.grid(True) |
| plt.tight_layout() |
| plt.savefig(img_path) |
| plt.close() |
|
|
| return stats, img_path |
|
|
|
|
| app = gr.App( |
| blocks=lambda: gr.Page( |
| title="📊 表格分析大模型", |
| description="上传 CSV 文件,我将输出统计摘要并绘制图表。", |
| components=[ |
| gr.File(label="上传 CSV 文件", file_types=[".csv"], name="file_input"), |
| gr.Button(value="开始分析", name="run_button"), |
| gr.Textbox(label="统计摘要", lines=10, name="output_text"), |
| gr.Image(label="图表", name="output_image"), |
| ], |
| on_event=[ |
| gr.Event( |
| name="run_button.click", |
| fn=analyze, |
| inputs=["file_input"], |
| outputs=["output_text", "output_image"] |
| ) |
| ] |
| ) |
| ) |
|
|
| if __name__ == "__main__": |
| app.run() |
|
|