1.软件准备
1.1 下载Anaconda
Anaconda(大蟒蛇),一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项,其中的conda可以提供虚拟环境管理,构建AI环境特别方便。
直接官网下载 https://www.anaconda.com/download (需要梯子),进入后直接输入邮箱,官网会往邮箱发送一个下载链接
可以直接通过百度网盘下载
链接: https://pan.baidu.com/s/1KTbeurKl-mZUq14mF1LgKA?pwd=6qs7 提取码: 6qs7
1.2下载GOT-OCR2.0-cpu-support
去github下载 https://github.com/ElvisClaros/GOT-OCR2.0/tree/cpu-support(需要梯子)
百度网盘下载
链接: https://pan.baidu.com/s/1BH7mm0FT9gJENyaW7IrG8A?pwd=g9zj 提取码: g9zj
1.3下载GOT-OCR-CPU模型
在抱抱脸下载 https://huggingface.co/srimanth-d/GOT_CPU (需要梯子)
切换标签逐个文件下载,放到文件夹GOT-CPU
,上传到服务器上
2.安装Anaconda
将安装文件上传到服务器,并cd到对应目录
##执行命令,会进入安装页面,需要阅读好长的协议一直按回车就行只到出现下图
bash Anaconda3-2024.10-1-Linux-x86_64.sh
确认安装目录,最好手动输入一下找个空间大的目录
接下载来就是漫长的等待,只到提示这些内容输入yes就安装完成了
##使配置文件生效
source ~/.bashrc
##检查是否安装完成,有版本号展示就行
conda -V
3.构建虚拟环境以及GOT-OCR
创建虚拟环境
##创建一个名为 mygot 且python版本为3.10的虚拟环境
conda create -n mygot python=3.10 -y
##激活虚拟环境
conda activate mygot
##激活后命令行前方会显示虚拟环境的名称,类似下面的样子
(mygot) [root@sybj-int-83 ylbzj]#
安装GOT-COR
##将zip包上传至服务器并cd到对应目录,并解压
cd /opt/ylbzj
unzip GOT-OCR2.0-cpu-support.zip
##安装albumentations,否则会报下图的错误,也有可能是不支持avx指令,联系运维开启avx指令或者自行deepseek
pip install albumentations==1.4.20
##进入目录执行安装
cd GOT-OCR2.0-cpu-support/GOT-OCR-2.0-master
pip install -e .
看到下面的就是安装成功了
安装的是cpu版本,git上所说的下面两个命令无需执行
pip install ninja
pip install flash-attn --no-build-isolation
4.测试
将以下脚本创建为test_got_ocr.py
from transformers import AutoModel, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('/opt/ylbzj/GOT-CPU', trust_remote_code=True)
model = AutoModel.from_pretrained('/opt/ylbzj/GOT-CPU', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
model = model.eval()
# input your test image
image_file = '/opt/ylbzj/WechatIMG436.jpg'
# plain texts OCR
res = model.chat(tokenizer, image_file, ocr_type='ocr')
print(res)
在虚拟环境下执行python test_got_ocr.py
报如下操作:
执行一下命令即可
pip install verovio
再次验证
5.提供API服务
用以下脚本创建名为GOT-OCR-Api.py
执行以下命令后台运行服务,需要注意脚本路径 nohup conda run -n mygot python GOT-OCR-Api.py > got-ocr-api.log 2>&1 &
执行这个脚本需要向安装flask,执行命令pip install flask
from flask import Flask, request, jsonify
from transformers import AutoModel, AutoTokenizer
app = Flask(__name__)
# Initialize the tokenizer and model once globally
tokenizer = AutoTokenizer.from_pretrained('/opt/ylbzj/GOT-CPU', trust_remote_code=True)
model = AutoModel.from_pretrained(
'/opt/ylbzj/GOT-CPU',
trust_remote_code=True,
low_cpu_mem_usage=True,
use_safetensors=True,
pad_token_id=tokenizer.eos_token_id
)
model.eval()
@app.route('/ocr/txt', methods=['POST'])
def ocr():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# Save the uploaded file temporarily
image_path = "/opt/ylbzj/GOT-OCR-API-File/" + file.filename
file.save(image_path)
# Perform OCR
res = model.chat(tokenizer, image_path, ocr_type='ocr')
return jsonify({'result': res})
@app.route('/ocr/format', methods=['POST'])
def ocrFormat():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# Save the uploaded file temporarily
image_path = "/opt/ylbzj/GOT-OCR-API-File/" + file.filename
file.save(image_path)
# Perform OCR
res = model.chat(tokenizer, image_path, ocr_type='format')
return jsonify({'result': res})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8066)