Spaces:
Paused
Paused
Update inference.py
Browse files24 unmodified lines2526272829303132333435363738394041424344454626 unmodified lines73747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616724 unmodified linestext = processor.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)inputs = processor(text=[text],images=[image],return_tensors="pt").to(device)output = model.generate(**inputs,max_new_tokens=1500,do_sample=False, # if it is true there will be extra text with output# temperature=0.1 # temp is not required)generated_ids = output[0][inputs.input_ids.shape[-1]:]26 unmodified lines"raw": response}return parsed# import json# from model_loader import get_model# from processor_utils import load_input# from prompt import get_prompt# def _extract_json_block(text):# start = text.find("{")# end = text.rfind("}") + 1# if start == -1 or end == 0:# return None# return text[start:end]# def _run_page_inference(image, model, processor, device):# messages = [# {# "role": "user",# "content": [# {"type": "image", "image": image},# {"type": "text", "text": get_prompt()}# ]# }# ]# text = processor.apply_chat_template(# messages,# tokenize=False,# add_generation_prompt=True# )# inputs = processor(# text=[text],# images=[image],# return_tensors="pt"# ).to(device)# output = model.generate(# **inputs,# max_new_tokens=150,# do_sample=False# )# generated_ids = output[0][inputs.input_ids.shape[-1]:]# response = processor.decode(# generated_ids,# skip_special_tokens=True# ).strip()# json_block = _extract_json_block(response)# if not json_block:# return {# "status": "error",# "raw_output": response,# "parsed": None# }# try:# parsed = json.loads(json_block)# return {# "status": "success",# "raw_output": response,# "parsed": parsed# }# except json.JSONDecodeError:# return {# "status": "error",# "raw_output": response,# "parsed": None# }# def process_document(file_path):# model, processor, device = get_model()# pages = load_input(file_path)# page_results = []# for page_number, image in enumerate(pages, start=1):# result = _run_page_inference(image, model, processor, device)# result["page_number"] = page_number# page_results.append(result)# return {# "total_pages": len(page_results),# "pages": page_results# }24 unmodified lines252627282930313233343536373839404142434445464748495026 unmodified lines7778798024 unmodified linestext = processor.apply_chat_template(messages,tokenize=False, # so that this can return string outputadd_generation_prompt=True # if true it will add extra on start and end)print(f"The text of inference is {text}")inputs = processor(text=[text],images=[image],return_tensors="pt").to(device)print(f"The inputs of inference is {inputs}")output = model.generate(**inputs,max_new_tokens=1500,do_sample=False, # if it is true there will be extra text with output# temperature=0.1 # temp is not required)print(f"The output of inference is {output}")generated_ids = output[0][inputs.input_ids.shape[-1]:]26 unmodified lines"raw": response}return parsed
inference.py+8-95