Files
DB-GPT/examples/excel/excel_analysis.py
Aries-ckt ef83851b31 🎉 DB-GPT V0.8.0 - Beta Testing (#2988)
Co-authored-by: lusain <lusain1990@gmail.com>
Co-authored-by: alan.cl <1165243776@qq.com>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-03-16 11:55:42 +08:00

55 lines
1.6 KiB
Python

import os
import pandas as pd
from flask import Flask, jsonify, request
app = Flask(__name__)
UPLOAD_FOLDER = "/tmp/uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@app.route("/upload_excel", methods=["POST"])
def upload_excel():
if "file" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"error": "No selected file"}), 400
file_path = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
file.save(file_path)
# Optional: Parse Excel to validate it works
try:
df = pd.read_excel(file_path)
columns = df.columns.tolist()
return jsonify({"message": "File uploaded successfully", "columns": columns})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/query_excel", methods=["POST"])
def query_excel():
data = request.json
file_name = data.get("file_name")
query = data.get("query")
file_path = os.path.join(app.config["UPLOAD_FOLDER"], file_name)
if not os.path.exists(file_path):
return jsonify({"error": "File not found"}), 404
try:
df = pd.read_excel(file_path)
# Placeholder logic for querying the Excel file:
# This should be replaced with DB-GPT integration for natural language queries.
response = f"Query on {len(df)} rows completed."
return jsonify({"query_result": response})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)