ashvin-savani commited on
Commit
a9a2fa5
·
1 Parent(s): dec222b
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -74,10 +74,15 @@ def prepare_prompt(image_path):
74
  return messages, text
75
 
76
  @spaces.GPU
77
- def process_image(image):
78
  if image is None:
79
  return "No image provided."
80
 
 
 
 
 
 
81
  base64_str = encode_image_from_pil(image)
82
  messages = [
83
  {
@@ -89,7 +94,7 @@ def process_image(image):
89
  ]
90
  text = processor.tokenizer.apply_chat_template(
91
  messages,
92
- template=json.dumps(invoice_schema, indent=4),
93
  tokenize=False,
94
  add_generation_prompt=True
95
  )
@@ -129,10 +134,18 @@ def process_image(image):
129
  # Gradio interface
130
  iface = gr.Interface(
131
  fn=process_image,
132
- inputs=gr.Image(type="pil", label="Upload Invoice Image"),
133
- outputs=gr.Textbox(label="Extracted Invoice Data (JSON)"),
 
 
 
 
 
 
 
 
134
  title="Invoice Parser with NuExtract",
135
- description="Upload an invoice image to extract structured data using AI."
136
  )
137
 
138
  iface.launch()
 
74
  return messages, text
75
 
76
  @spaces.GPU
77
+ def process_image(image, schema_str):
78
  if image is None:
79
  return "No image provided."
80
 
81
+ try:
82
+ custom_schema = json.loads(schema_str)
83
+ except json.JSONDecodeError:
84
+ return "Invalid JSON schema provided."
85
+
86
  base64_str = encode_image_from_pil(image)
87
  messages = [
88
  {
 
94
  ]
95
  text = processor.tokenizer.apply_chat_template(
96
  messages,
97
+ template=json.dumps(custom_schema, indent=4),
98
  tokenize=False,
99
  add_generation_prompt=True
100
  )
 
134
  # Gradio interface
135
  iface = gr.Interface(
136
  fn=process_image,
137
+ inputs=[
138
+ gr.Image(type="pil", label="Upload Invoice Image"),
139
+ gr.Textbox(
140
+ label="Custom Schema (JSON)",
141
+ value=json.dumps(invoice_schema, indent=4),
142
+ lines=10,
143
+ placeholder="Enter your custom JSON schema here..."
144
+ )
145
+ ],
146
+ outputs=gr.Textbox(label="Extracted Data (JSON)"),
147
  title="Invoice Parser with NuExtract",
148
+ description="Upload an invoice image and provide a custom JSON schema to extract structured data using AI."
149
  )
150
 
151
  iface.launch()