Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,41 @@
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
#
|
| 9 |
-
df = pd.read_csv(
|
| 10 |
|
| 11 |
-
#
|
| 12 |
stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# 绘制散点图
|
| 18 |
plt.figure(figsize=(6, 4))
|
| 19 |
plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7)
|
| 20 |
-
plt.title("Income vs Spending Score")
|
| 21 |
plt.xlabel("Income")
|
| 22 |
plt.ylabel("Spending Score")
|
|
|
|
| 23 |
plt.grid(True)
|
| 24 |
plt.tight_layout()
|
| 25 |
-
plt.savefig(
|
| 26 |
plt.close()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
def analyze(file_obj):
|
| 8 |
+
# 读取CSV
|
| 9 |
+
df = pd.read_csv(file_obj.name)
|
| 10 |
|
| 11 |
+
# 统计摘要
|
| 12 |
stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
|
| 13 |
|
| 14 |
+
# 生成图表并保存为临时图片
|
| 15 |
+
tmp_path = os.path.join(tempfile.gettempdir(), "income_vs_score.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 |
plt.tight_layout()
|
| 23 |
+
plt.savefig(tmp_path)
|
| 24 |
plt.close()
|
| 25 |
|
| 26 |
+
return stats, tmp_path
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
with gr.Blocks(title="CSV分析工具") as demo:
|
| 30 |
+
gr.Markdown("## 📊 表格分析大模型\n请上传一个CSV文件,我将输出统计摘要并绘制图表。")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
file_input = gr.File(label="上传 CSV 文件", file_types=[".csv"])
|
| 34 |
+
run_btn = gr.Button("开始分析")
|
| 35 |
+
|
| 36 |
+
output_text = gr.Textbox(label="统计摘要", lines=10)
|
| 37 |
+
output_image = gr.Image(label="图表")
|
| 38 |
+
|
| 39 |
+
run_btn.click(fn=analyze, inputs=file_input, outputs=[output_text, output_image])
|
| 40 |
+
|
| 41 |
+
demo.launch()
|
|
|