Spaces:
Build error
Build error
rawsun007 commited on
Commit Β·
24f83a8
1
Parent(s): 6ca6cb6
code update in app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,9 @@ import re
|
|
| 6 |
from fastapi import FastAPI
|
| 7 |
import uvicorn
|
| 8 |
|
| 9 |
-
# --------------------------------------------------
|
| 10 |
-
# 1. Model loading
|
| 11 |
-
# --------------------------------------------------
|
| 12 |
MODEL_ID = "rawsun00001/banking-sms-json-parser-v6-merged"
|
| 13 |
|
| 14 |
-
print("π Loading banking
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
MODEL_ID,
|
|
@@ -20,19 +17,14 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 20 |
)
|
| 21 |
if tokenizer.pad_token is None:
|
| 22 |
tokenizer.pad_token = tokenizer.eos_token
|
| 23 |
-
print("β
Model loaded
|
| 24 |
|
| 25 |
-
# --------------------------------------------------
|
| 26 |
-
# 2. Core parsing function
|
| 27 |
-
# --------------------------------------------------
|
| 28 |
def parse_banking_sms(raw_text: str) -> dict:
|
| 29 |
-
|
| 30 |
-
prompt =
|
| 31 |
-
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 33 |
if torch.cuda.is_available():
|
| 34 |
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 35 |
-
|
| 36 |
with torch.no_grad():
|
| 37 |
outputs = model.generate(
|
| 38 |
**inputs,
|
|
@@ -42,14 +34,12 @@ def parse_banking_sms(raw_text: str) -> dict:
|
|
| 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 |
-
|
| 50 |
-
if json_match:
|
| 51 |
try:
|
| 52 |
-
parsed = json.loads(
|
| 53 |
return {
|
| 54 |
"date": parsed.get("date"),
|
| 55 |
"type": parsed.get("type"),
|
|
@@ -60,72 +50,49 @@ def parse_banking_sms(raw_text: str) -> dict:
|
|
| 60 |
}
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
def chatbot_response(raw_text, chat_history):
|
| 77 |
-
result = parse_banking_sms(raw_text)
|
| 78 |
if result["is_transaction"]:
|
| 79 |
-
response =
|
| 80 |
-
|
| 81 |
-
π
Date: {result['date']}
|
| 82 |
-
π³ Type: {result['type'].title() if result['type'] else 'N/A'}
|
| 83 |
-
π° Amount: {result['amount']}
|
| 84 |
-
πͺ Category: {result['category']}
|
| 85 |
-
π’ Last 4 Digits: {result['last4']}"
|
|
|
|
| 86 |
else:
|
| 87 |
-
response = "βΉοΈ Non-Transaction Message\n\
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
return chat_history, ""
|
| 93 |
|
| 94 |
-
# --------------------------------------------------
|
| 95 |
-
# 4. Gradio Blocks UI (Spaces-compatible)
|
| 96 |
-
# --------------------------------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown(
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
""
|
| 108 |
-
|
| 109 |
-
chatbot = gr.Chatbot(label="Banking SMS Parser", height=400, type="messages")
|
| 110 |
-
msg = gr.Textbox(lines=3, placeholder="Paste your banking SMS/email hereβ¦", label="Input Message")
|
| 111 |
-
|
| 112 |
-
gr.Examples(
|
| 113 |
-
examples=[
|
| 114 |
-
"Your A/c XX1234 debited for 5000 on 15-Jan-2024 at AMAZON",
|
| 115 |
-
"2500 credited to A/c 9876 on 20-Dec-2023 from PAYROLL",
|
| 116 |
-
"Card 4321 used for 120 at STARBUCKS on 10-Nov-2023",
|
| 117 |
-
],
|
| 118 |
-
inputs=msg
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
msg.submit(chatbot_response, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 122 |
msg.submit(lambda: "", None, msg)
|
|
|
|
| 123 |
|
| 124 |
-
gr.Markdown("---\n**Model:** `rawsun00001/banking-sms-json-parser-v6-merged`")
|
| 125 |
-
|
| 126 |
-
# --------------------------------------------------
|
| 127 |
-
# 5. App launcher
|
| 128 |
-
# --------------------------------------------------
|
| 129 |
app = FastAPI()
|
| 130 |
app.mount("/", demo)
|
| 131 |
|
|
|
|
| 6 |
from fastapi import FastAPI
|
| 7 |
import uvicorn
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
MODEL_ID = "rawsun00001/banking-sms-json-parser-v6-merged"
|
| 10 |
|
| 11 |
+
print("π Loading bankingβSMS JSON parser model...")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
MODEL_ID,
|
|
|
|
| 17 |
)
|
| 18 |
if tokenizer.pad_token is None:
|
| 19 |
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
+
print("β
Model loaded")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
def parse_banking_sms(raw_text: str) -> dict:
|
| 23 |
+
sms = " ".join(raw_text.strip().split())
|
| 24 |
+
prompt = sms + "|"
|
|
|
|
| 25 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 26 |
if torch.cuda.is_available():
|
| 27 |
inputs = {k: v.cuda() for k, v in inputs.items()}
|
|
|
|
| 28 |
with torch.no_grad():
|
| 29 |
outputs = model.generate(
|
| 30 |
**inputs,
|
|
|
|
| 34 |
pad_token_id=tokenizer.eos_token_id,
|
| 35 |
eos_token_id=tokenizer.eos_token_id,
|
| 36 |
)
|
|
|
|
| 37 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
+
json_part = decoded[len(prompt) :].strip()
|
| 39 |
+
m = re.search(r"\{[^{}]+\}", json_part)
|
| 40 |
+
if m:
|
|
|
|
| 41 |
try:
|
| 42 |
+
parsed = json.loads(m.group())
|
| 43 |
return {
|
| 44 |
"date": parsed.get("date"),
|
| 45 |
"type": parsed.get("type"),
|
|
|
|
| 50 |
}
|
| 51 |
except json.JSONDecodeError:
|
| 52 |
pass
|
|
|
|
| 53 |
return {
|
| 54 |
"date": None,
|
| 55 |
"type": None,
|
| 56 |
"amount": None,
|
| 57 |
"category": None,
|
| 58 |
"last4": None,
|
| 59 |
+
"is_transaction": False,
|
| 60 |
}
|
| 61 |
|
| 62 |
+
def chatbot_response(user_message, history):
|
| 63 |
+
history = history or []
|
| 64 |
+
result = parse_banking_sms(user_message)
|
|
|
|
|
|
|
| 65 |
if result["is_transaction"]:
|
| 66 |
+
response = (
|
| 67 |
+
f"β
Transaction Detected!\n\n"
|
| 68 |
+
f"π
Date: {result['date']}\n"
|
| 69 |
+
f"π³ Type: {result['type'].title() if result['type'] else 'N/A'}\n"
|
| 70 |
+
f"π° Amount: {result['amount']}\n"
|
| 71 |
+
f"πͺ Category: {result['category']}\n"
|
| 72 |
+
f"π’ Last 4 Digits: {result['last4']}"
|
| 73 |
+
)
|
| 74 |
else:
|
| 75 |
+
response = ("βΉοΈ Non-Transaction Message\n\n"
|
| 76 |
+
"This looks like a promo or info message.")
|
| 77 |
+
history.append({"role": "user", "content": user_message})
|
| 78 |
+
history.append({"role": "assistant", "content": response})
|
| 79 |
+
return history, ""
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown(
|
| 83 |
+
"""
|
| 84 |
+
## π¦ Banking SMS JSON Parser Chatbot
|
| 85 |
+
|
| 86 |
+
Paste your banking SMS here β the app will detect whether it's a transaction
|
| 87 |
+
and extract date, amount, merchant, etc.
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
+
chatbot = gr.Chatbot(type="messages", label="Parser Bot", height=400)
|
| 91 |
+
msg = gr.Textbox(lines=3, label="Your SMS message")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
msg.submit(chatbot_response, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 93 |
msg.submit(lambda: "", None, msg)
|
| 94 |
+
gr.Markdown(f"\n---\n**Backend model:** `{MODEL_ID}`")
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
app = FastAPI()
|
| 97 |
app.mount("/", demo)
|
| 98 |
|