Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,79 @@ import pandas as pd
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
df = pd.read_csv(file.name)
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
plt.figure(figsize=(6, 4))
|
| 18 |
-
plt.
|
| 19 |
-
plt.title("Income vs Spending Score")
|
| 20 |
plt.xlabel("Income")
|
| 21 |
-
plt.ylabel("
|
|
|
|
| 22 |
plt.grid(True)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
plt.savefig(img_path)
|
| 26 |
plt.close()
|
| 27 |
|
| 28 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 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()
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import tempfile
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
# 主分析函数
|
| 17 |
+
def analyze_csv(file, u, alpha1, alpha2):
|
| 18 |
df = pd.read_csv(file.name)
|
| 19 |
+
df_original = df.copy()
|
| 20 |
+
df["Computed"] = df["Income"] * u + df["SpendingScore"] * alpha1 - df["Age"] * alpha2
|
| 21 |
|
| 22 |
+
output_path = os.path.join(tempfile.gettempdir(), "processed.csv")
|
| 23 |
+
df.to_csv(output_path, index=False)
|
| 24 |
|
| 25 |
+
image_path = os.path.join(tempfile.gettempdir(), "computed_plot.png")
|
| 26 |
plt.figure(figsize=(6, 4))
|
| 27 |
+
plt.plot(df["Income"], df["Computed"], marker='o')
|
|
|
|
| 28 |
plt.xlabel("Income")
|
| 29 |
+
plt.ylabel("Computed Value")
|
| 30 |
+
plt.title("Income vs Computed")
|
| 31 |
plt.grid(True)
|
| 32 |
+
plt.tight_layout()
|
| 33 |
+
plt.savefig(image_path)
|
|
|
|
| 34 |
plt.close()
|
| 35 |
|
| 36 |
+
return df_original, df, output_path, image_path
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# 示例数据定义(文件路径 + 参数)
|
| 40 |
+
example_data = [
|
| 41 |
+
["sample_table_0.csv", 0.0002, 0.4, 0.8],
|
| 42 |
+
["sample_table_1.csv", 0.0001, 0.5, 1.0],
|
| 43 |
+
["sample_table_2.csv", 0.00015, 0.6, 0.7],
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# 构建 Gradio 界面
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## 📊 表格分析大模型")
|
| 50 |
+
gr.Markdown("上传 CSV 文件或点击示例,我将为你分析并可视化结果。")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
file_input = gr.File(label="上传CSV文件", file_types=[".csv"])
|
| 54 |
+
u = gr.Number(label="u", value=0.0001)
|
| 55 |
+
alpha1 = gr.Number(label="alpha1", value=0.5)
|
| 56 |
+
alpha2 = gr.Number(label="alpha2", value=1.0)
|
| 57 |
+
|
| 58 |
+
run_btn = gr.Button("开始分析")
|
| 59 |
+
|
| 60 |
+
gr.Markdown("### ✅ 示例(点击自动加载)")
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=example_data,
|
| 63 |
+
inputs=[file_input, u, alpha1, alpha2],
|
| 64 |
+
outputs=["original_table", "processed_table", "download", "image"],
|
| 65 |
+
fn=analyze_csv,
|
| 66 |
+
examples_per_page=3,
|
| 67 |
+
label="点击示例自动分析"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
gr.Markdown("### 📄 原始 CSV")
|
| 71 |
+
original_table = gr.Dataframe(label="original_table")
|
| 72 |
+
|
| 73 |
+
gr.Markdown("### 📑 处理后 CSV")
|
| 74 |
+
processed_table = gr.Dataframe(label="processed_table")
|
| 75 |
+
download = gr.File(label="下载处理结果", elem_id="download")
|
| 76 |
+
|
| 77 |
+
gr.Markdown("### 📈 图表可视化")
|
| 78 |
+
image = gr.Image(label="image")
|
| 79 |
+
|
| 80 |
+
run_btn.click(fn=analyze_csv,
|
| 81 |
+
inputs=[file_input, u, alpha1, alpha2],
|
| 82 |
+
outputs=[original_table, processed_table, download, image])
|
| 83 |
|
| 84 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
|
|
|
|
|