Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,39 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
# 读取CSV文件
|
| 9 |
df = pd.read_csv(file.name)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
img_path = os.path.join(tempfile.gettempdir(), "plot.png")
|
| 16 |
plt.figure(figsize=(6, 4))
|
| 17 |
plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7)
|
|
|
|
| 18 |
plt.xlabel("Income")
|
| 19 |
plt.ylabel("Spending Score")
|
| 20 |
-
plt.title("Income vs Spending Score")
|
| 21 |
plt.grid(True)
|
| 22 |
-
|
|
|
|
| 23 |
plt.savefig(img_path)
|
| 24 |
plt.close()
|
| 25 |
|
| 26 |
return stats, img_path
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
gr.File(label="上传 CSV 文件", file_types=[".csv"], name="file_input"),
|
| 35 |
-
gr.Button(value="开始分析", name="run_button"),
|
| 36 |
-
gr.Textbox(label="统计摘要", lines=10, name="output_text"),
|
| 37 |
-
gr.Image(label="图表", name="output_image"),
|
| 38 |
-
],
|
| 39 |
-
on_event=[
|
| 40 |
-
gr.Event(
|
| 41 |
-
name="run_button.click",
|
| 42 |
-
fn=analyze,
|
| 43 |
-
inputs=["file_input"],
|
| 44 |
-
outputs=["output_text", "output_image"]
|
| 45 |
-
)
|
| 46 |
-
]
|
| 47 |
-
)
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system("pip uninstall -y gradio")
|
| 3 |
+
os.system("pip install gradio==4.44.1")
|
| 4 |
+
|
| 5 |
+
|
| 6 |
import pandas as pd
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
+
import gradio as gr
|
|
|
|
| 9 |
|
| 10 |
+
def analyze_csv(file):
|
|
|
|
| 11 |
df = pd.read_csv(file.name)
|
| 12 |
|
| 13 |
+
# 统计分析
|
| 14 |
stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
|
| 15 |
|
| 16 |
+
# 生成图像:Income vs SpendingScore
|
|
|
|
| 17 |
plt.figure(figsize=(6, 4))
|
| 18 |
plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7)
|
| 19 |
+
plt.title("Income vs Spending Score")
|
| 20 |
plt.xlabel("Income")
|
| 21 |
plt.ylabel("Spending Score")
|
|
|
|
| 22 |
plt.grid(True)
|
| 23 |
+
|
| 24 |
+
img_path = "income_vs_score.png"
|
| 25 |
plt.savefig(img_path)
|
| 26 |
plt.close()
|
| 27 |
|
| 28 |
return stats, img_path
|
| 29 |
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=analyze_csv,
|
| 32 |
+
inputs=gr.File(label="Upload CSV File", file_types=[".csv"]),
|
| 33 |
+
outputs=[gr.Text(label="📊 Statistical Summary"), gr.Image(label="📈 Income vs Spending Score")],
|
| 34 |
+
title="📊 表格分析大模型",
|
| 35 |
+
description="上传一个CSV表格,我将输出统计分析结果并展示一张图表。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
iface.launch()
|