-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1494 lines (1259 loc) · 70.9 KB
/
Copy pathapp.py
File metadata and controls
1494 lines (1259 loc) · 70.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import pandas as pd
import pdfplumber
import json
import re
from datetime import datetime
import ollama
import csv
import requests
import io
from PIL import Image
import numpy as np
# Try to import OCR libraries
try:
import pytesseract
OCR_AVAILABLE = True
except ImportError:
OCR_AVAILABLE = False
pytesseract = None
try:
import easyocr
EASYOCR_AVAILABLE = True
except ImportError:
EASYOCR_AVAILABLE = False
easyocr = None
# ────────────────────────────────────────────────
# CONFIGURATION
# ────────────────────────────────────────────────
MODEL_NAME = "mistral:7b" # Ollama model name (install with: ollama pull phi3)
OLLAMA_URL = "http://localhost:11434" # Default Ollama URL
N_CTX = 4096
# Much safer defaults - you can increase later after testing
MAX_NORMALIZE_ROWS = 35
MAX_CATEGORIZE_ROWS = 20 # Reduced to prevent token limit issues - auto-splits if needed
# Rough safety margins (tokens)
PROMPT_SAFETY_MARGIN = 800 # for instructions + JSON overhead + safety
MIN_OUTPUT_TOKENS = 400 # minimum we want to leave for output
# ────────────────────────────────────────────────
# OLLAMA CHECK & LOAD
# ────────────────────────────────────────────────
@st.cache_resource
def get_available_models():
"""Get list of available Ollama models"""
try:
response = requests.get(f"{OLLAMA_URL}/api/tags", timeout=5)
if response.status_code != 200:
return []
models = response.json().get("models", [])
# Return full model names (e.g., "mistral:7b", "llama3.2:8b")
return [m["name"] for m in models]
except Exception as e:
return []
@st.cache_resource
def check_ollama():
"""Check if Ollama is running"""
try:
# Test Ollama server
response = requests.get(f"{OLLAMA_URL}/api/tags", timeout=5)
if response.status_code != 200:
st.error("Ollama server not running at http://localhost:11434")
st.info("Run: `ollama serve` in terminal")
st.stop()
models = response.json().get("models", [])
if not models:
st.warning("No models found in Ollama")
st.info("Install a model: `ollama pull mistral` or `ollama pull llama3.2`")
return True
except Exception as e:
st.error(f"Ollama connection failed: {str(e)}")
st.info("1. Install Ollama: https://ollama.com")
st.info("2. Terminal: `ollama serve`")
st.info("3. Terminal: `ollama pull mistral` (or another model)")
st.stop()
@st.cache_resource
def load_ollama():
check_ollama()
# Create Ollama client with explicit host (if available)
client = None
try:
# Try to use Client class if available (newer ollama versions)
from ollama import Client
client = Client(host=OLLAMA_URL)
except (ImportError, AttributeError):
# Fallback to module-level functions (older ollama versions)
# The module-level functions use localhost:11434 by default
pass
return OLLAMA_URL, client
OLLAMA_URL, ollama_client = load_ollama()
# ────────────────────────────────────────────────
# TOKEN ESTIMATION & SAFETY
# ────────────────────────────────────────────────
def rough_token_count(text: str) -> int:
"""Very rough but fast token estimation for Phi-3 family"""
# ~3.8 chars/token average for mixed English + numbers + punctuation
return len(text) // 3 + len(text.split()) // 2 + 20 # +20 for safety
# ────────────────────────────────────────────────
# LLM NORMALIZATION (Ollama)
# ────────────────────────────────────────────────
def normalize_with_llm(df_raw: pd.DataFrame, model_name: str = None) -> pd.DataFrame:
if model_name is None:
model_name = st.session_state.get('selected_model', MODEL_NAME)
total = len(df_raw)
batch_size = MAX_NORMALIZE_ROWS
chunks = (total + batch_size - 1) // batch_size
# Quick test to verify model is responding
try:
if ollama_client:
test_response = ollama_client.chat(
model=model_name,
messages=[{"role": "user", "content": "Say 'OK'"}],
options={"num_predict": 10}
)
else:
test_response = ollama.chat(
model=model_name,
messages=[{"role": "user", "content": "Say 'OK'"}],
options={"num_predict": 10}
)
if not test_response or 'message' not in test_response or 'content' not in test_response.get('message', {}):
st.error(f"Model {model_name} is not responding correctly. Please check:")
st.info(f"1. Model is installed: `ollama pull {model_name.split(':')[0]}`")
st.info(f"2. Ollama is running: `ollama serve`")
st.info(f"3. Model name is correct: {model_name}")
return pd.DataFrame()
except Exception as e:
st.error(f"Cannot connect to Ollama model {model_name}: {str(e)}")
st.info(f"Troubleshooting:")
st.info(f"1. Ensure Ollama is running: `ollama serve`")
st.info(f"2. Check model is installed: `ollama list`")
st.info(f"3. Install model if needed: `ollama pull {model_name.split(':')[0]}`")
return pd.DataFrame()
all_results = []
progress = st.progress(0)
for i in range(chunks):
start = i * batch_size
end = min(start + batch_size, total)
chunk = df_raw.iloc[start:end].copy()
csv_text = chunk.to_csv(index=False, lineterminator='\n')
prompt = f"""You are a precise bank transaction JSON converter.
Output ONLY valid JSON array of objects. Nothing else. No explanations.
Each object MUST contain exactly these 3 fields: "date", "description", "amount"
STRICT RULES:
1. "date": Use "Booking Date" (keep YYYY-MM-DD format)
2. "amount": Use "Amount (EUR)" (keep negative for expenses)
3. "description":
- Prefer "Partner Name" → clean lightly (remove only obvious trailing codes like **5400*)
- Keep full merchant names (e.g. "LIDL BAILEN BARCELONA")
- If Partner Name empty → use "Type" or "Unknown Transfer"
4. INCLUDE ALL ROWS with date + amount. Do NOT skip:
- Transfers, savings, payments without partner
- Large/round amounts, anything that looks real
5. Never output text/markdown outside the JSON array
CSV data:
{csv_text}
Example output (exactly like this):
[{{"date":"2025-12-01","description":"LIDL BAILEN BARCELONA","amount":-16.19}},{{"date":"2025-12-01","description":"Revolut payment","amount":-13.00}}]
Respond with ONLY the JSON array:"""
prompt_tokens_est = rough_token_count(prompt)
# Safety check
remaining_context = N_CTX - prompt_tokens_est - PROMPT_SAFETY_MARGIN
if remaining_context < MIN_OUTPUT_TOKENS:
st.warning(f"Batch {i+1} too large (~{prompt_tokens_est} est. tokens). Skipping.")
continue
max_tokens = max(MIN_OUTPUT_TOKENS, min(remaining_context, 1400))
try:
with st.spinner(f"Normalizing batch {i+1}/{chunks}..."):
try:
if ollama_client:
response = ollama_client.chat(
model=model_name,
messages=[{"role": "user", "content": prompt}],
options={
"temperature": 0.05,
"top_p": 0.9,
"top_k": 40,
"num_predict": max_tokens,
"repeat_penalty": 1.12,
"stop": ["```", "\n\n\n", "Note:", "Explanation:", "```json"]
}
)
else:
response = ollama.chat(
model=model_name,
messages=[{"role": "user", "content": prompt}],
options={
"temperature": 0.05,
"top_p": 0.9,
"top_k": 40,
"num_predict": max_tokens,
"repeat_penalty": 1.12,
"stop": ["```", "\n\n\n", "Note:", "Explanation:", "```json"]
}
)
except Exception as ollama_error:
st.error(f"Ollama API call failed: {str(ollama_error)}")
st.error(f"Error type: {type(ollama_error).__name__}")
with st.expander("Debug: Ollama Error Details", expanded=False):
st.code(str(ollama_error), language="text")
st.info(f"Model: {model_name}")
st.info(f"Ollama URL: {OLLAMA_URL}")
raise ValueError(f"Ollama API error: {str(ollama_error)}")
# Debug: Check response structure
if not response:
st.error("Ollama returned None/empty response")
with st.expander("Debug: Response Details", expanded=False):
st.json({"response": None, "type": type(response).__name__})
raise ValueError("Ollama returned empty response")
# Check response structure
if 'message' not in response:
st.error("Ollama response missing 'message' key")
with st.expander("Debug: Response Structure", expanded=False):
st.json(response)
raise ValueError("Invalid Ollama response structure: missing 'message'")
if 'content' not in response['message']:
st.error("Ollama response missing 'content' key")
with st.expander("Debug: Response Structure", expanded=False):
st.json(response)
raise ValueError("Invalid Ollama response structure: missing 'content'")
text = response['message']['content']
# Check if text is None or empty
if text is None:
st.error("LLM returned None content")
with st.expander("Debug: Response Details", expanded=False):
st.json(response)
raise ValueError("LLM response content is None")
text = text.strip()
# Check if response is empty or too short
if not text or len(text) < 10:
st.error(f"LLM returned empty or very short response: {repr(text)}")
with st.expander("Debug: Full Response", expanded=False):
st.json(response)
st.code(f"Content length: {len(text) if text else 0}\nContent: {repr(text)}", language="text")
raise ValueError("LLM response is empty or too short")
# Show raw output for debugging
st.caption(f"Raw output preview: {repr(text[:200])}...")
# Try multiple extraction strategies
json_str = None
# Strategy 1: Look for JSON wrapped in markdown code blocks
json_match = re.search(r'```(?:json)?\s*(\[.*?\])\s*```', text, re.DOTALL)
if json_match:
json_str = json_match.group(1)
# Strategy 2: Find JSON array boundaries
if not json_str:
start = text.find('[')
end = text.rfind(']') + 1
if start >= 0 and end > start:
json_str = text[start:end]
# Strategy 3: Try to salvage partial JSON
if not json_str:
# Look for any JSON-like structure
json_match = re.search(r'\[.*?\]', text, re.DOTALL)
if json_match:
json_str = json_match.group(0)
else:
# Try to find last complete object
last_complete = text.rfind('},')
if last_complete > 0:
json_str = text[:last_complete + 1] + ']'
if not json_str:
st.error(f"Could not extract JSON from response.")
with st.expander("Debug: Full LLM Response", expanded=False):
st.code(text, language="text")
raise ValueError("No valid JSON array found in LLM response")
# Clean up the JSON string
json_str = json_str.strip()
# Try to fix common JSON issues (trailing commas, etc.)
# Remove trailing commas before closing brackets/braces
json_str = re.sub(r',(\s*[}\]])', r'\1', json_str)
# Try to parse JSON
try:
data = json.loads(json_str)
except json.JSONDecodeError as e:
st.error(f"JSON parse error: {str(e)}")
with st.expander("Debug: JSON Parse Error Details", expanded=False):
st.code(f"Error: {str(e)}\n\nAttempted JSON:\n{json_str}", language="text")
raise ValueError(f"Invalid JSON format: {str(e)}")
st.write(f"Batch {i+1}: LLM kept {len(data)}/{len(chunk)} rows")
if data:
st.json(data[:3]) # Show sample
df_chunk = pd.DataFrame(data)
all_results.append(df_chunk)
except Exception as e:
st.error(f"Batch {i+1}/{chunks} failed: {str(e)[:100]}")
continue
progress.progress((i + 1) / chunks)
progress.empty()
if not all_results:
st.error("All normalization batches failed.")
return pd.DataFrame()
df = pd.concat(all_results, ignore_index=True)
df["date"] = pd.to_datetime(df["date"], errors='coerce')
df["amount"] = pd.to_numeric(df["amount"], errors='coerce')
df = df.dropna(subset=["date", "amount"]).sort_values("date").reset_index(drop=True)
st.success(f"Normalized {len(df):,} transactions (dropped {total-len(df)})")
return df
# ────────────────────────────────────────────────
# CATEGORIZATION (Ollama)
# ────────────────────────────────────────────────
def categorize_batch(chunk_df: pd.DataFrame, categories: list[str], categories_str: str,
batch_num: int, total_batches: int, model_name: str) -> tuple[pd.DataFrame, Exception, str]:
"""Categorize a single batch. Returns (categorized_df, error, response_text)."""
csv_text = chunk_df[["orig_idx", "date", "description", "amount"]].to_csv(
index=False, quoting=csv.QUOTE_NONNUMERIC, lineterminator='\n')
# Convert date to string if needed
if pd.api.types.is_datetime64_any_dtype(chunk_df["date"]):
csv_text = chunk_df[["orig_idx", "date", "description", "amount"]].copy()
csv_text["date"] = chunk_df["date"].dt.strftime("%Y-%m-%d")
csv_text = csv_text.to_csv(index=False, quoting=csv.QUOTE_NONNUMERIC, lineterminator='\n')
prompt = f"""Output ONLY a JSON array with objects containing "orig_idx" and "category".
No other text, explanations, or markdown.
Available categories: {categories_str}
RULES:
- Assign EXACTLY ONE category per transaction
- You MUST return a category for EVERY orig_idx in the input data
- Match each transaction to the MOST APPROPRIATE category from the list above
- Use "Uncategorized" ONLY if the transaction truly doesn't fit any category (rare cases)
- Look at description and amount to determine category - be creative and match closely
- orig_idx must match input exactly (0, 1, 2, etc.)
Input data:
{csv_text}
Example output:
[{{"orig_idx":0,"category":"Groceries & Supermarkets"}},{{"orig_idx":1,"category":"Transport & Fuel"}}]
IMPORTANT: Return exactly {len(chunk_df)} objects, one for each row in the input. ONLY the JSON array:"""
prompt_tokens_est = rough_token_count(prompt)
remaining = N_CTX - prompt_tokens_est - PROMPT_SAFETY_MARGIN
if remaining < MIN_OUTPUT_TOKENS:
chunk_df["category"] = "Uncategorized"
return chunk_df.drop(columns="orig_idx"), None, ""
# Calculate max_tokens based on batch size - need more tokens for larger batches
# Rough estimate: ~80 tokens per transaction in JSON format for output
# Plus we need tokens for the prompt itself
estimated_output_tokens = len(chunk_df) * 80
safe_available = remaining * 0.7 # Use 70% to be safe
# If batch is too large, we'll need to split it
if estimated_output_tokens > safe_available:
# Calculate how many rows we can safely process
max_safe_rows = int(safe_available / 80)
if max_safe_rows < 1:
max_safe_rows = 1
st.warning(f"Batch {batch_num} too large ({len(chunk_df)} rows, ~{estimated_output_tokens} tokens needed, {remaining} available)")
st.info(f"Splitting into smaller chunks. Processing {max_safe_rows} rows at a time...")
# This batch will be processed in chunks - return a special indicator
# The caller should handle splitting
raise ValueError(f"BATCH_TOO_LARGE:{max_safe_rows}")
max_tokens = min(remaining, max(estimated_output_tokens, 1000))
try:
if ollama_client:
response = ollama_client.chat(
model=model_name,
messages=[{"role": "user", "content": prompt}],
options={
"temperature": 0.05,
"top_p": 0.9,
"repeat_penalty": 1.12,
"num_predict": max_tokens,
"stop": ["```", "\n\n", "Note:", "Explanation:"]
}
)
else:
response = ollama.chat(
model=model_name,
messages=[{"role": "user", "content": prompt}],
options={
"temperature": 0.05,
"top_p": 0.9,
"repeat_penalty": 1.12,
"num_predict": max_tokens,
"stop": ["```", "\n\n", "Note:", "Explanation:"]
}
)
text = response['message']['content'].strip()
# Check if response is empty or too short
if not text or len(text) < 10:
raise ValueError("LLM response is empty or too short")
# Try multiple extraction strategies
json_str = None
# Strategy 1: Look for JSON wrapped in markdown code blocks (non-greedy)
json_match = re.search(r'```(?:json)?\s*(\[.*?\])\s*```', text, re.DOTALL)
if json_match:
json_str = json_match.group(1)
# Strategy 2: Look for JSON wrapped in markdown code blocks (greedy - in case non-greedy fails)
if not json_str:
json_match = re.search(r'```(?:json)?\s*(\[.*\])\s*```', text, re.DOTALL)
if json_match:
json_str = json_match.group(1)
# Strategy 3: Try to find JSON array with balanced brackets (more robust - try this first)
if not json_str:
# Find first '[' and then find matching ']' by counting brackets
start = text.find('[')
if start >= 0:
bracket_count = 0
for i in range(start, len(text)):
if text[i] == '[':
bracket_count += 1
elif text[i] == ']':
bracket_count -= 1
if bracket_count == 0:
json_str = text[start:i+1]
break
# Strategy 4: Find JSON array boundaries (simple approach - fallback)
if not json_str:
start = text.find('[')
end = text.rfind(']') + 1
if start >= 0 and end > start:
json_str = text[start:end]
# Strategy 3.5: If we found opening bracket but no closing, try to complete it
if not json_str:
start = text.find('[')
if start >= 0:
# Check if there are any complete objects before the end
# Look for patterns like }, or }]
last_complete = max(text.rfind('},'), text.rfind('}]'))
if last_complete > start:
# We have a start and at least one complete object
# Try to salvage by closing the array
# Find the last complete object's closing brace
if text.rfind('},') > text.rfind('}]'):
# Last thing is }, so close with ]
potential_json = text[start:text.rfind('},') + 1] + ']'
else:
# Already has }] somewhere
potential_json = text[start:text.rfind('}]') + 2]
# Validate it looks like JSON before accepting
if potential_json.count('{') == potential_json.count('}') and 'orig_idx' in potential_json:
json_str = potential_json
# Strategy 5: Try regex with greedy matching (in case array spans multiple lines)
if not json_str:
json_match = re.search(r'\[.*\]', text, re.DOTALL)
if json_match:
json_str = json_match.group(0)
# Strategy 6: Look for JSON-like structure even if malformed
if not json_str:
# Try to find something that looks like JSON array
json_match = re.search(r'\[[^\]]*\{[^}]*"[^"]*"[^}]*\}[^\]]*\]', text, re.DOTALL)
if json_match:
json_str = json_match.group(0)
if not json_str:
# Last resort: If we see a '[' but extraction failed, try to salvage
start = text.find('[')
if start >= 0:
# Check if we can see JSON-like structure starting
if '"orig_idx"' in text or '"category"' in text:
# Looks like JSON but extraction failed - maybe response was truncated
# Try to get as much as possible and add closing bracket
# Find last complete item pattern
potential_end = max(
text.rfind('},'),
text.rfind('}'),
text.rfind('"category"') + 50 # rough estimate after category field
)
if potential_end > start:
# Try to create valid JSON by closing properly
salvage_json = text[start:potential_end].rstrip()
# Remove trailing comma if present
salvage_json = salvage_json.rstrip(',')
# Ensure it ends with }
if not salvage_json.endswith('}'):
# Find last }
last_brace = salvage_json.rfind('}')
if last_brace > 0:
salvage_json = salvage_json[:last_brace + 1]
# Add closing bracket
salvage_json += ']'
# Quick sanity check
if salvage_json.count('{') <= salvage_json.count('}') and '{' in salvage_json:
json_str = salvage_json
# If still no JSON found, raise error with helpful message
if not json_str:
# Store error details for debugging
has_opening = '[' in text
has_closing = ']' in text
preview = repr(text[:500]) if len(text) > 500 else repr(text)
error_msg = f"No valid JSON array found in LLM response. Has '[': {has_opening}, Has ']': {has_closing}. Response preview: {preview}"
raise ValueError(error_msg)
# Clean up JSON (remove trailing commas, fix common issues)
json_str = json_str.strip()
json_str = re.sub(r',(\s*[}\]])', r'\1', json_str)
# Try to parse JSON, with error handling for common issues
try:
data = json.loads(json_str)
except json.JSONDecodeError as json_err:
# If error mentions unterminated string, try to fix it
if "Unterminated string" in str(json_err) or "Expecting" in str(json_err):
# Try to find last complete object (ending with '},')
# rfind('},') returns the start position of '},' which is the position of '}'
last_complete_obj = json_str.rfind('},')
if last_complete_obj > 0:
# Include the '}' at position last_complete_obj, add ']' to close array
# This removes any incomplete last object
fixed_json = json_str[:last_complete_obj + 1] + ']'
try:
data = json.loads(fixed_json)
except json.JSONDecodeError as e2:
# If that still fails, try to find the last properly closed quote before the error position
# The error message usually contains the character position
error_pos_match = re.search(r'char (\d+)', str(json_err))
if error_pos_match:
error_pos = int(error_pos_match.group(1))
# Try truncating just before the error position
fixed_json = json_str[:error_pos].rstrip().rstrip(',') + ']'
try:
data = json.loads(fixed_json)
except json.JSONDecodeError:
raise ValueError(f"JSON parse error: {str(json_err)}. Could not auto-fix.")
else:
raise ValueError(f"JSON parse error: {str(json_err)}. Could not auto-fix.")
else:
raise ValueError(f"JSON parse error: {str(json_err)}")
else:
raise ValueError(f"JSON parse error: {str(json_err)}")
cat_map = {item["orig_idx"]: item["category"] for item in data}
# Validate all orig_idx values are covered
expected_indices = set(chunk_df["orig_idx"].tolist())
received_indices = set(cat_map.keys())
missing_indices = expected_indices - received_indices
if missing_indices:
# Warn about missing mappings - likely due to truncation
st.warning(f"LLM did not return categories for {len(missing_indices)} row(s) (orig_idx: {sorted(missing_indices)})")
# Try to infer if response was truncated
is_truncated = False
if text and not text.rstrip().endswith(']'):
is_truncated = True
st.warning(f"Response appears truncated - doesn't end with ']'. Last 50 chars: {repr(text[-50:])}")
# Try to categorize missing rows with a smaller batch
if len(missing_indices) < len(chunk_df) and len(missing_indices) <= 10:
st.info(f"Attempting to categorize {len(missing_indices)} missing row(s) separately...")
try:
# Get the missing rows (keep orig_idx for mapping)
missing_rows = chunk_df[chunk_df["orig_idx"].isin(missing_indices)].copy()
# Retry with just these rows (smaller batch should work)
retry_result, retry_error, _ = categorize_batch(
missing_rows, categories, categories_str,
batch_num, 1, model_name
)
if not retry_error:
# The retry_result has orig_idx removed, so we match by position
# missing_rows and retry_result are in the same order
for i, orig_idx_val in enumerate(missing_rows["orig_idx"].values):
if i < len(retry_result):
cat_map[orig_idx_val] = retry_result.iloc[i]["category"]
st.success(f"Successfully categorized orig_idx {orig_idx_val}")
# Re-map with updated cat_map
chunk_df["category"] = chunk_df["orig_idx"].map(cat_map)
except Exception as retry_e:
st.warning(f"Retry for missing rows failed: {str(retry_e)[:100]}")
if missing_indices:
st.info(f"{len(missing_indices)} row(s) still missing. Response length: {len(text)} chars. Consider reducing batch size from {MAX_CATEGORIZE_ROWS}.")
# Check for duplicate orig_idx in LLM response
if len(data) != len(cat_map):
st.warning(f"LLM returned duplicate orig_idx values. Some categories may be overwritten.")
# Map categories, but track which ones are missing
chunk_df["category"] = chunk_df["orig_idx"].map(cat_map)
# Only fill missing with "Uncategorized" if LLM didn't provide them
# This preserves the distinction between "LLM said Uncategorized" vs "LLM didn't provide category"
uncategorized_count = (chunk_df["category"] == "Uncategorized").sum() if "Uncategorized" in cat_map.values() else 0
missing_count = chunk_df["category"].isna().sum()
if missing_count > 0:
st.warning(f"{missing_count} row(s) missing from LLM response - assigning 'Uncategorized'")
chunk_df["category"] = chunk_df["category"].fillna("Uncategorized")
elif uncategorized_count > 0:
st.info(f"LLM assigned 'Uncategorized' to {uncategorized_count} row(s) - check if this is correct")
return chunk_df.drop(columns="orig_idx"), None, text
except Exception as e:
chunk_df["category"] = "Uncategorized"
# Return error with response text for debugging
response_text = text if 'text' in locals() else ""
return chunk_df.drop(columns="orig_idx"), e, response_text
def categorize_with_llm(df: pd.DataFrame, categories: list[str], model_name: str = None,
track_failures: bool = True) -> tuple[pd.DataFrame, dict]:
"""Categorize transactions. Returns (categorized_df, failed_batches_dict)."""
if model_name is None:
model_name = st.session_state.get('selected_model', MODEL_NAME)
if df.empty:
return df, {}
categories_str = " • ".join(categories)
batch_size = MAX_CATEGORIZE_ROWS
total = len(df)
chunks = (total + batch_size - 1) // batch_size
progress = st.progress(0)
status = st.empty()
categorized_chunks = []
failed_batches = {} # {batch_num: {"chunk_df": ..., "error": ...}}
for i in range(chunks):
start = i * batch_size
end = min(start + batch_size, total)
chunk_df = df.iloc[start:end].copy().reset_index(names="orig_idx")
status.text(f"Categorizing batch {i+1}/{chunks}...")
categorized_chunk, error, response_text = categorize_batch(chunk_df, categories, categories_str, i+1, chunks, model_name)
if error:
# Check if error is due to batch being too large
if isinstance(error, ValueError) and str(error).startswith("BATCH_TOO_LARGE:"):
# Extract the recommended batch size
try:
recommended_size = int(str(error).split(":")[1])
st.info(f"📦 Batch {i+1} too large, splitting into smaller chunks of {recommended_size} rows...")
# Split the chunk into smaller sub-chunks
sub_chunk_size = recommended_size
sub_chunks = (len(chunk_df) + sub_chunk_size - 1) // sub_chunk_size
sub_categorized_chunks = []
for sub_i in range(sub_chunks):
sub_start = sub_i * sub_chunk_size
sub_end = min(sub_start + sub_chunk_size, len(chunk_df))
sub_chunk_df = chunk_df.iloc[sub_start:sub_end].copy()
# Preserve original orig_idx - don't reset it, just ensure it's there
if "orig_idx" not in sub_chunk_df.columns:
sub_chunk_df = sub_chunk_df.reset_index(names="orig_idx")
status.text(f"Categorizing batch {i+1}/{chunks}, sub-chunk {sub_i+1}/{sub_chunks}...")
sub_result, sub_error, sub_response = categorize_batch(
sub_chunk_df, categories, categories_str,
f"{i+1}.{sub_i+1}", sub_chunks, model_name
)
if sub_error:
st.warning(f"Sub-chunk {sub_i+1} of batch {i+1} failed: {str(sub_error)[:100]}")
if track_failures:
failed_batches[f"{i+1}.{sub_i+1}"] = {
"chunk_df": sub_chunk_df,
"error": str(sub_error),
"categories": categories,
"categories_str": categories_str,
"model_name": model_name,
"response_text": sub_response
}
else:
st.success(f"Sub-chunk {sub_i+1}/{sub_chunks} of batch {i+1} categorized")
sub_categorized_chunks.append(sub_result)
# Combine sub-chunks
if sub_categorized_chunks:
categorized_chunk = pd.concat(sub_categorized_chunks, ignore_index=True)
st.success(f"Batch {i+1}/{chunks} completed ({sub_chunks} sub-chunks)")
else:
# All sub-chunks failed
categorized_chunk = chunk_df.copy()
categorized_chunk["category"] = "Uncategorized"
categorized_chunk = categorized_chunk.drop(columns="orig_idx")
except (ValueError, IndexError):
# Couldn't parse the error, treat as regular error
st.warning(f"Batch {i+1} failed → 'Uncategorized': {str(error)[:100]}")
if track_failures:
failed_batches[i+1] = {
"chunk_df": chunk_df,
"error": str(error),
"categories": categories,
"categories_str": categories_str,
"model_name": model_name,
"response_text": response_text
}
else:
# Regular error
st.warning(f"⚠️ Batch {i+1} failed → 'Uncategorized': {str(error)[:100]}")
if track_failures:
failed_batches[i+1] = {
"chunk_df": chunk_df,
"error": str(error),
"categories": categories,
"categories_str": categories_str,
"model_name": model_name,
"response_text": response_text
}
else:
st.success(f"Batch {i+1}/{chunks} categorized successfully")
categorized_chunks.append(categorized_chunk)
progress.progress((i + 1) / chunks)
progress.empty()
status.empty()
result_df = pd.concat(categorized_chunks, ignore_index=True)
return result_df, failed_batches
# ────────────────────────────────────────────────
# RECEIPT EXTRACTION (Ollama)
# ────────────────────────────────────────────────
def validate_image_quality(image_bytes: bytes, file_name: str) -> tuple[bool, str]:
"""
Validate image quality for OCR processing.
Returns:
(is_valid, error_message)
is_valid: True if image quality is acceptable
error_message: Error message if quality is poor, empty string if valid
"""
try:
# Open image to check dimensions
image = Image.open(io.BytesIO(image_bytes))
width, height = image.size
# Check minimum resolution
MIN_WIDTH = 300
MIN_HEIGHT = 300
MIN_TOTAL_PIXELS = 100000 # ~316x316 pixels
total_pixels = width * height
errors = []
# Check width
if width < MIN_WIDTH:
errors.append(f"width too small ({width}px, minimum {MIN_WIDTH}px)")
# Check height
if height < MIN_HEIGHT:
errors.append(f"height too small ({height}px, minimum {MIN_HEIGHT}px)")
# Check total pixels (catches very small images)
if total_pixels < MIN_TOTAL_PIXELS:
errors.append(f"image too small ({total_pixels:,} pixels, minimum {MIN_TOTAL_PIXELS:,})")
# Check file size (very small files might be low quality)
file_size_kb = len(image_bytes) / 1024
if file_size_kb < 10: # Less than 10KB is suspicious
errors.append(f"file size too small ({file_size_kb:.1f}KB, may indicate low quality)")
# Check aspect ratio (extremely wide or tall images might be problematic)
aspect_ratio = width / height if height > 0 else 0
if aspect_ratio > 10 or aspect_ratio < 0.1:
errors.append(f"extreme aspect ratio ({aspect_ratio:.2f}, may affect OCR accuracy)")
if errors:
error_msg = f"**{file_name}** has poor quality: {', '.join(errors)}"
error_msg += f"\n\n**Current dimensions:** {width}×{height} pixels ({total_pixels:,} total)"
error_msg += f"\n**File size:** {file_size_kb:.1f} KB"
error_msg += "\n\n**Recommendations:**"
error_msg += f"\n- Minimum resolution: {MIN_WIDTH}×{MIN_HEIGHT} pixels"
error_msg += "\n- Use a well-lit, in-focus photo"
error_msg += "\n- Ensure text is clearly visible and not blurry"
error_msg += "\n- Try taking the photo again with better lighting"
return False, error_msg
return True, ""
except Exception as e:
return False, f"Could not validate image {file_name}: {str(e)}"
def extract_text_from_image(image_bytes: bytes, file_name: str) -> str:
"""
Extract text from an image using OCR.
Tries pytesseract first, then easyocr as fallback.
"""
try:
# Open image from bytes
image = Image.open(io.BytesIO(image_bytes))
# Try pytesseract first (faster, more common)
if OCR_AVAILABLE:
try:
# Convert to RGB if necessary (pytesseract needs RGB)
if image.mode != 'RGB':
image = image.convert('RGB')
# Extract text using pytesseract
text = pytesseract.image_to_string(image, lang='eng')
if text and text.strip():
return text.strip()
except Exception as e:
st.warning(f"pytesseract failed for {file_name}: {str(e)[:100]}")
# Fallback to easyocr if available
if EASYOCR_AVAILABLE:
try:
# Initialize EasyOCR reader (only once, cache it)
if 'easyocr_reader' not in st.session_state:
st.session_state.easyocr_reader = easyocr.Reader(['en'], gpu=False)
reader = st.session_state.easyocr_reader
# Convert PIL image to numpy array for easyocr
img_array = np.array(image)
# Extract text
results = reader.readtext(img_array)
text = '\n'.join([result[1] for result in results])
if text and text.strip():
return text.strip()
except Exception as e:
st.warning(f"easyocr failed for {file_name}: {str(e)[:100]}")
return None
except Exception as e:
st.warning(f"Could not process image {file_name}: {str(e)[:100]}")
return None
def extract_receipt_with_ollama(files, model_name: str = None) -> list[dict]:
"""
Extract receipt data from uploaded files using Ollama.
Args:
files: List of uploaded file objects (from st.file_uploader with accept_multiple_files=True)
model_name: Ollama model name to use
Returns:
List of dictionaries, one per receipt, with keys:
date, merchant, total_amount, currency, vat_amount, suggested_category, confidence
"""
if model_name is None:
model_name = st.session_state.get('selected_model', MODEL_NAME)
if not files:
return []
# Normalize to list if single file
if not isinstance(files, list):
files = [files]
all_results = []
batch_size = 3 # Process max 3-4 files at a time
# Process files in batches
for batch_start in range(0, len(files), batch_size):
batch_end = min(batch_start + batch_size, len(files))
batch_files = files[batch_start:batch_end]
batch_texts = []
batch_file_names = []
# Extract text from each file in batch
for file in batch_files:
file_name = file.name
file_ext = file_name.lower().split('.')[-1] if '.' in file_name else ''
text_content = None
if file_ext == 'pdf':
# Extract text from PDF using pdfplumber
try:
# Read file content once
file_bytes = file.read()
# Reset file pointer for potential reuse
if hasattr(file, 'seek'):
file.seek(0)
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
text_parts = []
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
text_parts.append(page_text)
text_content = "\n\n".join(text_parts)
except Exception as e:
st.warning(f"Could not extract text from PDF {file_name}: {str(e)}")
text_content = None
elif file_ext in ['jpg', 'jpeg', 'png']:
# Validate image quality first
try:
file_bytes = file.read()
if hasattr(file, 'seek'):
file.seek(0)
# Check image quality before processing
is_valid, error_message = validate_image_quality(file_bytes, file_name)
if not is_valid:
st.error(error_message)
# Still try to process, but warn the user
st.warning("Processing anyway, but results may be poor. Consider using a higher quality image.")
# Option to skip this file
if st.checkbox(f"Skip {file_name} (low quality)", key=f"skip_{file_name}"):
continue
# Extract text from image using OCR
with st.spinner(f"Extracting text from {file_name} using OCR..."):
text_content = extract_text_from_image(file_bytes, file_name)
if text_content:
st.success(f"Extracted {len(text_content)} characters from {file_name}")
else:
if not OCR_AVAILABLE and not EASYOCR_AVAILABLE:
st.error(f"No OCR library available. Install pytesseract: `pip install pytesseract` (requires Tesseract OCR)")
st.info("Alternatively: `pip install easyocr`")
else:
st.warning(f"Could not extract text from {file_name}. Image may be unclear or contain no text.")
text_content = None
except Exception as e:
st.warning(f"Error processing image {file_name}: {str(e)[:100]}")
text_content = None
if text_content:
batch_texts.append(text_content)
batch_file_names.append(file_name)
else:
st.warning(f"Skipping {file_name}: Could not extract text content")
if not batch_texts: