Update app.py
Browse files3 unmodified lines4567891011121314151617181 unmodified line202122232425262728293031323334353637383940413 unmodified linesimport tempfileimport osdef analyze(file_obj):# 读取CSVdf = pd.read_csv(file_obj.name)# 统计摘要stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()# 生成图表并保存为临时图片tmp_path = os.path.join(tempfile.gettempdir(), "income_vs_score.png")plt.figure(figsize=(6, 4))plt.scatter(df["Income"], df["SpendingScore"], alpha=0.7)plt.xlabel("Income")1 unmodified lineplt.title("Income vs Spending Score")plt.grid(True)plt.tight_layout()plt.savefig(tmp_path)plt.close()return stats, tmp_pathwith 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()3 unmodified lines4567891011121314151617181 unmodified line20212223242526272829303132333435363738394041424344454647484950513 unmodified linesimport tempfileimport osdef analyze(file):# 读取CSV文件df = pd.read_csv(file.name)# 统计摘要stats = df.describe().loc[["mean", "std", "min", "max"]].round(2).to_string()# 生成图像并保存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 lineplt.title("Income vs Spending Score")plt.grid(True)plt.tight_layout()plt.savefig(img_path)plt.close()return stats, img_pathapp = 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