first commit

This commit is contained in:
yumoqing 2025-07-16 14:29:03 +08:00
commit a1d9b7e524
7 changed files with 130 additions and 0 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# checklang

48
app/checklang.py Normal file
View File

@ -0,0 +1,48 @@
import os
from ahserver.webapp import webapp
from ahserver.serverenv import ServerEnv
from appPublic.registerfunction import RegisterFunction
import fasttext
def docs(request, *args, **kw):
return """Check langage for text
path: /v1/checklang
method: POST
headers:
Content-Text: application/json
data:
{
"text": "what is language of this sentence? "
}
response:
{
"lang": "en"
}
"""
def checklang(request, params_kw, *args, **kw):
engine = kw.get('engine')
text = params_kw.text
pred = engine.predict(text)
d = pred[0][0][9:]
return {
'lang':d
}
def init():
p = os.path.join(os.path.dirname(__file__), 'lid.176.ftz')
print(f'model path={p}')
model = fasttext.load_model(p)
# 需先下载模型https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.ftz
env = ServerEnv()
env.engine = model
rf = RegisterFunction()
rf.register('checklang', checklang)
if __name__ == '__main__':
rf = RegisterFunction()
rf.register('checklang', checklang)
rf.register('docs', docs)
webapp(init)

BIN
app/lid.176.ftz Normal file

Binary file not shown.

28
conf/config.json Executable file
View File

@ -0,0 +1,28 @@
{
"website":{
"paths":[
["$[workdir]$/wwwroot",""]
],
"client_max_size":10000,
"host":"0.0.0.0",
"port":9080,
"coding":"utf-8",
"indexes":[
"index.html"
],
"startswiths":[
{
"leading":"/docs",
"registerfunction":"docs"
},{
"leading":"/v1/checklang",
"registerfunction":"checklang"
}
],
"processors":[
[".tmpl","tmpl"]
],
"session_max_time":3000,
"session_issue_time":2500
}
}

47
install.sh Executable file
View File

@ -0,0 +1,47 @@
port=$1
userid=$(id -un)
groupid=$(id -gn)
workdir=$(pwd)
package=$(basename $workdir)
cat <<EOF > $package.service
[Unit]
Wants=systemd-networkd.service
[Service]
User=$userid
Group=$groupid
WorkingDirectory=$workdir
Type=forking
ExecStart=$workdir/start.sh
ExecStop=$workdir/stop.sh
StandardOutput=append:/var/log/$package/$package.log
StandardError=append:/var/log/$package/$package.log
SyslogIdentifier=$package
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF > $workdir/start.sh
#!/usr/bin/bash
$workdir/$package.env/bin/python $workdir/app/$package.py -p $port
EOF
cat <<EOF > $workdir/stop.sh
#!/usr/bin/bash
ps -ef|grep "$package.py"|grep -v grep|awk '{print("kill -9", $2)}'|sh
EOF
chmod +x $workdir/*.sh
python3 -m venv $package.env
$package.env/bin/pip install setuptools wheel
$package.env/bin/pip install -r requirements.txt
sudo mkdir /var/log/$package
sudo cp $package.service /etc/systemd/system
sudo systemctl enable $package
sudo systemctl start $package

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
fasttext
git+https://git.kaiyuancloud.cn/yumoqing/apppublic
git+https://git.kaiyuancloud.cn/yumoqing/sqlor
git+https://git.kaiyuancloud.cn/yumoqing/ahserver

0
wwwroot/index.html Normal file
View File