| # 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 [] |