-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
278 lines (225 loc) · 8.67 KB
/
app.py
File metadata and controls
278 lines (225 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from flask import Flask, request, jsonify, render_template, session
import os
import json
import uuid
from datetime import datetime
import asyncio
from werkzeug.utils import secure_filename
from doc import analyze_file, analyze_data_with_ai
from database import ChatDatabase
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.secret_key = 'your-secret-key-change-this'
# 配置文件上传
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'xls', 'parquet', 'json'}
MAX_FILE_SIZE = 16 * 1024 * 1024 # 16MB
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = MAX_FILE_SIZE
# 确保上传文件夹存在
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# 初始化数据库
db = ChatDatabase()
def format_analysis_result(result):
"""将AI分析结果转换为markdown格式"""
if "error" in result:
return f"❌ **错误**: {result['error']}"
markdown_content = []
# 问题标题
markdown_content.append(f"## 📊 数据分析结果")
markdown_content.append(f"**问题**: {result['question']}")
markdown_content.append("")
# SQL查询
markdown_content.append("### 🔍 生成的SQL查询")
markdown_content.append("```sql")
markdown_content.append(result['sql_query'])
markdown_content.append("```")
markdown_content.append("")
# 数据信息
data_info = result['data_info']
markdown_content.append("### 📋 数据概览")
markdown_content.append(f"- **行数**: {data_info['行数']:,}")
markdown_content.append(f"- **列数**: {data_info['列数']}")
markdown_content.append(f"- **列名**: {', '.join(data_info['列名'])}")
markdown_content.append("")
# 查询结果
query_result = result['result']
markdown_content.append(f"### 📈 查询结果 ({query_result['row_count']:,} 行)")
if query_result['row_count'] == 0:
markdown_content.append("没有找到匹配的数据。")
else:
# 生成表格
columns = query_result['columns']
data = query_result['data']
# 表格头部
header = "| " + " | ".join(columns) + " |"
separator = "| " + " | ".join([":---"] * len(columns)) + " |"
markdown_content.append(header)
markdown_content.append(separator)
# 表格数据(最多显示前10行)
display_rows = min(10, len(data))
for i in range(display_rows):
row = data[i]
row_values = []
for col in columns:
value = row.get(col)
if value is None:
row_values.append("null")
elif isinstance(value, (int, float)):
if isinstance(value, float):
row_values.append(f"{value:.2f}")
else:
row_values.append(f"{value:,}")
else:
# 截断过长的文本
str_value = str(value)
if len(str_value) > 50:
str_value = str_value[:47] + "..."
row_values.append(str_value)
markdown_content.append("| " + " | ".join(row_values) + " |")
if query_result['row_count'] > 10:
markdown_content.append("")
markdown_content.append(f"*显示前 10 行,共 {query_result['row_count']:,} 行*")
return "\n".join(markdown_content)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/upload', methods=['POST'])
def upload_file():
try:
if 'file' not in request.files:
return jsonify({'error': '没有选择文件'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': '没有选择文件'}), 400
if not allowed_file(file.filename):
return jsonify({'error': '不支持的文件类型,仅支持 CSV, Excel, Parquet, JSON'}), 400
# 保存文件
filename = secure_filename(file.filename)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
unique_filename = f"{timestamp}_{filename}"
filepath = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
file.save(filepath)
# 异步调用文件分析
result = asyncio.run(analyze_file(file_path=filepath))
if 'error' in result:
# 如果分析出错,删除临时文件
if os.path.exists(filepath):
os.remove(filepath)
return jsonify(result), 400
# 生成会话ID
session_id = session.get('session_id')
if not session_id:
session_id = str(uuid.uuid4())
session['session_id'] = session_id
db.create_session(session_id)
# 保存文件信息到数据库
file_info = {
'id': str(uuid.uuid4()),
'filename': filename,
'filepath': filepath,
'data_info': result['data_info']
}
db.save_file_info(session_id, file_info)
# 返回文件信息和数据概要
return jsonify({
'success': True,
'file_id': file_info['id'],
'filename': filename,
})
except Exception as e:
print(f"Error: {str(e)}")
return jsonify({'error': f'服务器错误: {str(e)}'}), 500
@app.route('/api/ask_question', methods=['POST'])
def ask_question():
try:
# 获取请求参数
data = request.get_json()
file_id = data.get('file_id')
question = data.get('question', '')
if not file_id:
return jsonify({'error': '请选择要分析的文件'}), 400
if not question.strip():
return jsonify({'error': '请输入问题'}), 400
# 获取会话ID
session_id = session.get('session_id')
if not session_id:
return jsonify({'error': '请先上传文件'}), 400
# 获取文件详情
file_detail = db.get_file_detail(file_id)
if not file_detail:
return jsonify({'error': '文件不存在'}), 404
# 检查文件是否属于当前会话
# 注意:这里简化处理,实际应该检查file_detail.session_id == session_id
# 异步调用AI分析
result = asyncio.run(
analyze_data_with_ai(
file_path=file_detail['filepath'],
question=question,
data_info=file_detail['data_info']
)
)
if 'error' in result:
return jsonify(result), 400
# 将结果转换为markdown格式
markdown_result = format_analysis_result(result)
# 生成聊天记录
chat_record = {
'id': str(uuid.uuid4()),
'timestamp': datetime.now().isoformat(),
'question': question,
'result': result,
'markdown_result': markdown_result
}
# 保存到数据库
db.save_chat_record(session_id, file_id, chat_record)
return jsonify({
'success': True,
'chat_id': chat_record['id'],
'markdown_result': markdown_result
})
except Exception as e:
import traceback
traceback.print_exc()
print(f"Error: {str(e)}")
return jsonify({'error': f'服务器错误: {str(e)}'}), 500
@app.route('/api/chat_history')
def get_chat_history():
session_id = session.get('session_id')
if not session_id:
return jsonify({'history': []})
history = db.get_chat_history(session_id)
return jsonify({'history': history})
@app.route('/api/new_session', methods=['POST'])
def new_session():
session_id = str(uuid.uuid4())
session['session_id'] = session_id
db.create_session(session_id)
return jsonify({'session_id': session_id})
@app.route('/api/sessions')
def get_all_sessions():
"""获取所有会话列表"""
sessions = db.get_all_sessions()
return jsonify({'sessions': sessions})
@app.route('/api/files')
def get_files():
"""获取当前会话的所有文件"""
session_id = session.get('session_id')
if not session_id:
return jsonify({'files': []})
files = db.get_files(session_id)
return jsonify({'files': files})
@app.route('/api/switch_session/<session_id>', methods=['POST'])
def switch_session(session_id):
"""切换到指定会话"""
if db.session_exists(session_id):
session['session_id'] = session_id
return jsonify({'success': True, 'session_id': session_id})
else:
return jsonify({'error': '会话不存在'}), 404
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)