import time import os from os import getcwd, path import importlib.metadata from dotenv import load_dotenv def check_additional_requirements(): if importlib.util.find_spec("detectron2") is None: os.system('pip install detectron2@git+https://github.com/facebookresearch/detectron2.git') if importlib.util.find_spec("gradio") is not None: if importlib.metadata.version("gradio")!="3.4.1": os.system("pip uninstall -y gradio") os.system("pip install gradio==3.4.1") else: os.system("pip install gradio==3.4.1") os.system(os.environ["DD_ADDONS"]) return load_dotenv() check_additional_requirements() import deepdoctection as dd from deepdoctection.dataflow.serialize import DataFromList from deepdoctection.utils.settings import get_type from dd_addons.analyzer.loader import get_loader from dd_addons.extern.guidance import TOKEN_DEFAULT_INSTRUCTION from dd_addons.utils.settings import register_llm_token_tag, register_string_categories_from_list from dd_addons.extern.openai import OpenAiLmmTokenClassifier, is_api_key_valid import gradio as gr dd.Page.add_attribute_name("raw_json_output") analyzer = get_loader(reset_config_file=True, config_overwrite=["OCR.USE_TESSERACT=False", "OCR.USE_TEXTRACT=True", "WORD_MATCHING.MAX_PARENT_ONLY=True"]) demo = gr.Blocks(css="scrollbar.css") def process_analyzer(openai_api_key, categories_str, instruction_str, img, pdf, max_datapoints): if not is_api_key_valid(openai_api_key): return [], {}, "You have entered no or an invalid api key. Please enter a valid api key" categories_list = categories_str.split(",") if not categories_str: return [], {}, "You did not enter any entities. Please enter a at least one category." register_string_categories_from_list(categories_list, "custom_token_classes") custom_token_class = dd.object_types_registry.get("custom_token_classes") print([token_class for token_class in custom_token_class]) register_llm_token_tag([token_class for token_class in custom_token_class]) categories = { str(idx + 1): get_type(val) for idx, val in enumerate(categories_list) } gpt_token_classifier = OpenAiLmmTokenClassifier( model_name="gpt-3.5-turbo", categories=categories, api_key=openai_api_key, instruction= instruction_str if instruction_str else None, ) analyzer.pipe_component_list[8].language_model = gpt_token_classifier if img is not None: image = dd.Image(file_name=str(time.time()).replace(".","") + ".png", location="") image.image = img[:, :, ::-1] df = DataFromList(lst=[image]) df = analyzer.analyze(dataset_dataflow=df) elif pdf: df = analyzer.analyze(path=pdf.name, max_datapoints=max_datapoints) else: raise ValueError df.reset_state() json_out = {} dpts = [] json_out_raw = {} for idx, dp in enumerate(df): dpts.append(dp) json_out[f"page_{idx}"] = dp.get_token() json_out_raw[f"page_{idx}"] = dp.raw_json_output return [dp.viz(show_cells=False, show_layouts=False, show_tables=False, show_words=True, show_token_class=True, ignore_default_token_class=True) for dp in dpts], json_out, json_out_raw, "No error" with demo: with gr.Box(): gr.Markdown("