mirror of
https://github.com/labring/FastGPT.git
synced 2026-03-02 01:02:30 +08:00
4.8.11 perf (#2832)
* save toast * perf: surya ocr * perf: remove same model name * fix: indexes * perf: ip check * feat: Fixed the version number of the subapplication * feat: simple app get latest child version * perf: update child dispatch variables * feat: variables update doc
This commit is contained in:
110
python/ocr/surya/README.md
Normal file
110
python/ocr/surya/README.md
Normal file
File diff suppressed because one or more lines are too long
@@ -19,7 +19,8 @@ from surya.model.recognition.model import load_model as load_rec_model
|
||||
from surya.model.recognition.processor import load_processor as load_rec_processor
|
||||
from surya.ocr import run_ocr
|
||||
from surya.schema import OCRResult
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore", category=FutureWarning, module="transformers")
|
||||
app = FastAPI()
|
||||
security = HTTPBearer()
|
||||
env_bearer_token = None
|
||||
@@ -100,40 +101,53 @@ class Chat(object):
|
||||
return string_result
|
||||
|
||||
def query_ocr(self, image_base64: str,
|
||||
sorted: bool) -> List[OCRResult] | str:
|
||||
sorted: bool) -> str:
|
||||
if image_base64 is None or len(image_base64) == 0:
|
||||
return []
|
||||
image = Chat.base64_to_image(image_base64)
|
||||
return ""
|
||||
try:
|
||||
image = Chat.base64_to_image(image_base64)
|
||||
ocr_result = self.surya.run(image)
|
||||
result = []
|
||||
|
||||
ocr_result = self.surya.run(image)
|
||||
result = []
|
||||
for text_line in ocr_result[0].text_lines:
|
||||
result.append(text_line.text)
|
||||
|
||||
for text_line in ocr_result[0].text_lines:
|
||||
result.append({"text": text_line.text, "bbox": text_line.bbox})
|
||||
if sorted:
|
||||
result = Chat.sort_text_by_bbox(result)
|
||||
if sorted:
|
||||
result = self.sort_text_lines(result)
|
||||
|
||||
torch_gc()
|
||||
return result
|
||||
# 将所有文本行合并成一个字符串,用换行符分隔
|
||||
final_result = "\n".join(result)
|
||||
|
||||
torch_gc()
|
||||
return final_result
|
||||
except Exception as e:
|
||||
logging.error(f"OCR 处理失败: {e}")
|
||||
raise HTTPException(status_code=400, detail=f"OCR 处理失败: {str(e)}")
|
||||
|
||||
@app.post('/v1/surya_ocr')
|
||||
@staticmethod
|
||||
def sort_text_lines(text_lines: List[str]) -> List[str]:
|
||||
# 这里可以实现自定义的排序逻辑
|
||||
# 目前只是简单地返回原始列表,因为我们没有位置信息来进行排序
|
||||
return text_lines
|
||||
|
||||
@app.post('/v1/ocr/text')
|
||||
async def handle_post_request(
|
||||
image_req: ImageReq,
|
||||
credentials: HTTPAuthorizationCredentials = Security(security)):
|
||||
token = credentials.credentials
|
||||
if env_bearer_token is not None and token != env_bearer_token:
|
||||
raise HTTPException(status_code=401, detail="Invalid token")
|
||||
raise HTTPException(status_code=401, detail="无效的令牌")
|
||||
chat = Chat()
|
||||
try:
|
||||
results = []
|
||||
for image_base64 in image_req.images:
|
||||
results.append(chat.query_ocr(image_base64, image_req.sorted))
|
||||
return {"error": "success", "results": results}
|
||||
return {"error": None, "results": results}
|
||||
except HTTPException as he:
|
||||
raise he
|
||||
except Exception as e:
|
||||
logging.error(f"识别报错:{e}")
|
||||
return {"error": "识别出错"}
|
||||
|
||||
raise HTTPException(status_code=500, detail=f"识别出错: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
env_bearer_token = os.getenv("ACCESS_TOKEN")
|
||||
@@ -1,120 +0,0 @@
|
||||
# 接入Surya OCR文本检测
|
||||
|
||||
## 源码部署
|
||||
|
||||
### 1. 安装环境
|
||||
|
||||
- Python 3.9+
|
||||
- CUDA 11.8
|
||||
- 科学上网环境
|
||||
|
||||
### 2. 安装依赖
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### 3. 下载模型
|
||||
|
||||
代码首次运行时会自动从huggingface下载模型,可跳过以下步骤。
|
||||
也可以手动下载模型,在对应代码目录下clone模型
|
||||
|
||||
```sh
|
||||
mkdir vikp && cd vikp
|
||||
|
||||
git lfs install
|
||||
|
||||
git clone https://huggingface.co/vikp/surya_det3
|
||||
# 镜像下载 https://hf-mirror.com/vikp/surya_det3
|
||||
|
||||
git clone https://huggingface.co/vikp/surya_rec2
|
||||
# 镜像下载 https://hf-mirror.com/vikp/surya_rec2
|
||||
```
|
||||
|
||||
最终手动下载的目录结构如下:
|
||||
|
||||
```
|
||||
vikp/surya_det3
|
||||
vikp/surya_rec2
|
||||
app.py
|
||||
Dockerfile
|
||||
requirements.txt
|
||||
```
|
||||
|
||||
### 4. 运行代码
|
||||
|
||||
```bash
|
||||
python app.py
|
||||
```
|
||||
|
||||
对应请求地址为
|
||||
`http://0.0.0.0:7230/v1/surya_ocr`
|
||||
|
||||
### 5. 测试
|
||||
|
||||
```python
|
||||
import requests
|
||||
import base64
|
||||
|
||||
IMAGE_PATH = "your/path/to/image.png"
|
||||
ACCESS_TOKEN = "your_access_token"
|
||||
|
||||
with open(IMAGE_PATH, 'rb') as img_file:
|
||||
encoded_string = base64.b64encode(img_file.read())
|
||||
encoded_image = encoded_string.decode('utf-8')
|
||||
data = {"images": [encoded_image], "sorted": True}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {ACCESS_TOKEN}"
|
||||
}
|
||||
res = requests.post(url="http://0.0.0.0:7230/v1/surya_ocr",
|
||||
headers=headers,
|
||||
json=data)
|
||||
|
||||
print(res.text)
|
||||
```
|
||||
|
||||
## docker部署
|
||||
|
||||
### 镜像获取
|
||||
|
||||
**本地编译镜像:**
|
||||
```bash
|
||||
docker build -t surya_ocr:v0.1 .
|
||||
```
|
||||
|
||||
**或拉取线上镜像:**
|
||||
Todo:待发布
|
||||
|
||||
### docker-compose.yml示例
|
||||
```yaml
|
||||
version: '3'
|
||||
services:
|
||||
surya-ocr:
|
||||
image: surya_ocr:v0.1
|
||||
container_name: surya-ocr
|
||||
# GPU运行环境,如果宿主机未安装,将deploy配置隐藏即可
|
||||
deploy:
|
||||
resources:
|
||||
reservations:
|
||||
devices:
|
||||
- driver: nvidia
|
||||
count: all
|
||||
capabilities: [gpu]
|
||||
ports:
|
||||
- 7230:7230
|
||||
environment:
|
||||
- BATCH_SIZE=32
|
||||
- ACCESS_TOKEN=YOUR_ACCESS_TOKEN
|
||||
- LANGS='["zh","en"]'
|
||||
```
|
||||
**环境变量:**
|
||||
```
|
||||
BATCH_SIZE:根据实际内存/显存情况配置,每个batch约占用40MB的VRAM,cpu默认32,mps默认64,cuda默认512
|
||||
ACCESS_TOKEN:服务的access_token
|
||||
LANGS:支持的语言列表,默认["zh","en"]
|
||||
```
|
||||
|
||||
## 接入FastGPT
|
||||
|
||||
Todo: 待补充
|
||||
Reference in New Issue
Block a user