File size: 5,875 Bytes
86e0139
3d9242d
86e0139
3d9242d
86e0139
7e9c0f2
86e0139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)