credent007 commited on
Commit
6ae1160
·
verified ·
1 Parent(s): 758e8e3

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +8 -95
inference.py CHANGED
@@ -25,22 +25,26 @@ def process_document(image):
25
 
26
  text = processor.apply_chat_template(
27
  messages,
28
- tokenize=False,
29
- add_generation_prompt=True
30
  )
 
31
 
32
  inputs = processor(
33
  text=[text],
34
  images=[image],
35
  return_tensors="pt"
36
  ).to(device)
37
-
 
38
  output = model.generate(
39
  **inputs,
40
  max_new_tokens=1500,
41
  do_sample=False, # if it is true there will be extra text with output
42
  # temperature=0.1 # temp is not required
43
  )
 
 
44
 
45
  generated_ids = output[0][inputs.input_ids.shape[-1]:]
46
 
@@ -73,95 +77,4 @@ def process_document(image):
73
  "raw": response
74
  }
75
 
76
- return parsed
77
- # import json
78
- # from model_loader import get_model
79
- # from processor_utils import load_input
80
- # from prompt import get_prompt
81
-
82
-
83
- # def _extract_json_block(text):
84
- # start = text.find("{")
85
- # end = text.rfind("}") + 1
86
-
87
- # if start == -1 or end == 0:
88
- # return None
89
-
90
- # return text[start:end]
91
-
92
-
93
- # def _run_page_inference(image, model, processor, device):
94
- # messages = [
95
- # {
96
- # "role": "user",
97
- # "content": [
98
- # {"type": "image", "image": image},
99
- # {"type": "text", "text": get_prompt()}
100
- # ]
101
- # }
102
- # ]
103
-
104
- # text = processor.apply_chat_template(
105
- # messages,
106
- # tokenize=False,
107
- # add_generation_prompt=True
108
- # )
109
-
110
- # inputs = processor(
111
- # text=[text],
112
- # images=[image],
113
- # return_tensors="pt"
114
- # ).to(device)
115
-
116
- # output = model.generate(
117
- # **inputs,
118
- # max_new_tokens=150,
119
- # do_sample=False
120
- # )
121
-
122
- # generated_ids = output[0][inputs.input_ids.shape[-1]:]
123
-
124
- # response = processor.decode(
125
- # generated_ids,
126
- # skip_special_tokens=True
127
- # ).strip()
128
-
129
- # json_block = _extract_json_block(response)
130
-
131
- # if not json_block:
132
- # return {
133
- # "status": "error",
134
- # "raw_output": response,
135
- # "parsed": None
136
- # }
137
-
138
- # try:
139
- # parsed = json.loads(json_block)
140
- # return {
141
- # "status": "success",
142
- # "raw_output": response,
143
- # "parsed": parsed
144
- # }
145
- # except json.JSONDecodeError:
146
- # return {
147
- # "status": "error",
148
- # "raw_output": response,
149
- # "parsed": None
150
- # }
151
-
152
-
153
- # def process_document(file_path):
154
- # model, processor, device = get_model()
155
- # pages = load_input(file_path)
156
-
157
- # page_results = []
158
-
159
- # for page_number, image in enumerate(pages, start=1):
160
- # result = _run_page_inference(image, model, processor, device)
161
- # result["page_number"] = page_number
162
- # page_results.append(result)
163
-
164
- # return {
165
- # "total_pages": len(page_results),
166
- # "pages": page_results
167
- # }
 
25
 
26
  text = processor.apply_chat_template(
27
  messages,
28
+ tokenize=False, # so that this can return string output
29
+ add_generation_prompt=True # if true it will add extra on start and end
30
  )
31
+ print(f"The text of inference is {text}")
32
 
33
  inputs = processor(
34
  text=[text],
35
  images=[image],
36
  return_tensors="pt"
37
  ).to(device)
38
+ print(f"The inputs of inference is {inputs}")
39
+
40
  output = model.generate(
41
  **inputs,
42
  max_new_tokens=1500,
43
  do_sample=False, # if it is true there will be extra text with output
44
  # temperature=0.1 # temp is not required
45
  )
46
+ print(f"The output of inference is {output}")
47
+
48
 
49
  generated_ids = output[0][inputs.input_ids.shape[-1]:]
50
 
 
77
  "raw": response
78
  }
79
 
80
+ return parsed