File size: 667 Bytes
06bc836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# api.py
import requests

SERVER = "http://192.168.10.3:5000/predict"

def fetch_candidates(text):
    print(f"API request text: {text}")
    if not text.strip():
        return []
    try:
        # 增加超时到 2 秒。如果 2 秒还没出结果,说明后端推理太慢。
        r = requests.post(
            SERVER,
            json={"text": text},
            timeout=2.0
        )
        if r.status_code == 200:
            data = r.json()
            print(f"API response data: {data}")
            return [x["word"] for x in data.get("candidates", [])]
    except Exception as e:
        print(f"API Error: {e}")
    return []