Files
DB-GPT/examples/excel/excel_analysis.py
aries_ckt bbf270699a Merge branch 'feature/support-neo4j' into feat/dbgpt_skill
# Conflicts:
#	packages/dbgpt-ext/src/dbgpt_ext/storage/knowledge_graph/community/neo4j_store_adapter.py
#	uv.lock
#	web/locales/en/common.ts
#	web/locales/zh/common.ts
#	web/package-lock.json
#	web/yarn.lock
2026-02-12 17:43:17 +08:00

50 lines
1.6 KiB
Python

from flask import Flask, request, jsonify
import pandas as pd
import os
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)