File size: 2,020 Bytes
15549a1
 
f3718f0
15549a1
f3718f0
 
 
 
 
 
 
5c68062
0d31272
 
 
15549a1
0d31272
15549a1
5c68062
 
 
15549a1
 
0d31272
 
 
 
 
 
 
 
 
 
15549a1
 
 
 
0d31272
15549a1
 
0d31272
 
 
15549a1
0d31272
f3718f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import requests
import time

API_URL = "http://127.0.0.1:5000/api/process"

def save_to_file(user_input):
    with open("user_input.txt", "w") as file:
        file.write(user_input)

def save_score(user_id, subset_name, question_id, score, retain_option, retain_reason):
    if user_id.strip() == '':
        raise ValueError("User ID cannot be empty.")
    
    score_data = {
        "subset_name": subset_name,
        "question_id": question_id,
        "score": score,
        "retain_option": retain_option,
        "retain_reason": retain_reason
    }

    # 创建用户目录路径
    user_dir = os.path.join("scores", user_id)
    os.makedirs(user_dir, exist_ok=True)

    # JSON 文件路径
    json_file_path = os.path.join(user_dir, "score.json")

    # 读取现有数据或初始化为空列表
    if os.path.exists(json_file_path):
        with open(json_file_path, "r", encoding="utf-8") as file:
            data = json.load(file)
    else:
        data = []

    # 添加新的分数数据
    data.append(score_data)

    # 保存数据到 JSON 文件
    with open(json_file_path, "w", encoding="utf-8") as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

        return f'Time {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} UCT-8, score submitted successfully. User ID: {user_id}, Subset: {subset_name}, Question ID: {question_id}, Score: {score}'

def send_msg_to_server(input_text):
    try:
        # 构造请求数据
        payload = {"text": input_text}
        headers = {"Content-Type": "application/json"}
        
        # 发送请求
        response = requests.post(API_URL, json=payload, headers=headers)
        response.raise_for_status()  # 检查是否请求成功
        
        # 返回响应结果
        result = response.json()  # 假设服务器返回的是 JSON 格式
        return result.get("processed_text", "No result returned.")
    except requests.RequestException as e:
        return f"请求失败:{e}"