bbbdbbb commited on
Commit
d5bf59e
·
verified ·
1 Parent(s): 375b8fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -1,40 +1,44 @@
1
-
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import gradio as gr
 
 
5
 
6
  def analyze_csv(file):
 
7
  df = pd.read_csv(file.name)
8
 
 
9
  stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
10
 
11
- # 生成图像
 
 
 
12
  plt.figure(figsize=(6, 4))
13
  plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7)
14
  plt.title("Income vs Spending Score")
15
  plt.xlabel("Income")
16
  plt.ylabel("Spending Score")
17
  plt.grid(True)
18
- img_path = "income_vs_score.png"
19
- plt.savefig(img_path)
20
  plt.close()
21
 
 
22
  return {
23
- "📊 Summary": stats,
24
- "📈 Chart": img_path
25
  }
26
 
 
27
  iface = gr.Interface(
28
  fn=analyze_csv,
29
- inputs=gr.File(label="Upload CSV File", file_types=[".csv"]),
30
- outputs=[
31
- gr.Text(label="📊 Summary"),
32
- gr.Image(label="📈 Chart")
33
- ],
34
  title="📊 表格分析大模型",
35
- description="上传一个CSV表格,我将输出统计分析结果并展示一张图表。"
36
  )
37
 
38
-
39
  if __name__ == "__main__":
40
  iface.launch()
 
 
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 analyze_csv(file):
8
+ # 读取上传的文件
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
+ tmp_img_path = os.path.join(tempfile.gettempdir(), "income_vs_score.png")
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(tmp_img_path)
26
  plt.close()
27
 
28
+ # 返回字典结构,避免多输出 schema 解析问题
29
  return {
30
+ "统计摘要": stats,
31
+ "图表": tmp_img_path
32
  }
33
 
34
+ # Gradio 接口,注意 outputs 使用 gr.Json 兼容 dict 返回
35
  iface = gr.Interface(
36
  fn=analyze_csv,
37
+ inputs=gr.File(file_types=[".csv"], label="上传CSV文件"),
38
+ outputs=gr.JSON(label="分析结果(包含统计摘要 + 图像路径)"),
 
 
 
39
  title="📊 表格分析大模型",
40
+ description="上传一个CSV表格,我将输出统计分析结果并展示一张图表的路径。"
41
  )
42
 
 
43
  if __name__ == "__main__":
44
  iface.launch()