Seth0330 commited on
Commit
41b5dc5
·
verified ·
1 Parent(s): 1374602

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -39
app.py CHANGED
@@ -22,15 +22,12 @@ MODELS = {
22
  "key_env": "DEEPSEEK_API_KEY",
23
  "response_format": None,
24
  },
25
- "Llama 4 Mavericks": {
26
- "api_url": "https://openrouter.ai/api/v1/chat/completions",
27
- "model": "meta-llama/llama-4-maverick:free",
28
- "key_env": "OPENROUTER_API_KEY",
29
- "response_format": {"type": "json_object"},
30
- "extra_headers": {
31
- "HTTP-Referer": "https://huggingface.co",
32
- "X-Title": "Invoice Extractor",
33
- },
34
  },
35
  "Mistral Small": {
36
  "api_url": "https://openrouter.ai/api/v1/chat/completions",
@@ -146,24 +143,16 @@ def extract_invoice_info(model_choice, text):
146
  if not data:
147
  return None
148
 
149
- # normalize + supplier fallback
150
- if model_choice in ("Llama 4 Mavericks","Mistral Small"):
151
- hdr = data.setdefault("invoice_header", {})
152
- for k in ("invoice_number","invoice_date","po_number","invoice_value","supplier_name","customer_name"):
153
- hdr.setdefault(k, None)
154
- if not hdr.get("supplier_name"):
155
- hdr["supplier_name"] = fallback_supplier(text)
156
- items = data.setdefault("line_items", [])
157
- for itm in items:
158
- for k in ("item_number","description","quantity","unit_price","total_price"):
159
- itm.setdefault(k, None)
160
- else:
161
- for k in ("invoice_number","invoice_date","po_number","invoice_value"):
162
- data.setdefault(k, None)
163
- items = data.setdefault("line_items", [])
164
- for itm in items:
165
- for k in ("description","quantity","unit_price","total_price"):
166
- itm.setdefault(k, None)
167
 
168
  return data
169
 
@@ -190,18 +179,12 @@ with tab2:
190
  info = extract_invoice_info(mdl, txt)
191
  if info:
192
  st.success("Extraction Complete")
193
- if mdl in ("Llama 4 Mavericks","Mistral Small"):
194
- h=info["invoice_header"]
195
- c1,c2,c3 = st.columns(3)
196
- c1.metric("Invoice #", h["invoice_number"]); c1.metric("Supplier", h["supplier_name"])
197
- c2.metric("Date", h["invoice_date"]); c2.metric("Customer", h["customer_name"])
198
- c3.metric("PO #", h["po_number"]); c3.metric("Total", h["invoice_value"])
199
- st.subheader("Line Items"); st.table(info["line_items"])
200
- else:
201
- c1,c2 = st.columns(2)
202
- c1.metric("Invoice #", info["invoice_number"]); c1.metric("PO #", info["po_number"])
203
- c2.metric("Date", info["invoice_date"]); c2.metric("Value", info["invoice_value"])
204
- st.subheader("Line Items"); st.table(info["line_items"])
205
 
206
  if "last_api" in st.session_state:
207
  with st.expander("Debug"):
 
22
  "key_env": "DEEPSEEK_API_KEY",
23
  "response_format": None,
24
  },
25
+ "OpenAI GPT-4.1": {
26
+ "api_url": "https://api.openai.com/v1/chat/completions",
27
+ "model": "gpt-4-1106-preview",
28
+ "key_env": "OPENAI_API_KEY",
29
+ "response_format": None, # OpenAI doesn't require explicit json_object response_format for extraction
30
+ "extra_headers": {},
 
 
 
31
  },
32
  "Mistral Small": {
33
  "api_url": "https://openrouter.ai/api/v1/chat/completions",
 
143
  if not data:
144
  return None
145
 
146
+ # normalize fields (no special fallback needed for GPT-4.1, just like Mistral)
147
+ hdr = data.setdefault("invoice_header", {})
148
+ for k in ("invoice_number","invoice_date","po_number","invoice_value","supplier_name","customer_name"):
149
+ hdr.setdefault(k, None)
150
+ if not hdr.get("supplier_name"):
151
+ hdr["supplier_name"] = fallback_supplier(text)
152
+ items = data.setdefault("line_items", [])
153
+ for itm in items:
154
+ for k in ("item_number","description","quantity","unit_price","total_price"):
155
+ itm.setdefault(k, None)
 
 
 
 
 
 
 
 
156
 
157
  return data
158
 
 
179
  info = extract_invoice_info(mdl, txt)
180
  if info:
181
  st.success("Extraction Complete")
182
+ h=info["invoice_header"]
183
+ c1,c2,c3 = st.columns(3)
184
+ c1.metric("Invoice #", h["invoice_number"]); c1.metric("Supplier", h["supplier_name"])
185
+ c2.metric("Date", h["invoice_date"]); c2.metric("Customer", h["customer_name"])
186
+ c3.metric("PO #", h["po_number"]); c3.metric("Total", h["invoice_value"])
187
+ st.subheader("Line Items"); st.table(info["line_items"])
 
 
 
 
 
 
188
 
189
  if "last_api" in st.session_state:
190
  with st.expander("Debug"):