kokluch commited on
Commit
664d4ee
·
1 Parent(s): 26278b4

Add logging

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from time import sleep
 
2
 
3
  import httpx
4
  from fastapi import FastAPI
@@ -12,9 +13,17 @@ from urlscan_client import UrlscanClient
12
  import requests
13
  import re
14
 
 
 
15
  app = FastAPI()
16
  urlscan = UrlscanClient()
17
 
 
 
 
 
 
 
18
  class MessageModel(BaseModel):
19
  text: str
20
 
@@ -74,7 +83,7 @@ def predict(model: InputModel) -> OutputModel:
74
  sender = model.query.sender
75
  text = model.query.message.text
76
 
77
- print(f"[{sender}] {text}")
78
 
79
  # Debug sleep
80
  pattern = r"^Sent from your Twilio trial account - sleep (\d+)$"
@@ -83,6 +92,7 @@ def predict(model: InputModel) -> OutputModel:
83
  if match:
84
  number_str = match.group(1)
85
  sleep_duration = int(number_str)
 
86
  sleep(sleep_duration)
87
  return OutputModel(action=ActionModel.JUNK, sub_action=SubActionModel.NONE)
88
 
@@ -92,6 +102,7 @@ def predict(model: InputModel) -> OutputModel:
92
 
93
  if match:
94
  category_str = match.group(1)
 
95
  match category_str:
96
  case 'junk':
97
  return OutputModel(action=ActionModel.JUNK, sub_action=SubActionModel.NONE)
@@ -104,7 +115,7 @@ def predict(model: InputModel) -> OutputModel:
104
  label = result[0]['label']
105
  score = result[0]['score']
106
 
107
- print(f"classification {label} score {score}")
108
 
109
  if label == 'LABEL_0':
110
  score = 1 - score
@@ -114,20 +125,20 @@ def predict(model: InputModel) -> OutputModel:
114
  commercial_stop = False
115
 
116
  if re.search(commercial_sender_pattern, sender):
117
- print("commercial sender")
118
  score = score * 0.9
119
  if re.search(commercial_stop_pattern, text):
120
- print("STOP founded")
121
  score = score * 0.9
122
  commercial_stop = True
123
  else:
124
- print("STOP missing")
125
 
126
  urls = extract_urls(text)
127
 
128
  if urls:
129
- print(f"found URLs: {urls}")
130
- print("searching for past scans")
131
  search_results = [urlscan.search(f"domain:{extract_domain_from_url(url)}") for url in urls]
132
 
133
  scan_results = []
@@ -139,15 +150,15 @@ def predict(model: InputModel) -> OutputModel:
139
  scan_results.append(scan_result)
140
 
141
  if not scan_results:
142
- print("scanning...")
143
  scan_results = [urlscan.scan(url) for url in urls]
144
 
145
  for result in scan_results:
146
  overall = result.get('verdicts', {}).get('overall', {})
147
- print(f"overall verdict: {overall}")
148
  if overall.get('hasVerdicts'):
149
  score = overall.get('score')
150
- print(f"verdict score {score}")
151
 
152
  if 0 < overall.get('score'):
153
  score = 1.0
@@ -155,10 +166,10 @@ def predict(model: InputModel) -> OutputModel:
155
  elif overall.get('score') < 0:
156
  score = score * 0.9
157
  else:
158
- print(f"no URL found")
159
  score = score * 0.9
160
 
161
- print(f"final score {score}")
162
  action = ActionModel.NONE
163
  if score > 0.7:
164
  action=ActionModel.JUNK
@@ -168,7 +179,7 @@ def predict(model: InputModel) -> OutputModel:
168
  else:
169
  action=ActionModel.JUNK
170
 
171
- print(f"final action {action}")
172
  return OutputModel(action=action, sub_action=SubActionModel.NONE)
173
 
174
  class ReportModel(BaseModel):
 
1
  from time import sleep
2
+ import logging
3
 
4
  import httpx
5
  from fastapi import FastAPI
 
13
  import requests
14
  import re
15
 
16
+
17
+
18
  app = FastAPI()
19
  urlscan = UrlscanClient()
20
 
21
+ # Configuration de base du logging
22
+ logging.basicConfig(
23
+ level=logging.DEBUG,
24
+ format='%(asctime)s [%(levelname)s] %(message)s'
25
+ )
26
+
27
  class MessageModel(BaseModel):
28
  text: str
29
 
 
83
  sender = model.query.sender
84
  text = model.query.message.text
85
 
86
+ logging.info(f"[{sender}] {text}")
87
 
88
  # Debug sleep
89
  pattern = r"^Sent from your Twilio trial account - sleep (\d+)$"
 
92
  if match:
93
  number_str = match.group(1)
94
  sleep_duration = int(number_str)
95
+ logging.debug(f"[DEBUG SLEEP] Sleeping for {sleep_duration} seconds for sender {sender}")
96
  sleep(sleep_duration)
97
  return OutputModel(action=ActionModel.JUNK, sub_action=SubActionModel.NONE)
98
 
 
102
 
103
  if match:
104
  category_str = match.group(1)
105
+ logging.info(f"[DEBUG CATEGORY] Forced category: {category_str} for sender {sender}")
106
  match category_str:
107
  case 'junk':
108
  return OutputModel(action=ActionModel.JUNK, sub_action=SubActionModel.NONE)
 
115
  label = result[0]['label']
116
  score = result[0]['score']
117
 
118
+ logging.info(f"[CLASSIFICATION] label={label} score={score}")
119
 
120
  if label == 'LABEL_0':
121
  score = 1 - score
 
125
  commercial_stop = False
126
 
127
  if re.search(commercial_sender_pattern, sender):
128
+ logging.info("[COMMERCIAL] Commercial sender detected")
129
  score = score * 0.9
130
  if re.search(commercial_stop_pattern, text):
131
+ logging.info("[COMMERCIAL] STOP keyword detected")
132
  score = score * 0.9
133
  commercial_stop = True
134
  else:
135
+ logging.info("[COMMERCIAL] STOP keyword missing")
136
 
137
  urls = extract_urls(text)
138
 
139
  if urls:
140
+ logging.info(f"[URL] URLs found: {urls}")
141
+ logging.info("[URL] Searching for previous scans")
142
  search_results = [urlscan.search(f"domain:{extract_domain_from_url(url)}") for url in urls]
143
 
144
  scan_results = []
 
150
  scan_results.append(scan_result)
151
 
152
  if not scan_results:
153
+ logging.info("[URL] No previous scan found, launching a new scan...")
154
  scan_results = [urlscan.scan(url) for url in urls]
155
 
156
  for result in scan_results:
157
  overall = result.get('verdicts', {}).get('overall', {})
158
+ logging.info(f"[URLSCAN] Overall verdict: {overall}")
159
  if overall.get('hasVerdicts'):
160
  score = overall.get('score')
161
+ logging.info(f"[URLSCAN] Verdict score: {score}")
162
 
163
  if 0 < overall.get('score'):
164
  score = 1.0
 
166
  elif overall.get('score') < 0:
167
  score = score * 0.9
168
  else:
169
+ logging.info(f"[URL] No URL found")
170
  score = score * 0.9
171
 
172
+ logging.info(f"[FINAL SCORE] {score}")
173
  action = ActionModel.NONE
174
  if score > 0.7:
175
  action=ActionModel.JUNK
 
179
  else:
180
  action=ActionModel.JUNK
181
 
182
+ logging.info(f"[FINAL ACTION] {action}")
183
  return OutputModel(action=action, sub_action=SubActionModel.NONE)
184
 
185
  class ReportModel(BaseModel):