MCPcopy Create free account
hub / github.com/chatdoc-com/OCRFlux / process_task

Function process_task

ocrflux/client.py:147–201  ·  view source on GitHub ↗
(args, task_name, task_args)

Source from the content-addressed store, hash-verified

145 pass
146
147async def process_task(args, task_name, task_args):
148 COMPLETION_URL = f"{args.url}:{args.port}/v1/chat/completions"
149 MAX_RETRIES = args.max_page_retries
150 attempt = 0
151 while attempt < MAX_RETRIES:
152 if task_name == 'page_to_markdown':
153 query = build_page_to_markdown_query(args, *task_args)
154 elif task_name == 'element_merge_detect':
155 query = build_element_merge_detect_query(args, *task_args)
156 elif task_name == 'html_table_merge':
157 query = build_html_table_merge_query(args, *task_args)
158
159 query["temperature"] = 0.1 * attempt
160
161 try:
162 status_code, response_body = await apost(COMPLETION_URL, json_data=query)
163
164 if status_code != 200:
165 raise ValueError(f"Error http status {status_code}")
166
167 base_response_data = json.loads(response_body)
168 response_content = base_response_data["choices"][0]["message"]["content"]
169
170 if task_name == 'page_to_markdown':
171 model_response_json = json.loads(response_content)
172 page_response = PageResponse(**model_response_json)
173 natural_text = page_response.natural_text
174 markdown_element_list = []
175 for text in natural_text.split('\n\n'):
176 if text.startswith("<Image>") and text.endswith("</Image>"):
177 pass
178 elif text.startswith("<table>") and text.endswith("</table>"):
179 try:
180 new_text = table_matrix2html(text)
181 except:
182 new_text = text.replace("<t>","").replace("<l>","").replace("<lt>","")
183 markdown_element_list.append(new_text)
184 else:
185 markdown_element_list.append(text)
186 return_data = markdown_element_list
187
188 elif task_name == 'element_merge_detect':
189 return_data = eval(response_content)
190
191 elif task_name == 'html_table_merge':
192 if not (response_content.startswith("<table>") and response_content.endswith("</table>")):
193 raise ValueError("Response is not a table")
194 return_data = response_content
195
196 return return_data
197
198 except Exception as e:
199 traceback.print_exc()
200 attempt += 1
201 return None
202
203def build_document_text(page_to_markdown_result, element_merge_detect_result, html_table_merge_result):
204 page_to_markdown_keys = list(page_to_markdown_result.keys())

Callers 1

requestFunction · 0.70

Calls 6

PageResponseClass · 0.90
table_matrix2htmlFunction · 0.90
apostFunction · 0.70

Tested by

no test coverage detected