Spaces:
Paused
Paused
few changes for logging
Browse files
app.py
CHANGED
@@ -8,6 +8,9 @@ import torch
|
|
8 |
from ast import literal_eval
|
9 |
from PIL import Image
|
10 |
|
|
|
|
|
|
|
11 |
# Load the model on the available device(s)
|
12 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
13 |
"Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
|
@@ -56,14 +59,14 @@ def demo(image_path, prompt):
|
|
56 |
],
|
57 |
}
|
58 |
]
|
59 |
-
|
60 |
# Preparation for inference
|
61 |
text = processor.apply_chat_template(
|
62 |
messages, tokenize=False, add_generation_prompt=True
|
63 |
)
|
64 |
-
|
65 |
image_inputs, video_inputs = process_vision_info(messages)
|
66 |
-
|
67 |
inputs = processor(
|
68 |
text=[text],
|
69 |
images=image_inputs,
|
@@ -71,22 +74,23 @@ def demo(image_path, prompt):
|
|
71 |
padding=True,
|
72 |
return_tensors="pt",
|
73 |
)
|
74 |
-
|
75 |
inputs = inputs.to("cuda")
|
76 |
-
|
77 |
# Inference: Generation of the output
|
78 |
generated_ids = model.generate(**inputs, max_new_tokens=1500)
|
79 |
-
|
80 |
generated_ids_trimmed = [
|
81 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
82 |
]
|
83 |
-
|
84 |
output_text = processor.batch_decode(
|
85 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
86 |
)
|
87 |
-
|
88 |
|
89 |
# Handle output text to convert it into JSON
|
|
|
90 |
try:
|
91 |
almost_json = output_text[0].split('\n')[-1].split('\n')[0]
|
92 |
json = literal_eval(almost_json)
|
@@ -100,9 +104,10 @@ def process_document(image):
|
|
100 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
101 |
image.save(tmp_file.name) # Save the image to the temporary file
|
102 |
image_path = tmp_file.name # Get the path of the saved file
|
103 |
-
|
104 |
# Process the image with your model
|
105 |
one = demo(image_path, other_benifits)
|
|
|
106 |
two = demo(image_path, tax_deductions)
|
107 |
json_op = {
|
108 |
"tax_deductions": one,
|
|
|
8 |
from ast import literal_eval
|
9 |
from PIL import Image
|
10 |
|
11 |
+
import logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
|
14 |
# Load the model on the available device(s)
|
15 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
16 |
"Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
|
|
|
59 |
],
|
60 |
}
|
61 |
]
|
62 |
+
logging.info("Step 1: Preparing inference")
|
63 |
# Preparation for inference
|
64 |
text = processor.apply_chat_template(
|
65 |
messages, tokenize=False, add_generation_prompt=True
|
66 |
)
|
67 |
+
logging.info("2")
|
68 |
image_inputs, video_inputs = process_vision_info(messages)
|
69 |
+
logging.info("3")
|
70 |
inputs = processor(
|
71 |
text=[text],
|
72 |
images=image_inputs,
|
|
|
74 |
padding=True,
|
75 |
return_tensors="pt",
|
76 |
)
|
77 |
+
logging.info("4")
|
78 |
inputs = inputs.to("cuda")
|
79 |
+
logging.info("5")
|
80 |
# Inference: Generation of the output
|
81 |
generated_ids = model.generate(**inputs, max_new_tokens=1500)
|
82 |
+
logging.info("6")
|
83 |
generated_ids_trimmed = [
|
84 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
85 |
]
|
86 |
+
logging.info("7")
|
87 |
output_text = processor.batch_decode(
|
88 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
89 |
)
|
90 |
+
logging.info("8", output_text)
|
91 |
|
92 |
# Handle output text to convert it into JSON
|
93 |
+
json = str()
|
94 |
try:
|
95 |
almost_json = output_text[0].split('\n')[-1].split('\n')[0]
|
96 |
json = literal_eval(almost_json)
|
|
|
104 |
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
105 |
image.save(tmp_file.name) # Save the image to the temporary file
|
106 |
image_path = tmp_file.name # Get the path of the saved file
|
107 |
+
logging.info(image_path)
|
108 |
# Process the image with your model
|
109 |
one = demo(image_path, other_benifits)
|
110 |
+
logging.info("kjf")
|
111 |
two = demo(image_path, tax_deductions)
|
112 |
json_op = {
|
113 |
"tax_deductions": one,
|