TokenopolyHQ
commited on
Commit
Β·
d287b8a
1
Parent(s):
2e3e2fd
code update in app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,6 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 16 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 17 |
device_map="auto" if torch.cuda.is_available() else None,
|
| 18 |
)
|
| 19 |
-
|
| 20 |
if tokenizer.pad_token is None:
|
| 21 |
tokenizer.pad_token = tokenizer.eos_token
|
| 22 |
print("β
Model loaded successfully!")
|
|
@@ -31,7 +30,7 @@ def parse_banking_sms(raw_text: str) -> dict:
|
|
| 31 |
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 33 |
if torch.cuda.is_available():
|
| 34 |
-
inputs = {k: v.
|
| 35 |
|
| 36 |
with torch.no_grad():
|
| 37 |
outputs = model.generate(
|
|
@@ -73,78 +72,58 @@ def parse_banking_sms(raw_text: str) -> dict:
|
|
| 73 |
# --------------------------------------------------
|
| 74 |
# 3. Chatbot response handler
|
| 75 |
# --------------------------------------------------
|
| 76 |
-
def chatbot_response(
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
history
|
| 101 |
-
history
|
| 102 |
-
return history, history
|
| 103 |
|
| 104 |
# --------------------------------------------------
|
| 105 |
-
# 4. Gradio
|
| 106 |
# --------------------------------------------------
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
""
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 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 |
-
|
| 150 |
-
|
|
|
|
| 16 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 17 |
device_map="auto" if torch.cuda.is_available() else None,
|
| 18 |
)
|
|
|
|
| 19 |
if tokenizer.pad_token is None:
|
| 20 |
tokenizer.pad_token = tokenizer.eos_token
|
| 21 |
print("β
Model loaded successfully!")
|
|
|
|
| 30 |
|
| 31 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 32 |
if torch.cuda.is_available():
|
| 33 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 34 |
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = model.generate(
|
|
|
|
| 72 |
# --------------------------------------------------
|
| 73 |
# 3. Chatbot response handler
|
| 74 |
# --------------------------------------------------
|
| 75 |
+
def chatbot_response(raw_text):
|
| 76 |
+
history = []
|
| 77 |
+
result = parse_banking_sms(raw_text)
|
| 78 |
+
|
| 79 |
+
if result["is_transaction"]:
|
| 80 |
+
response = (
|
| 81 |
+
"β
**Transaction Detected!**\n\n"
|
| 82 |
+
f"π
**Date:** {result['date']}\n"
|
| 83 |
+
f"π³ **Type:** {result['type'].title() if result['type'] else 'N/A'}\n"
|
| 84 |
+
f"π° **Amount:** {result['amount']}\n"
|
| 85 |
+
f"πͺ **Category:** {result['category']}\n"
|
| 86 |
+
f"π’ **Last 4 Digits:** {result['last4']}\n\n"
|
| 87 |
+
"**Full JSON:**\n```
|
| 88 |
+
+ json.dumps(result, indent=2)
|
| 89 |
+
+ "\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 |
+
"**Classification:**\n```
|
| 96 |
+
+ json.dumps(result, indent=2)
|
| 97 |
+
+ "\n```"
|
| 98 |
+
)
|
| 99 |
|
| 100 |
+
history.append((raw_text, response))
|
| 101 |
+
return history
|
|
|
|
| 102 |
|
| 103 |
# --------------------------------------------------
|
| 104 |
+
# 4. Gradio Interface
|
| 105 |
# --------------------------------------------------
|
| 106 |
+
iface = gr.Interface(
|
| 107 |
+
fn=chatbot_response,
|
| 108 |
+
inputs=gr.Textbox(
|
| 109 |
+
lines=3,
|
| 110 |
+
placeholder="Paste your banking SMS or email hereβ¦"
|
| 111 |
+
),
|
| 112 |
+
outputs=gr.Chatbot(
|
| 113 |
+
label="Banking SMS Parser",
|
| 114 |
+
height=400
|
| 115 |
+
),
|
| 116 |
+
title="π¦ Banking SMS JSON Parser Chatbot",
|
| 117 |
+
description="Simply paste any banking SMS or email snippet and get structured JSON!",
|
| 118 |
+
examples=[
|
| 119 |
+
["Your A/c XX1234 debited for 5000 on 15-Jan-2024 at AMAZON"],
|
| 120 |
+
["2500 credited to A/c 9876 on 20-Dec-2023 from PAYROLL"],
|
| 121 |
+
["Card 4321 used for 120 at STARBUCKS on 10-Nov-2023"],
|
| 122 |
+
],
|
| 123 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# --------------------------------------------------
|
| 126 |
# 5. App launcher
|
| 127 |
# --------------------------------------------------
|
| 128 |
if __name__ == "__main__":
|
| 129 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|