TokenopolyHQ commited on
Commit
d287b8a
Β·
1 Parent(s): 2e3e2fd

code update in app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -68
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.to("cuda") for k, v in inputs.items()}
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(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(share=True, server_name="0.0.0.0", server_port=7860)
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)