import os import json from flask import Flask, render_template, request, jsonify from PIL import Image app = Flask(__name__) app.config['JSON_AS_ASCII'] = False # 确保JSON响应中的中文字符正确显示 # 添加全局变量来存储当前界面 current_page = "主页" # 确保templates和static文件夹存在 templates_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') if not os.path.exists(templates_dir): os.makedirs(templates_dir) if not os.path.exists(static_dir): os.makedirs(static_dir) # 图片路径 image_paths = { "主页": "E:/文件/LLM_model/RAG/图片/D3/主页.PNG", "工程信息": "E:/文件/LLM_model/RAG/图片/D3/工程信息.PNG", "取费设置": "E:/文件/LLM_model/RAG/图片/D3/取费设置.PNG", "组合件": "E:/文件/LLM_model/RAG/图片/D3/组合件.PNG", "工程量": "E:/文件/LLM_model/RAG/图片/D3/工程量.PNG", "材机分析": "E:/文件/LLM_model/RAG/图片/D3/材机分析.PNG", "工程费用": "E:/文件/LLM_model/RAG/图片/D3/工程费用.PNG", "报表输出": "E:/文件/LLM_model/RAG/图片/D3/报表输出.PNG" } # 创建HTML模板 with open(os.path.join(templates_dir, 'index.html'), 'w', encoding='utf-8') as f: f.write(''' 页面切换示例
页面1
页面2
页面3
页面4
页面5
页面6
页面7
页面8
''') # 将图片复制到static文件夹 def copy_image_to_static(source_path, target_name): try: img = Image.open(source_path) target_path = os.path.join(static_dir, f"{target_name}.png") img.save(target_path) return True except Exception as e: print(f"复制图片失败: {e}") @app.route('/') def index(): # 在第一次请求时设置图片 setup_images() return render_template('index.html') @app.route('/get_image') def get_image(): title = request.args.get('title', '主页') if title in image_paths: image_url = f"/static/{title}.png" resp = jsonify({'image_url': image_url}) resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp resp = jsonify({'error': '图片不存在'}) resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp # 设置图片 def setup_images(): # 检查是否已经设置过图片 if not hasattr(setup_images, 'done'): # 复制内容图片 for title, path in image_paths.items(): copy_image_to_static(path, title) # 标记为已完成 setup_images.done = True # 添加处理聊天消息的路由 @app.route('/ask', methods=['POST']) def ask(): data = request.json message = data.get('message', '') # 这里可以添加调用Dify API的逻辑 resp = jsonify({ 'answer': f'您好,我收到了您的问题:"{message}"。由于当前无法连接到Dify服务器,我只能提供有限的回复。' }) resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp # 添加获取当前界面名称的接口 @app.route('/get_current_page', methods=['GET']) def get_current_page(): global current_page title = request.args.get('title') # 如果请求中包含title参数,则更新当前界面 if title: current_page = title print(f"更新当前界面为: {current_page}") response = { 'software_name': '博微配网工程计价通D3软件',#博微配网工程计价通D3软件 'engineering_type': '单工程', 'software_version': '3.2.4.23', 'project_name': '525252.BDD3', 'current_page': current_page } # 检查是否是浏览器直接访问 if request.headers.get('Accept', '').find('text/html') != -1: # 返回一个HTML页面,显示JSON数据 html = f''' 当前界面信息

当前界面信息

software_name: {response['software_name']}

engineering_type: {response['engineering_type']}

software_version: {response['software_version']}

project_name: {response['project_name']}

current_page: {response['current_page']}

原始JSON:

{json.dumps(response, ensure_ascii=False, indent=2)}
''' return html # API调用返回JSON return json.dumps(response, ensure_ascii=False) # 添加清空JSON的路由 @app.route('/clear_json') def clear_json(): global current_page # 重置当前页面 current_page = "" # 返回空JSON,添加编码设置 resp = jsonify({}) resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp if __name__ == "__main__": app.run(host='0.0.0.0', port=1234, debug=True)