-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_processor.py
More file actions
50 lines (42 loc) · 1.78 KB
/
text_processor.py
File metadata and controls
50 lines (42 loc) · 1.78 KB
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
import json
import logging
import urllib.request
from config import ZHIPU_API_KEY, GLM_MODEL
from asr_client import load_hotwords
API_URL = "https://open.bigmodel.cn/api/anthropic/v1/messages"
class TextProcessor:
def __init__(self):
self._api_key = ZHIPU_API_KEY
self._hotwords = load_hotwords()
def improve(self, raw_text: str) -> str:
if not raw_text:
return raw_text
system_prompt = (
"你是一个语音输入后处理助手。用户给你一段语音识别的原始文本,"
"你需要:\n"
"1. 修正同音字错误和错别字\n"
"2. 添加正确的标点符号\n"
"3. 只输出修正后的文本,不要任何解释\n"
"如果原文已经正确,直接返回原文即可。"
)
if self._hotwords:
system_prompt += f"\n以下是用户的专业词汇参考:{self._hotwords},请优先使用这些词的正确写法。"
try:
headers = {
"Content-Type": "application/json",
"x-api-key": self._api_key,
"anthropic-version": "2023-06-01",
}
data = json.dumps({
"model": GLM_MODEL,
"max_tokens": 1024,
"system": system_prompt,
"messages": [{"role": "user", "content": raw_text}],
}).encode("utf-8")
req = urllib.request.Request(API_URL, data=data, headers=headers, method="POST")
resp = urllib.request.urlopen(req, timeout=30)
result = json.loads(resp.read().decode("utf-8"))
return result["content"][0]["text"].strip()
except Exception as e:
logging.warning("GLM 纠错失败: %s", e)
return raw_text