Spaces:
Paused
Paused
two op
Browse files
app.py
CHANGED
@@ -47,11 +47,10 @@ tax_deductions = '''Extract the following information in the given format:
|
|
47 |
|
48 |
|
49 |
def process_document(image):
|
50 |
-
# Save the uploaded image to a temporary file
|
51 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
52 |
-
image = Image.fromarray(image)
|
53 |
-
image.save(tmp_file.name)
|
54 |
-
image_path = tmp_file.name
|
55 |
|
56 |
|
57 |
messages = [
|
@@ -60,7 +59,7 @@ def process_document(image):
|
|
60 |
"content": [
|
61 |
{
|
62 |
"type": "image",
|
63 |
-
"image": image_path,
|
64 |
},
|
65 |
{"type": "text", "text": '''Extract the following information in the given format:
|
66 |
{
|
@@ -72,8 +71,7 @@ def process_document(image):
|
|
72 |
'california:': {
|
73 |
'withholding tax:': {'Amount':'', 'Year-To_Date':""},
|
74 |
'ee disability tax:': {'Amount':'', 'Year-To-Date':""}}},
|
75 |
-
}
|
76 |
-
'''},
|
77 |
],
|
78 |
}
|
79 |
]
|
@@ -89,7 +87,6 @@ def process_document(image):
|
|
89 |
return_tensors="pt",
|
90 |
)
|
91 |
inputs = inputs.to("cuda")
|
92 |
-
# Inference: Generation of the output
|
93 |
generated_ids = model.generate(**inputs, max_new_tokens=1500)
|
94 |
generated_ids_trimmed = [
|
95 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
@@ -97,15 +94,12 @@ def process_document(image):
|
|
97 |
output_text = processor.batch_decode(
|
98 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
99 |
)
|
100 |
-
# Handle output text to convert it into JSON
|
101 |
try:
|
102 |
-
# almost_json = output_text[0].replace('```\n', '').replace('\n```', '')
|
103 |
almost_json = output_text[0].split('```\n')[-1].split('\n```')[0]
|
104 |
|
105 |
json = literal_eval(almost_json)
|
106 |
except:
|
107 |
try:
|
108 |
-
# almost_json = output_text[0].replace('```json\n', '').replace('\n```', '')
|
109 |
almost_json = output_text[0].split('```json\n')[-1].split('\n```')[0]
|
110 |
json = literal_eval(almost_json)
|
111 |
except:
|
@@ -117,7 +111,7 @@ def process_document(image):
|
|
117 |
"content": [
|
118 |
{
|
119 |
"type": "image",
|
120 |
-
"image": image_path,
|
121 |
},
|
122 |
{"type": "text", "text": '''Extract the following information in the given format:
|
123 |
{'other_benefits_and_information': {
|
@@ -129,8 +123,7 @@ def process_document(image):
|
|
129 |
}
|
130 |
'payment method': 'eg. Direct payment',
|
131 |
'Amount': 'eg. 12.99'
|
132 |
-
}
|
133 |
-
'''},
|
134 |
],
|
135 |
}
|
136 |
]
|
@@ -165,20 +158,20 @@ def process_document(image):
|
|
165 |
except:
|
166 |
json_2 = output_text[0]
|
167 |
|
168 |
-
json_op = {
|
169 |
-
|
170 |
-
|
171 |
-
}
|
172 |
-
# Optionally, you can delete the temporary file after use
|
173 |
os.remove(image_path)
|
174 |
|
175 |
-
return
|
176 |
|
177 |
# Create Gradio interface
|
178 |
demo = gr.Interface(
|
179 |
fn=process_document,
|
180 |
inputs="image", # Gradio will handle the image input
|
181 |
-
outputs="json",
|
182 |
title="PaySlip_Demo_Model",
|
183 |
examples=[["Slip_1.jpg"], ["Slip_2.jpg"]],
|
184 |
cache_examples=False
|
|
|
47 |
|
48 |
|
49 |
def process_document(image):
|
|
|
50 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
51 |
+
image = Image.fromarray(image)
|
52 |
+
image.save(tmp_file.name)
|
53 |
+
image_path = tmp_file.name
|
54 |
|
55 |
|
56 |
messages = [
|
|
|
59 |
"content": [
|
60 |
{
|
61 |
"type": "image",
|
62 |
+
"image": image_path,
|
63 |
},
|
64 |
{"type": "text", "text": '''Extract the following information in the given format:
|
65 |
{
|
|
|
71 |
'california:': {
|
72 |
'withholding tax:': {'Amount':'', 'Year-To_Date':""},
|
73 |
'ee disability tax:': {'Amount':'', 'Year-To-Date':""}}},
|
74 |
+
}'''},
|
|
|
75 |
],
|
76 |
}
|
77 |
]
|
|
|
87 |
return_tensors="pt",
|
88 |
)
|
89 |
inputs = inputs.to("cuda")
|
|
|
90 |
generated_ids = model.generate(**inputs, max_new_tokens=1500)
|
91 |
generated_ids_trimmed = [
|
92 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
|
|
94 |
output_text = processor.batch_decode(
|
95 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
96 |
)
|
|
|
97 |
try:
|
|
|
98 |
almost_json = output_text[0].split('```\n')[-1].split('\n```')[0]
|
99 |
|
100 |
json = literal_eval(almost_json)
|
101 |
except:
|
102 |
try:
|
|
|
103 |
almost_json = output_text[0].split('```json\n')[-1].split('\n```')[0]
|
104 |
json = literal_eval(almost_json)
|
105 |
except:
|
|
|
111 |
"content": [
|
112 |
{
|
113 |
"type": "image",
|
114 |
+
"image": image_path,
|
115 |
},
|
116 |
{"type": "text", "text": '''Extract the following information in the given format:
|
117 |
{'other_benefits_and_information': {
|
|
|
123 |
}
|
124 |
'payment method': 'eg. Direct payment',
|
125 |
'Amount': 'eg. 12.99'
|
126 |
+
}'''},
|
|
|
127 |
],
|
128 |
}
|
129 |
]
|
|
|
158 |
except:
|
159 |
json_2 = output_text[0]
|
160 |
|
161 |
+
# json_op = {
|
162 |
+
# "tax_deductions": json,
|
163 |
+
# "other_benifits": json_2
|
164 |
+
# }
|
165 |
+
# # Optionally, you can delete the temporary file after use
|
166 |
os.remove(image_path)
|
167 |
|
168 |
+
return json, json_2
|
169 |
|
170 |
# Create Gradio interface
|
171 |
demo = gr.Interface(
|
172 |
fn=process_document,
|
173 |
inputs="image", # Gradio will handle the image input
|
174 |
+
outputs=["json", "json"],
|
175 |
title="PaySlip_Demo_Model",
|
176 |
examples=[["Slip_1.jpg"], ["Slip_2.jpg"]],
|
177 |
cache_examples=False
|