TokenopolyHQ commited on
Commit
0b139fc
Β·
1 Parent(s): e2465f1

Deploy Banking SMS JSON Parser Chatbot

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +84 -84
  3. requirements.txt +3 -3
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏦 # Any valid emoji
4
  colorFrom: blue # Card background gradient (start)
5
  colorTo: green # Card background gradient (end)
6
  sdk: gradio # You created a Gradio Space
7
- sdk_version: 5.39.0
8
  app_file: app.py # The Python file that launches the app
9
  pinned: false # Show/hide on your profile page
10
  ---
 
4
  colorFrom: blue # Card background gradient (start)
5
  colorTo: green # Card background gradient (end)
6
  sdk: gradio # You created a Gradio Space
7
+ sdk_version: 4.44.0
8
  app_file: app.py # The Python file that launches the app
9
  pinned: false # Show/hide on your profile page
10
  ---
app.py CHANGED
@@ -1,13 +1,15 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import torch, json, re, textwrap
 
 
4
 
5
  # --------------------------------------------------
6
  # 1. Model loading
7
  # --------------------------------------------------
8
  MODEL_ID = "rawsun00001/banking-sms-json-parser-v6-merged"
9
 
10
- print("πŸ”„ Loading banking-SMS JSON parser …")
11
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
12
  model = AutoModelForCausalLM.from_pretrained(
13
  MODEL_ID,
@@ -17,42 +19,37 @@ model = AutoModelForCausalLM.from_pretrained(
17
 
18
  if tokenizer.pad_token is None:
19
  tokenizer.pad_token = tokenizer.eos_token
20
- print("βœ… Model ready!")
21
 
22
  # --------------------------------------------------
23
- # 2. Core parsing routine
24
  # --------------------------------------------------
25
  def parse_banking_sms(raw_text: str) -> dict:
26
- """
27
- Accept ANY raw SMS/email snippet and return a dict with the 6 fields:
28
- date, type, amount, category, last4, is_transaction
29
- """
30
- # Collapse internal whitespace so odd line-breaks don’t confuse the model
31
- sms = " ".join(raw_text.strip().split())
32
- prompt = f"{sms}|"
33
 
34
  inputs = tokenizer(prompt, return_tensors="pt")
35
  if torch.cuda.is_available():
36
- inputs = {k: v.cuda() for k, v in inputs.items()}
37
 
38
  with torch.no_grad():
39
  outputs = model.generate(
40
  **inputs,
41
  max_new_tokens=120,
42
- do_sample=False, # deterministic
43
  repetition_penalty=1.05,
44
  pad_token_id=tokenizer.eos_token_id,
45
  eos_token_id=tokenizer.eos_token_id,
46
  )
47
 
48
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
- json_chunk = decoded[len(prompt) :].strip()
50
 
51
- # Robust JSON extraction
52
- match = re.search(r"\{[^{}]+\}", json_chunk)
53
- if match:
54
  try:
55
- parsed = json.loads(match.group())
56
  return {
57
  "date": parsed.get("date"),
58
  "type": parsed.get("type"),
@@ -64,86 +61,89 @@ def parse_banking_sms(raw_text: str) -> dict:
64
  except json.JSONDecodeError:
65
  pass
66
 
67
- # Default non-transaction template
68
- return dict.fromkeys(
69
- ["date", "type", "amount", "category", "last4"], None
70
- ) | {"is_transaction": False}
 
 
 
 
71
 
72
  # --------------------------------------------------
73
- # 3. Chatbot wrapper
74
  # --------------------------------------------------
75
  def chatbot_response(message, history):
76
- result = parse_banking_sms(message)
77
-
78
- if result["is_transaction"]:
79
- md = f"""
80
- βœ… **Transaction detected**
81
-
82
- | Field | Value |
83
- |-------|-------|
84
- | **Date** | {result['date']} |
85
- | **Type** | {result['type'] or 'N/A'} |
86
- | **Amount** | {result['amount']} |
87
- | **Category** | {result['category']} |
88
- | **Last-4** | {result['last4']} |
89
-
90
- <details>
91
- <summary>Full JSON</summary>
92
-
93
- {textwrap.indent(json.dumps(result, indent=2), '')}
94
-
95
- text
96
- </details>
97
- """
98
- else:
99
- md = f"""
100
- ℹ️ **Non-transaction message**
101
-
102
- The text you provided looks like a promotional / informational SMS.
103
-
104
- {textwrap.indent(json.dumps(result, indent=2), '')}
105
-
106
- text
107
- """
108
 
109
  history = history or []
110
- history.append((message, md))
111
  return history, history
112
 
113
  # --------------------------------------------------
114
  # 4. Gradio interface
115
  # --------------------------------------------------
116
- with gr.Blocks(
117
- theme=gr.themes.Soft(),
118
- title="🏦 Banking SMS JSON Parser",
119
- css=".gradio-container {max-width: 800px !important; margin: auto !important;}",
120
- ) as demo:
121
-
122
- gr.Markdown(
123
- """
124
- # 🏦 Banking SMS JSON Parser Chatbot
125
-
126
- Paste **any** banking SMS or email snippet below β€” no special prompt
127
- formatting required. The model returns structured JSON and a friendly
128
- summary.
129
- """
130
- )
131
-
132
- chatbot = gr.Chatbot(label="Parser Chat", height=450)
133
  msg = gr.Textbox(
134
- label="Paste SMS / Email text here",
135
- placeholder="e.g. Your A/c XX1234 debited for 5,000 at AMAZON …",
136
- lines=3,
137
- max_lines=10,
138
  )
139
- state = gr.State([])
140
-
141
- msg.submit(chatbot_response, [msg, state], [chatbot, state])
142
- msg.submit(lambda: "", None, msg) # clear box after Enter
143
- gr.Markdown(
144
- "---\n**Model:** `rawsun00001/banking-sms-json-parser-v6-merged` β€” 100 % accuracy on test set;"
145
- " 169 MB FP16"
 
 
 
 
 
 
146
  )
147
 
 
 
 
 
 
 
 
 
148
  if __name__ == "__main__":
149
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ import json
5
+ import re
6
 
7
  # --------------------------------------------------
8
  # 1. Model loading
9
  # --------------------------------------------------
10
  MODEL_ID = "rawsun00001/banking-sms-json-parser-v6-merged"
11
 
12
+ print("πŸ”„ Loading banking SMS JSON parser model...")
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
  model = AutoModelForCausalLM.from_pretrained(
15
  MODEL_ID,
 
19
 
20
  if tokenizer.pad_token is None:
21
  tokenizer.pad_token = tokenizer.eos_token
22
+ print("βœ… Model loaded successfully!")
23
 
24
  # --------------------------------------------------
25
+ # 2. Core parsing function
26
  # --------------------------------------------------
27
  def parse_banking_sms(raw_text: str) -> dict:
28
+ """Parse any raw SMS/email text and return structured JSON"""
29
+ sms_text = " ".join(raw_text.strip().split())
30
+ prompt = f"{sms_text}|"
 
 
 
 
31
 
32
  inputs = tokenizer(prompt, return_tensors="pt")
33
  if torch.cuda.is_available():
34
+ inputs = {k: v.to("cuda") for k, v in inputs.items()}
35
 
36
  with torch.no_grad():
37
  outputs = model.generate(
38
  **inputs,
39
  max_new_tokens=120,
40
+ do_sample=False,
41
  repetition_penalty=1.05,
42
  pad_token_id=tokenizer.eos_token_id,
43
  eos_token_id=tokenizer.eos_token_id,
44
  )
45
 
46
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
+ json_part = decoded[len(prompt):].strip()
48
 
49
+ json_match = re.search(r'\{[^{}]+\}', json_part)
50
+ if json_match:
 
51
  try:
52
+ parsed = json.loads(json_match.group())
53
  return {
54
  "date": parsed.get("date"),
55
  "type": parsed.get("type"),
 
61
  except json.JSONDecodeError:
62
  pass
63
 
64
+ return {
65
+ "date": None,
66
+ "type": None,
67
+ "amount": None,
68
+ "category": None,
69
+ "last4": None,
70
+ "is_transaction": False
71
+ }
72
 
73
  # --------------------------------------------------
74
+ # 3. Chatbot response handler
75
  # --------------------------------------------------
76
  def chatbot_response(message, history):
77
+ """Handle user input and generate chatbot response"""
78
+ try:
79
+ result = parse_banking_sms(message)
80
+
81
+ if result["is_transaction"]:
82
+ response = (
83
+ f"βœ… **Transaction Detected!**\n\n"
84
+ f"πŸ“… **Date:** {result['date']}\n"
85
+ f"πŸ’³ **Type:** {result['type'].title() if result['type'] else 'N/A'}\n"
86
+ f"πŸ’° **Amount:** {result['amount']}\n"
87
+ f"πŸͺ **Category:** {result['category']}\n"
88
+ f"πŸ”’ **Last 4 Digits:** {result['last4']}\n\n"
89
+ f"**Full JSON:**\n```json\n{json.dumps(result, indent=2)}\n```"
90
+ )
91
+ else:
92
+ response = (
93
+ "ℹ️ **Non-Transaction Message**\n\n"
94
+ "This appears to be a promotional or informational message, not a banking transaction.\n\n"
95
+ f"**Classification:**\n```json\n{json.dumps(result, indent=2)}\n```"
96
+ )
97
+ except Exception as e:
98
+ response = f"❌ **Error:** Sorry, I couldn't parse that message.\n\nError: {str(e)}"
 
 
 
 
 
 
 
 
 
 
99
 
100
  history = history or []
101
+ history.append((message, response))
102
  return history, history
103
 
104
  # --------------------------------------------------
105
  # 4. Gradio interface
106
  # --------------------------------------------------
107
+ with gr.Blocks(theme=gr.themes.Soft(), title="🏦 Banking SMS JSON Parser") as demo:
108
+ gr.Markdown("""
109
+ # 🏦 Banking SMS JSON Parser Chatbot
110
+
111
+ Paste any banking SMS/email below – no special formatting needed!
112
+
113
+ **Features:**
114
+ - βœ… Detects real transactions vs promotional messages
115
+ - βœ… Extracts date, amount, merchant, category, account info
116
+ - βœ… Works with all Indian & global banking formats
117
+ """)
118
+
119
+ chatbot = gr.Chatbot(label="Banking SMS Parser", height=400)
 
 
 
 
120
  msg = gr.Textbox(
121
+ label="Paste your banking SMS/email here",
122
+ placeholder="Example: Your A/c XX1234 debited for 5000 at AMAZON...",
123
+ lines=3
 
124
  )
125
+ chat_history = gr.State([])
126
+
127
+ gr.Examples(
128
+ examples=[
129
+ ["Your A/c XX1234 debited for 5000 on 15-Jan-2024 at AMAZON"],
130
+ ["2500 credited to A/c **9876 on 20-Dec-2023 from PAYROLL"],
131
+ ["Card **4321 used for 120 at STARBUCKS on 10-Nov-2023"],
132
+ ["Transaction Alert: 45.99 debited from **2468 at NETFLIX"],
133
+ ["Your account balance is 5000. Thank you for banking with us."],
134
+ ["Congratulations! You are eligible for a personal loan up to 50000."]
135
+ ],
136
+ inputs=msg,
137
+ label="Try these example messages:"
138
  )
139
 
140
+ msg.submit(chatbot_response, [msg, chat_history], [chatbot, chat_history])
141
+ msg.submit(lambda: "", None, msg) # Clear input
142
+
143
+ gr.Markdown("---\n**Model:** `rawsun00001/banking-sms-json-parser-v6-merged`")
144
+
145
+ # --------------------------------------------------
146
+ # 5. App launcher
147
+ # --------------------------------------------------
148
  if __name__ == "__main__":
149
  demo.launch()
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
- huggingface_hub==0.25.2
2
- transformers==4.36.0
3
  torch==2.1.0
4
- gradio==5.39.0
5
  accelerate==0.24.0
 
 
1
+ transformers==4.45.0
 
2
  torch==2.1.0
3
+ gradio==4.44.0
4
  accelerate==0.24.0
5
+ huggingface_hub==0.25.2