Spaces:
Sleeping
Sleeping
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}" |