nagarmayank commited on
Commit
e3dd34f
·
1 Parent(s): a8f2af1

Fix transaction classification handling and improve error reporting in check_txn_and_decide method

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -37,11 +37,10 @@ ist_tz = timezone("Asia/Kolkata")
37
  class TransactionParser(BaseModel):
38
  """This Pydantic class is used to parse the transaction message. The message is taken and the output is structured in a specific format based upon below definitions."""
39
 
40
- amount: str = Field(description="The amount of the transaction strictly in decimal format. Do not insert currency symbol.", example="123.45")
41
  dr_or_cr: str = Field(description="Identify if the transaction was debit (spent) or credit (received). Strictly choose one of the values - Debit or Credit")
42
  receiver: str = Field(description="The recipient of the transaction. Identify the Merchant Name from the message text.")
43
- category: str = Field(description="The category of the transaction. The category of the transaction is linked to the Merchant Name. Strictly choose from one the of values - Shopping,EMI,Education,Miscellaneous,Grocery,Utility,House Help,Travel,Transport,Food")
44
- # transaction_date: str = Field(description="Use today's date strictly in yyyy-mm-dd format.")
45
  transaction_origin: str = Field(description="The origin of the transaction. Provide the card or account number as well.")
46
 
47
  class TransactionClassification(BaseModel):
@@ -101,29 +100,29 @@ class Agent:
101
 
102
  SCOPES = ('https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive')
103
 
104
- service_account_info = json.loads(GOOGLESHEETS_CREDENTIALS)
105
  credentials = service_account.Credentials.from_service_account_info(service_account_info, scopes=SCOPES)
106
  client = pygsheets.authorize(custom_credentials=credentials)
107
  worksheet = client.open_by_url(SHEET_URL)
108
  wk = worksheet[0]
109
  # Get number of rows in the worksheet
110
- df = wk.get_as_df(start='A1', end='G999')
111
  nrows = df.shape[0]
112
- wk.update_value(f'A{nrows+2}', result.amount)
113
- wk.update_value(f'B{nrows+2}', result.dr_or_cr)
114
- wk.update_value(f'C{nrows+2}', result.receiver)
115
- wk.update_value(f'D{nrows+2}', result.category)
116
- wk.update_value(f'E{nrows+2}', datetime.now(ist_tz).strftime("%Y-%m-%d"))
117
- wk.update_value(f'F{nrows+2}', result.transaction_origin)
118
- wk.update_value(f'G{nrows+2}', state["messages"][0])
119
  print(f"{datetime.now(ist_tz)}: Writing transaction message to Google Sheets completed.")
120
- return {"messages": ["Transaction Completed"]}
121
 
122
  def check_txn_and_decide(self, state: AgentState):
123
  try:
124
- result = json.loads(state['messages'][-1].content)['classification']
125
  except json.JSONDecodeError:
126
- result = state['messages'][-1].content.strip()
127
 
128
  return result == "Transaction"
129
 
 
37
  class TransactionParser(BaseModel):
38
  """This Pydantic class is used to parse the transaction message. The message is taken and the output is structured in a specific format based upon below definitions."""
39
 
40
+ amount: str = Field(description="The amount of the transaction strictly in decimal format. Do not insert currency symbol.", example="123.45") # type: ignore
41
  dr_or_cr: str = Field(description="Identify if the transaction was debit (spent) or credit (received). Strictly choose one of the values - Debit or Credit")
42
  receiver: str = Field(description="The recipient of the transaction. Identify the Merchant Name from the message text.")
43
+ category: str = Field(description="The category of the transaction. The category of the transaction is linked to the Merchant Name. Strictly choose from one the of values - Shopping,EMI,Education,Miscellaneous,Grocery,Utility,House Help,Travel,Transport,Food,Insurance")
 
44
  transaction_origin: str = Field(description="The origin of the transaction. Provide the card or account number as well.")
45
 
46
  class TransactionClassification(BaseModel):
 
100
 
101
  SCOPES = ('https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive')
102
 
103
+ service_account_info = json.loads(GOOGLESHEETS_CREDENTIALS) # type: ignore
104
  credentials = service_account.Credentials.from_service_account_info(service_account_info, scopes=SCOPES)
105
  client = pygsheets.authorize(custom_credentials=credentials)
106
  worksheet = client.open_by_url(SHEET_URL)
107
  wk = worksheet[0]
108
  # Get number of rows in the worksheet
109
+ df = wk.get_as_df(start='A1', end='G999') # type: ignore
110
  nrows = df.shape[0]
111
+ wk.update_value(f'A{nrows+2}', result.amount) # type: ignore
112
+ wk.update_value(f'B{nrows+2}', result.dr_or_cr) # type: ignore
113
+ wk.update_value(f'C{nrows+2}', result.receiver) # type: ignore
114
+ wk.update_value(f'D{nrows+2}', result.category) # type: ignore
115
+ wk.update_value(f'E{nrows+2}', datetime.now(ist_tz).strftime("%Y-%m-%d")) # type: ignore
116
+ wk.update_value(f'F{nrows+2}', result.transaction_origin) # type: ignore
117
+ wk.update_value(f'G{nrows+2}', state["messages"][0]) # type: ignore
118
  print(f"{datetime.now(ist_tz)}: Writing transaction message to Google Sheets completed.")
119
+ return {"messages": ["Transaction Completed"]} # type: ignore
120
 
121
  def check_txn_and_decide(self, state: AgentState):
122
  try:
123
+ result = state['messages'][-1].classification # type: ignore
124
  except json.JSONDecodeError:
125
+ raise HTTPException(status_code=400, detail="Invalid response format from model")
126
 
127
  return result == "Transaction"
128