Views
No views yet
pip install anyparse-pythonconfig/config.yaml from AnyParse into your project directory.1# use modelscope (default)
2export ANYPARSE_MODEL_MIRROR="modelscope"
3
4# use huggingface
5export ANYPARSE_MODEL_MIRROR="huggingface"
6
7# download models
8anyparse-cli download --config config/config.yaml --model1# Sync
2from anyparse import AnyParser
3
4model = AnyParser(config="config/config.yaml")
5res = model.invoke(file = "/path/to/your_file")
6
7
8
9# or Async
10from anyparse import AsyncAnyParser
11
12model = AsyncAnyParser(config="config/config.yaml")
13res = await model.ainvoke(file = "/path/to/your_file")1# help
2
3anyparse-cli --help
4
5# parse file
6anyparse-cli parse --config config/config.yaml --file /path/to/your_file
7
8# start api server
9anyparse-cli api --config config/config.yaml
10
11# see allowed file types
12anyparse-cli allow --config config/config.yaml
13
14# see commands help
15anyparse-cli [COMMAND] --help1# start fastapi server and openai proxy
2## use restful api or openai client call
3anyparse-cli api --config config/config.yaml --host 0.0.0.0 --port 18007 --seckey 'your_custom_secret_key'1# openai
2from openai import OpenAI
3
4client = OpenAI(
5 base_url = "http://localhost:18007/anyparse/openai/v1",
6 api_key = "your_custom_secret_key",
7)
8## get model id and allowed file types
9print(client.models.list())
10
11
12
13## parse file
14import base64
15
16with open("1.pdf", "r", encoding="utf-8") as f:
17 text_content = f.read()
18
19encoded_bytes = base64.b64encode(text_content.encode('utf-8'))
20base64_str = encoded_bytes.decode('utf-8')
21
22response = client.chat.completions.create(
23 model="anyparse",
24 messages=[
25 {
26 "role": "user",
27 "content": [
28 {
29 "type": "file",
30 "file": {
31 "file_data": f"data:application/pdf;base64,{base64_str}"
32 }
33 }
34 ]
35 }
36 ], # data:application/pdf;base64 prefix follow: client.models.list().data[0].allow_mimetypes
37 # extra_body={
38 # "runtimes_args": {
39 # "use_doc_layout": True
40 # }
41 # }
42)
43
44print(response.choices[0].message.content)
45
46
47
48# or restful
49import requests as rq
50
51headers = {
52 "Authorization": "Bearer your_custom_secret_key"
53}
54
55url = "http://localhost:18007/anyparse/invoke/v1"
56
57args = {
58 "use_doc_cls": False,
59 "use_doc_rectifier": False,
60 "use_doc_layout": True
61}
62
63file = '/path/to/your_file'
64
65files = {
66 'file': open(file,'rb')
67}
68
69res = rq.post(url, files = files, data = args, headers = headers)
70print(res.json())
71