# flash-attn takes ~15 hours to compile: https://github.com/Dao-AILab/flash-attention/issues/1038
mkdir -p model
while ! PATH=~/.local/bin:$PATH huggingface-cli download Tele-AI/TeleChat2-115B --local-dir $(pwd)/model; do if df --output=avail -h .|tail -1|[1-9][0-9]*G; then echo Retry in 1 minute; sleep 60; else echo Disk full error; exit 1; fi; done
# - above is best done on wired network (and don't allow machine to suspend; some implementations even seem to stop the download when screensaver is in use)
# - and that 115B model is 423 gigabytes (not counting the approximately 15 gigabytes required for the tools) so I hope you have space on your hard drive: a "500G" drive is NOT likely to be sufficient once OS and inode usage is taken out.
# And it doesn't work anyway: .cache/huggingface/modules/transformers_modules/model/modeling_telechat.py", line 187, in forward: assert all((i.is_cuda for i in (q, k, v)))
# (after 1 hour 5 minutes running on a 100Mbps network drive; probably about 14 minutes on a local disk)
# Meanwhile, donloading a smaller model instead: Tele-AI/telechat-7B (takes 14 gigabytes for the model)
# gets: RuntimeError: FlashAttention only supports Ampere GPUs or newer.
if"translation"inenor"incorrect"inenorre.search(r"\btypo\b",en):continue# alternate translation, old translation, typo, etc
en=re.sub(" ([^)]*name[^)]*)","",en)# "X (surname)" is ok if X is capitals only
if"("inen:continue# we don't want entries like "notice (on paper)" where the parens indicate it's a specific meaning
ifre.search("; [^A-Z]",en):continue# "here; in this region" probably shouldn't pull out "here", but ";" + abbreviation is probably OK, as is "Name; Name"
forenin[een.strip()foreeninre.sub("[(][^)]*[)]","",en).split(';')]:# relevant only if commenting out the "(" continue above
fortaginre.findall(r"(?:<[^>]*>\s*)+",txt,flags=re.DOTALL):e2c[tag]=tag# keep (runs of) tags, TODO: might be better if we don't make them sentence objects
fori,kinenumerate(keyList):# TODO: this loop is slow: might want to get an annogen-generated annotator to do it (but there's the \b) or make an OR list like the annogen normaliser
ifk.startswith("<"):txt=txt.replace(k," {%d} "%i)# irrespective of word boundaries
else:
txt=re.sub(r"\b"+re.escape(k)+r"\b"," {%d} "%i,txt,flags=0ifre.search("[A-Z]",k)elsere.IGNORECASE)# (don't match lower case if we have upper case, as it might be a name or abbreviation that in lower case will be a normal word and not this entry (TODO there can be false positives at start of sentences though), but do match title case if we are lower case)
sentences=re.findall(r"[^ .!?].*?(?:$|[.!?])(?=$|\s+)",txt,flags=re.DOTALL)# TODO: at one point didn't include a <p> (prob repr'd as {..}) at start of sentence, regex seems ok, did model drop it?
answer,history=model.chat(tokenizer=tokeniser,question=f"请把这个英文句子翻译成汉语:【{s}】做这个翻译,以下个词典条目也许有用:{';'.join(f'{k}={v}' for k in e2c.items() if k.lower() in s.lower())}",history=history,generation_config=gcfg,stream=False)