from flask import Flask, request, render_template_string from utils import analyze_chat import os app = Flask(__name__) # 检查是否在Hugging Face环境中运行 IS_HF_SPACE = os.environ.get("SPACE_ID") is not None @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': file = request.files['file'] entities, relations, graph_html, risk_analysis = analyze_chat(file) # 针对Hugging Face Space优化显示 if IS_HF_SPACE: return render_template_string(''' 聊天记录分析

聊天记录分析结果

人物实体

{{ entities }}

人物关系

{{ relations }}

法律风险分析

{{ risk_analysis }}

关系图谱

{{ graph_html|safe }}
返回
''', entities=entities, relations=relations, graph_html=graph_html, risk_analysis=risk_analysis) else: return render_template_string(''' 聊天记录分析

聊天记录分析结果

人物实体

{{ entities }}

人物关系

{{ relations }}

法律风险分析

{{ risk_analysis }}

关系图谱

{{ graph_html|safe }} 返回 ''', entities=entities, relations=relations, graph_html=graph_html, risk_analysis=risk_analysis) # 针对Hugging Face Space优化界面 if IS_HF_SPACE: return ''' 聊天记录分析工具

聊天记录分析工具



支持JSON或TXT格式的聊天记录文件

此工具将分析聊天记录中的人物关系并生成关系图谱,同时进行法律风险分析

''' else: return ''' 聊天记录分析

聊天记录分析

支持JSON或TXT格式的聊天记录文件

''' if __name__ == '__main__': # 针对Hugging Face Space优化配置 port = int(os.environ.get("PORT", 7860)) app.run(host='0.000.0', port=port)