File size: 2,541 Bytes
6f19c94
 
fefc6e0
6f19c94
 
438368f
 
6f19c94
c1922a7
2d928bd
 
 
 
 
 
 
 
c73045b
2d928bd
 
c1922a7
2d928bd
 
c1922a7
2d928bd
438368f
2d928bd
438368f
2d928bd
 
438368f
2d928bd
 
438368f
c1922a7
2d928bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c73045b
2d928bd
c73045b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
os.system("pip uninstall -y gradio")
os.system("pip install gradio==3.50.2")


import pandas as pd
import matplotlib.pyplot as plt
import gradio as gr

import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import tempfile
import os

# 主分析函数
def analyze_csv(file, u, alpha1, alpha2):
    df = pd.read_csv(file.name)
    df_original = df.copy()
    df["Computed"] = df["Income"] * u + df["SpendingScore"] * alpha1 - df["Age"] * alpha2

    output_path = os.path.join(tempfile.gettempdir(), "processed.csv")
    df.to_csv(output_path, index=False)

    image_path = os.path.join(tempfile.gettempdir(), "computed_plot.png")
    plt.figure(figsize=(6, 4))
    plt.plot(df["Income"], df["Computed"], marker='o')
    plt.xlabel("Income")
    plt.ylabel("Computed Value")
    plt.title("Income vs Computed")
    plt.grid(True)
    plt.tight_layout()
    plt.savefig(image_path)
    plt.close()

    return df_original, df, output_path, image_path


# 示例数据定义(文件路径 + 参数)
example_data = [
    ["sample_table_0.csv", 0.0002, 0.4, 0.8],
    ["sample_table_1.csv", 0.0001, 0.5, 1.0],
    ["sample_table_2.csv", 0.00015, 0.6, 0.7],
]


# 构建 Gradio 界面
with gr.Blocks() as demo:
    gr.Markdown("## 📊 表格分析大模型")
    gr.Markdown("上传 CSV 文件或点击示例,我将为你分析并可视化结果。")

    with gr.Row():
        file_input = gr.File(label="上传CSV文件", file_types=[".csv"])
        u = gr.Number(label="u", value=0.0001)
        alpha1 = gr.Number(label="alpha1", value=0.5)
        alpha2 = gr.Number(label="alpha2", value=1.0)

    run_btn = gr.Button("开始分析")

    gr.Markdown("### ✅ 示例(点击自动加载)")
    gr.Examples(
        examples=example_data,
        inputs=[file_input, u, alpha1, alpha2],
        outputs=["original_table", "processed_table", "download", "image"],
        fn=analyze_csv,
        examples_per_page=3,
        label="点击示例自动分析"
    )

    gr.Markdown("### 📄 原始 CSV")
    original_table = gr.Dataframe(label="original_table")

    gr.Markdown("### 📑 处理后 CSV")
    processed_table = gr.Dataframe(label="processed_table")
    download = gr.File(label="下载处理结果", elem_id="download")

    gr.Markdown("### 📈 图表可视化")
    image = gr.Image(label="image")

    run_btn.click(fn=analyze_csv,
                  inputs=[file_input, u, alpha1, alpha2],
                  outputs=[original_table, processed_table, download, image])

demo.launch()