Spaces:
Running
Running
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, question_id, score): | |
score_data = { | |
"user_id": user_id, | |
"question_id": question_id, | |
"score": score | |
} | |
if os.path.exists("score.json"): | |
with open("score.json", "r") as file: | |
data = json.load(file) | |
else: | |
data = [] | |
data.append(score_data) | |
with open("score.json", "w") as file: | |
json.dump(data, file, indent=4) | |
return f'Time {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())} UCT-8, score submitted successfully.' | |
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}" |