Update app.py
Browse files3 unmodified lines45678978910111213141514151617181 unmodified line202122232324252627282930313233343536373839404126272829303132333435363738394041424344454647484950513 unmodified linesimport tempfileimport os
def analyze(file_obj): # 读取CSV df = pd.read_csv(file_obj.name)def analyze(file): # 读取CSV文件 df = pd.read_csv(file.name)
# 统计摘要 stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()
# 生成图表并保存为临时图片 tmp_path = os.path.join(tempfile.gettempdir(), "income_vs_score.png") # 生成图像并保存 img_path = os.path.join(tempfile.gettempdir(), "plot.png") plt.figure(figsize=(6, 4)) plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7) plt.xlabel("Income")1 unmodified line plt.title("Income vs Spending Score") plt.grid(True) plt.tight_layout() plt.savefig(tmp_path) plt.savefig(img_path) plt.close()
return stats, tmp_path
with gr.Blocks(title="CSV分析工具") as demo: gr.Markdown("## 📊 表格分析大模型\n请上传一个CSV文件,我将输出统计摘要并绘制图表。")
with gr.Row(): file_input = gr.File(label="上传 CSV 文件", file_types=[".csv"]) run_btn = gr.Button("开始分析")
output_text = gr.Textbox(label="统计摘要", lines=10) output_image = gr.Image(label="图表")
run_btn.click(fn=analyze, inputs=file_input, outputs=[output_text, output_image])
demo.launch() return stats, img_path
app = gr.App( blocks=lambda: gr.Page( title="📊 表格分析大模型", description="上传 CSV 文件,我将输出统计摘要并绘制图表。", components=[ gr.File(label="上传 CSV 文件", file_types=[".csv"], name="file_input"), gr.Button(value="开始分析", name="run_button"), gr.Textbox(label="统计摘要", lines=10, name="output_text"), gr.Image(label="图表", name="output_image"), ], on_event=[ gr.Event( name="run_button.click", fn=analyze, inputs=["file_input"], outputs=["output_text", "output_image"] ) ] ))
if __name__ == "__main__": app.run()app.py+32-22