chen666-666's picture
Update app.py
86e0139 verified
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('''
<html>
<head>
<title>聊天记录分析</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 1200px; margin: 0 auto; }
.section { margin-bottom: 30px; padding: 15px; border-radius: 8px; background-color: #f8f9fa; }
h2 { color: #007BFF; }
pre { background-color: #e9ecef; padding: 10px; border-radius: 4px; overflow-x: auto; }
.graph-container { min-height: 600px; }
</style>
</head>
<body>
<div class="container">
<h1>聊天记录分析结果</h1>
<div class="section">
<h2>人物实体</h2>
<pre>{{ entities }}</pre>
</div>
<div class="section">
<h2>人物关系</h2>
<pre>{{ relations }}</pre>
</div>
<div class="section">
<h2>法律风险分析</h2>
<pre>{{ risk_analysis }}</pre>
</div>
<div class="section graph-container">
<h2>关系图谱</h2>
{{ graph_html|safe }}
</div>
<a href="/">返回</a>
</div>
</body>
</html>
''', entities=entities, relations=relations, graph_html=graph_html, risk_analysis=risk_analysis)
else:
return render_template_string('''
<html>
<head>
<title>聊天记录分析</title>
</head>
<body>
<h1>聊天记录分析结果</h1>
<h2>人物实体</h2>
<p>{{ entities }}</p>
<h2>人物关系</h2>
<p>{{ relations }}</p>
<h2>法律风险分析</h2>
<pre>{{ risk_analysis }}</pre>
<h2>关系图谱</h2>
{{ graph_html|safe }}
<a href="/">返回</a>
</body>
</html>
''', entities=entities, relations=relations, graph_html=graph_html, risk_analysis=risk_analysis)
# 针对Hugging Face Space优化界面
if IS_HF_SPACE:
return '''
<html>
<head>
<title>聊天记录分析工具</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 800px; margin: 0 auto; text-align: center; }
.upload-box { border: 2px dashed #007BFF; padding: 30px; border-radius: 8px; margin: 20px 0; }
.upload-btn { background-color: #007BFF; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
.upload-btn:hover { background-color: #0056b3; }
h1 { color: #007BFF; }
</style>
</head>
<body>
<div class="container">
<h1>聊天记录分析工具</h1>
<div class="upload-box">
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" required>
<br><br>
<button type="submit" class="upload-btn">分析聊天记录</button>
</form>
<p>支持JSON或TXT格式的聊天记录文件</p>
</div>
<p>此工具将分析聊天记录中的人物关系并生成关系图谱,同时进行法律风险分析</p>
</div>
</body>
</html>
'''
else:
return '''
<html>
<head>
<title>聊天记录分析</title>
</head>
<body>
<h1>聊天记录分析</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="分析聊天记录">
</form>
<p>支持JSON或TXT格式的聊天记录文件</p>
</body>
</html>
'''
if __name__ == '__main__':
# 针对Hugging Face Space优化配置
port = int(os.environ.get("PORT", 7860))
app.run(host='0.000.0', port=port)