xelpmocAI commited on
Commit
7f5acb9
·
verified ·
1 Parent(s): 880bfb1
Files changed (1) hide show
  1. app.py +23 -35
app.py CHANGED
@@ -43,18 +43,8 @@ tax_deductions = '''Extract the following information in the given format:
43
  }
44
  '''
45
 
46
-
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) # Convert NumPy array to PIL Image
53
- image.save(tmp_file.name) # Save the image to the temporary file
54
- image_path = tmp_file.name # Get the path of the saved file
55
-
56
-
57
- messages = [
58
  {
59
  "role": "user",
60
  "content": [
@@ -62,18 +52,7 @@ def process_document(image):
62
  "type": "image",
63
  "image": image_path, # Use the file path here
64
  },
65
- {"type": "text", "text": '''Extract the following information in the given format:
66
- {
67
- 'tax_deductions': {
68
- 'federal:': {
69
- 'withholding tax:': {'Amount':'', 'Year-To_Date':""},
70
- 'ee social security tax:': {'Amount':'', 'Year-To_Date':""},
71
- 'ee medicare tax:': {'Amount':'', 'Year-To_Date':""}},
72
- 'california:': {
73
- 'withholding tax:': {'Amount':'', 'Year-To_Date':""},
74
- 'ee disability tax:': {'Amount':'', 'Year-To-Date':""}}},
75
- }
76
- '''},
77
  ],
78
  }
79
  ]
@@ -106,31 +85,40 @@ def process_document(image):
106
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
107
  )
108
  print("8")
109
- print("output_text ---->>>>", output_text)
110
- print("output text type --->>>> ", type(output_text))
111
- print("First element", output_text[0])
112
 
113
  # Handle output text to convert it into JSON
114
- json = str()
115
  try:
116
- print("Trying ......")
117
  almost_json = output_text[0].split('\n')[-1].split('\n')[0]
118
  json = literal_eval(almost_json)
119
- except Exception as e:
120
- print(f"Got error {e}")
121
- # json = output_text[0] # Return raw output if JSON parsing fails
122
- json = output_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  # Optionally, you can delete the temporary file after use
125
  os.remove(image_path)
126
 
127
- return json
128
 
129
  # Create Gradio interface
130
  demo = gr.Interface(
131
  fn=process_document,
132
  inputs="image", # Gradio will handle the image input
133
- outputs="text",
134
  title="PaySlip_Demo_Model",
135
  examples=[["Slip_1.jpg"], ["Slip_2.jpg"]],
136
  cache_examples=False
 
43
  }
44
  '''
45
 
46
+ def demo(image_path, prompt):
47
+ messages = [
 
 
 
 
 
 
 
 
 
 
48
  {
49
  "role": "user",
50
  "content": [
 
52
  "type": "image",
53
  "image": image_path, # Use the file path here
54
  },
55
+ {"type": "text", "text": prompt},
 
 
 
 
 
 
 
 
 
 
 
56
  ],
57
  }
58
  ]
 
85
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
86
  )
87
  print("8")
 
 
 
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)
93
+ except:
94
+ json = output_text[0] # Return raw output if JSON parsing fails
95
+ return json
96
+
97
+ def process_document(image):
98
+ # Save the uploaded image to a temporary file
99
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
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,
109
+ "other_benifits": two
110
+ }
111
 
112
  # Optionally, you can delete the temporary file after use
113
  os.remove(image_path)
114
 
115
+ return json_op
116
 
117
  # Create Gradio interface
118
  demo = gr.Interface(
119
  fn=process_document,
120
  inputs="image", # Gradio will handle the image input
121
+ outputs="json",
122
  title="PaySlip_Demo_Model",
123
  examples=[["Slip_1.jpg"], ["Slip_2.jpg"]],
124
  cache_examples=False