TokenopolyHQ
commited on
Commit
Β·
0b139fc
1
Parent(s):
e2465f1
Deploy Banking SMS JSON Parser Chatbot
Browse files- README.md +1 -1
- app.py +84 -84
- 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:
|
| 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
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# --------------------------------------------------
|
| 6 |
# 1. Model loading
|
| 7 |
# --------------------------------------------------
|
| 8 |
MODEL_ID = "rawsun00001/banking-sms-json-parser-v6-merged"
|
| 9 |
|
| 10 |
-
print("π Loading banking
|
| 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
|
| 21 |
|
| 22 |
# --------------------------------------------------
|
| 23 |
-
# 2. Core parsing
|
| 24 |
# --------------------------------------------------
|
| 25 |
def parse_banking_sms(raw_text: str) -> dict:
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
|
| 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
|
| 37 |
|
| 38 |
with torch.no_grad():
|
| 39 |
outputs = model.generate(
|
| 40 |
**inputs,
|
| 41 |
max_new_tokens=120,
|
| 42 |
-
do_sample=False,
|
| 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 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if match:
|
| 54 |
try:
|
| 55 |
-
parsed = json.loads(
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# --------------------------------------------------
|
| 73 |
-
# 3. Chatbot
|
| 74 |
# --------------------------------------------------
|
| 75 |
def chatbot_response(message, history):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 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,
|
| 111 |
return history, history
|
| 112 |
|
| 113 |
# --------------------------------------------------
|
| 114 |
# 4. Gradio interface
|
| 115 |
# --------------------------------------------------
|
| 116 |
-
with gr.Blocks(
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
"""
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
chatbot = gr.Chatbot(label="Parser Chat", height=450)
|
| 133 |
msg = gr.Textbox(
|
| 134 |
-
label="Paste SMS
|
| 135 |
-
placeholder="
|
| 136 |
-
lines=3
|
| 137 |
-
max_lines=10,
|
| 138 |
)
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 2 |
-
transformers==4.36.0
|
| 3 |
torch==2.1.0
|
| 4 |
-
gradio==
|
| 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
|