Seth0330 commited on
Commit
731ef3d
·
verified ·
1 Parent(s): e7adc3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -50
app.py CHANGED
@@ -85,50 +85,55 @@ def clean_json_response(text):
85
  if not text:
86
  return None
87
  orig = text
88
- # strip fences
89
  text = re.sub(r'```(?:json)?', '', text).strip()
90
- # grab braces
91
- start = text.find('{')
92
- end = text.rfind('}') + 1
93
  if start < 0 or end < 1:
94
- st.error("Couldn't locate JSON")
95
  st.code(orig)
96
  return None
97
- fragment = text[start:end]
98
- # remove stray trailing commas before } or ]
99
- fragment = re.sub(r',\s*([}\]])', r'\1', fragment)
100
  try:
101
- return json.loads(fragment)
102
  except json.JSONDecodeError as e:
103
- st.error(f"JSON parse error: {e}")
104
- st.code(fragment)
105
- return None
 
 
 
 
 
106
 
107
  def fallback_supplier(text):
108
- # first non-empty line heuristic
109
- lines = [l.strip() for l in text.splitlines() if l.strip()]
110
- return lines[0] if lines else None
 
 
111
 
112
  def get_extraction_prompt(model_choice, txt):
113
- # every prompt now demands "json" and COMPACT JSON output
114
  if model_choice.startswith("DeepSeek"):
115
  return (
116
- "Extract full invoice info below and RETURN ONLY a valid json object (compact, single line) with these fields:\n"
117
- '{"invoice_number":"string","invoice_date":"YYYY-MM-DD","po_number":"string|null",'
118
- '"invoice_value":"string with currency","line_items":[{"description":"string","quantity":"number",'
119
- '"unit_price":"string with currency","total_price":"string with currency"}]}\n'
120
- "Use null for missing fields. NO extra text.\n\n"
121
  f"Invoice Text:\n{txt}"
122
  )
123
  else:
124
  return (
125
- "You are given invoice text. Extract data and RETURN ONLY a compact json object (one line) exactly like this:\n"
126
  '{"invoice_header":{"invoice_number":"string","invoice_date":"YYYY-MM-DD",'
127
  '"po_number":"string|null","invoice_value":"string with currency",'
128
  '"supplier_name":"string|null","customer_name":"string|null"},'
129
  '"line_items":[{"item_number":"string|null","description":"string","quantity":number,'
130
  '"unit_price":"string with currency","total_price":"string with currency"}]}\n'
131
- "Use null for missing. NO explanations or extra keys.\n\n"
132
  f"Invoice Text:\n{txt}"
133
  )
134
 
@@ -141,62 +146,62 @@ def extract_invoice_info(model_choice, text):
141
  if not data:
142
  return None
143
 
144
- # normalize header + fallback supplier
145
  if model_choice in ("Llama 4 Mavericks","Mistral Small"):
146
- hdr = data.setdefault("invoice_header",{})
147
  for k in ("invoice_number","invoice_date","po_number","invoice_value","supplier_name","customer_name"):
148
- hdr.setdefault(k,None)
149
  if not hdr.get("supplier_name"):
150
  hdr["supplier_name"] = fallback_supplier(text)
151
- items = data.setdefault("line_items",[])
152
  for itm in items:
153
  for k in ("item_number","description","quantity","unit_price","total_price"):
154
- itm.setdefault(k,None)
155
  else:
156
  for k in ("invoice_number","invoice_date","po_number","invoice_value"):
157
- data.setdefault(k,None)
158
- items = data.setdefault("line_items",[])
159
  for itm in items:
160
  for k in ("description","quantity","unit_price","total_price"):
161
- itm.setdefault(k,None)
162
 
163
  return data
164
 
165
- # UI
166
  tab1, tab2 = st.tabs(["PDF Summarizer","Invoice Extractor"])
167
 
168
  with tab1:
169
- st.title("PDF → Bullet-Point Summarizer")
170
- pdf = st.file_uploader("Upload PDF",type="pdf")
171
- pct = st.slider("Summarization %",1,100,20)
172
  if st.button("Summarize") and pdf:
173
  txt = read_pdf(io.BytesIO(pdf.getvalue()))
174
  keys = extract_key_phrases(txt)
175
- scores = score_sentences(txt,keys)
176
  n = max(1, len(scores)*pct//100)
177
- st.markdown(summarize_text(scores,num_points=n))
178
 
179
  with tab2:
180
  st.title("Invoice Extractor")
181
- mdl = st.selectbox("Model",list(MODELS.keys()))
182
- inv_pdf = st.file_uploader("Invoice PDF",type="pdf")
183
  if st.button("Extract") and inv_pdf:
184
  txt = read_pdf(io.BytesIO(inv_pdf.getvalue()))
185
- info = extract_invoice_info(mdl,txt)
186
  if info:
187
- st.success("Done")
188
  if mdl in ("Llama 4 Mavericks","Mistral Small"):
189
  h=info["invoice_header"]
190
- c1,c2,c3=st.columns(3)
191
- c1.metric("Invoice #",h["invoice_number"]);c1.metric("Supplier",h["supplier_name"])
192
- c2.metric("Date",h["invoice_date"]);c2.metric("Customer",h["customer_name"])
193
- c3.metric("PO #",h["po_number"]);c3.metric("Total",h["invoice_value"])
194
- st.table(info["line_items"])
195
  else:
196
- c1,c2=st.columns(2)
197
- c1.metric("Invoice #",info["invoice_number"]);c1.metric("PO #",info["po_number"])
198
- c2.metric("Date",info["invoice_date"]);c2.metric("Value",info["invoice_value"])
199
- st.table(info["line_items"])
200
 
201
  if "last_api" in st.session_state:
202
  with st.expander("Debug"):
 
85
  if not text:
86
  return None
87
  orig = text
88
+ # strip ``` fences
89
  text = re.sub(r'```(?:json)?', '', text).strip()
90
+ # find outer braces
91
+ start, end = text.find('{'), text.rfind('}') + 1
 
92
  if start < 0 or end < 1:
93
+ st.error("Couldn't locate JSON in response.")
94
  st.code(orig)
95
  return None
96
+ frag = text[start:end]
97
+ # remove stray trailing commas
98
+ frag = re.sub(r',\s*([}\]])', r'\1', frag)
99
  try:
100
+ return json.loads(frag)
101
  except json.JSONDecodeError as e:
102
+ # attempt to insert missing commas between adjacent fields
103
+ repaired = re.sub(r'"\s*"\s*(?="[^"]+"\s*:)', '","', frag)
104
+ try:
105
+ return json.loads(repaired)
106
+ except json.JSONDecodeError:
107
+ st.error(f"JSON parse error: {e}")
108
+ st.code(frag)
109
+ return None
110
 
111
  def fallback_supplier(text):
112
+ for line in text.splitlines():
113
+ line = line.strip()
114
+ if line:
115
+ return line
116
+ return None
117
 
118
  def get_extraction_prompt(model_choice, txt):
 
119
  if model_choice.startswith("DeepSeek"):
120
  return (
121
+ "Extract full invoice info and RETURN ONLY a single-line json object with fields:\n"
122
+ '{"invoice_number":"string","invoice_date":"YYYY-MM-DD",'
123
+ '"po_number":"string|null","invoice_value":"string with currency",'
124
+ '"line_items":[{"description":"string","quantity":"number","unit_price":"string with currency","total_price":"string with currency"}]}\n'
125
+ "Use null for missing. NO extra text.\n\n"
126
  f"Invoice Text:\n{txt}"
127
  )
128
  else:
129
  return (
130
+ "Extract invoice data and RETURN ONLY a compact, one-line json object exactly:\n"
131
  '{"invoice_header":{"invoice_number":"string","invoice_date":"YYYY-MM-DD",'
132
  '"po_number":"string|null","invoice_value":"string with currency",'
133
  '"supplier_name":"string|null","customer_name":"string|null"},'
134
  '"line_items":[{"item_number":"string|null","description":"string","quantity":number,'
135
  '"unit_price":"string with currency","total_price":"string with currency"}]}\n'
136
+ "Use null for missing. NO extras.\n\n"
137
  f"Invoice Text:\n{txt}"
138
  )
139
 
 
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
 
170
+ # ---- UI ----
171
  tab1, tab2 = st.tabs(["PDF Summarizer","Invoice Extractor"])
172
 
173
  with tab1:
174
+ st.title("PDF → Bullet Points")
175
+ pdf = st.file_uploader("Upload PDF", type="pdf")
176
+ pct = st.slider("Summarization %", 1, 100, 20)
177
  if st.button("Summarize") and pdf:
178
  txt = read_pdf(io.BytesIO(pdf.getvalue()))
179
  keys = extract_key_phrases(txt)
180
+ scores = score_sentences(txt, keys)
181
  n = max(1, len(scores)*pct//100)
182
+ st.markdown(summarize_text(scores, num_points=n))
183
 
184
  with tab2:
185
  st.title("Invoice Extractor")
186
+ mdl = st.selectbox("Model", list(MODELS.keys()))
187
+ inv_pdf = st.file_uploader("Invoice PDF", type="pdf")
188
  if st.button("Extract") and inv_pdf:
189
  txt = read_pdf(io.BytesIO(inv_pdf.getvalue()))
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"):