Spaces:
Sleeping
Sleeping
rafaldembski
commited on
Commit
•
a454a10
1
Parent(s):
a6df4ef
Update utils/functions.py
Browse files- utils/functions.py +141 -113
utils/functions.py
CHANGED
@@ -5,74 +5,102 @@ from phonenumbers import geocoder, carrier
|
|
5 |
import re
|
6 |
import requests
|
7 |
import os
|
8 |
-
import json
|
9 |
from datetime import datetime
|
10 |
import logging
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Konfiguracja logowania
|
13 |
-
logging.basicConfig(
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def add_fake_number(phone_number):
|
40 |
-
|
41 |
try:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
fake_numbers.append({
|
50 |
-
"phone_number": phone_number,
|
51 |
-
"reported_at": datetime.now().isoformat()
|
52 |
-
})
|
53 |
-
try:
|
54 |
-
with open(FAKE_NUMBERS_FILE, 'w', encoding='utf-8') as f:
|
55 |
-
json.dump(fake_numbers, f, ensure_ascii=False, indent=4)
|
56 |
-
logging.info(f"Numer {phone_number} został pomyślnie dodany do fake_numbers.json.")
|
57 |
return True
|
58 |
-
|
59 |
-
logging.
|
60 |
return False
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
64 |
|
65 |
-
# Sprawdzenie, czy numer telefonu jest w
|
66 |
def is_fake_number(phone_number):
|
|
|
67 |
try:
|
68 |
-
|
69 |
-
fake_numbers = json.load(f)
|
70 |
-
exists = any(entry["phone_number"] == phone_number for entry in fake_numbers)
|
71 |
logging.info(f"Sprawdzanie numeru {phone_number}: {'znaleziony' if exists else 'nie znaleziony'}.")
|
72 |
return exists
|
73 |
-
except
|
74 |
-
logging.error(f"Nie udało się
|
75 |
return False
|
|
|
|
|
76 |
|
77 |
# Funkcja do weryfikacji numeru telefonu
|
78 |
def get_phone_info(phone_number):
|
@@ -244,87 +272,87 @@ Provide your analysis and conclusions following the guidelines above."""
|
|
244 |
|
245 |
# Inicjalizacja pliku statystyk
|
246 |
def init_stats_file():
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
|
253 |
# Pobranie statystyk
|
254 |
def get_stats():
|
255 |
-
|
256 |
try:
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
|
|
|
|
|
|
263 |
return {"total_analyses": 0, "total_frauds_detected": 0}
|
|
|
|
|
264 |
|
265 |
# Aktualizacja statystyk analizy
|
266 |
def update_stats(fraud_detected=False):
|
267 |
-
|
268 |
-
try:
|
269 |
-
with open(stats_file, 'r', encoding='utf-8') as f:
|
270 |
-
stats = json.load(f)
|
271 |
-
except (json.JSONDecodeError, FileNotFoundError):
|
272 |
-
stats = {"total_analyses": 0, "total_frauds_detected": 0}
|
273 |
-
logging.warning(f"{stats_file} jest pusty lub uszkodzony. Inicjalizuję nową listę.")
|
274 |
-
|
275 |
-
stats["total_analyses"] += 1
|
276 |
-
if fraud_detected:
|
277 |
-
stats["total_frauds_detected"] += 1
|
278 |
-
|
279 |
try:
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
except Exception as e:
|
|
|
284 |
logging.error(f"Nie udało się zaktualizować statystyk: {e}")
|
285 |
-
|
286 |
-
|
287 |
-
def init_history_file():
|
288 |
-
history_file = os.path.join(BASE_DIR, '..', 'history.json')
|
289 |
-
if not os.path.exists(history_file):
|
290 |
-
with open(history_file, 'w', encoding='utf-8') as f:
|
291 |
-
json.dump([], f, ensure_ascii=False, indent=4)
|
292 |
-
logging.info(f"Utworzono nowy plik historii analiz {history_file}.")
|
293 |
|
294 |
# Dodanie wpisu do historii analiz
|
295 |
def add_to_history(message, phone_number, analysis, risk, recommendations):
|
296 |
-
|
297 |
try:
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
"phone_number": phone_number,
|
308 |
-
"analysis": analysis,
|
309 |
-
"risk_assessment": risk,
|
310 |
-
"recommendations": recommendations
|
311 |
-
})
|
312 |
-
|
313 |
-
try:
|
314 |
-
with open(history_file, 'w', encoding='utf-8') as f:
|
315 |
-
json.dump(history, f, ensure_ascii=False, indent=4)
|
316 |
logging.info(f"Dodano wpis do historii analiz dla numeru {phone_number}.")
|
317 |
except Exception as e:
|
|
|
318 |
logging.error(f"Nie udało się zapisać historii analiz dla numeru {phone_number}: {e}")
|
|
|
|
|
319 |
|
320 |
# Pobranie historii analiz
|
321 |
def get_history():
|
322 |
-
|
323 |
try:
|
324 |
-
|
325 |
-
history = json.load(f)
|
326 |
logging.info("Historia analiz została pobrana pomyślnie.")
|
327 |
return history
|
328 |
-
except
|
329 |
-
logging.error(f"Nie udało się
|
330 |
return []
|
|
|
|
|
|
5 |
import re
|
6 |
import requests
|
7 |
import os
|
|
|
8 |
from datetime import datetime
|
9 |
import logging
|
10 |
|
11 |
+
from sqlalchemy import create_engine, Column, String, Integer, DateTime
|
12 |
+
from sqlalchemy.ext.declarative import declarative_base
|
13 |
+
from sqlalchemy.orm import sessionmaker
|
14 |
+
|
15 |
+
from dotenv import load_dotenv
|
16 |
+
|
17 |
+
# Załaduj zmienne środowiskowe z pliku .env (jeśli używasz)
|
18 |
+
# W Streamlit Spaces Secrets są automatycznie dostępne
|
19 |
+
load_dotenv()
|
20 |
+
|
21 |
# Konfiguracja logowania
|
22 |
+
logging.basicConfig(
|
23 |
+
filename='app.log',
|
24 |
+
level=logging.INFO,
|
25 |
+
format='%(asctime)s %(levelname)s:%(message)s'
|
26 |
+
)
|
27 |
+
|
28 |
+
# Konfiguracja bazy danych SQLite
|
29 |
+
DATABASE_URL = "sqlite:///scam_detector.db"
|
30 |
+
|
31 |
+
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
32 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
33 |
+
Base = declarative_base()
|
34 |
+
|
35 |
+
# Definicje modeli
|
36 |
+
class FakeNumber(Base):
|
37 |
+
__tablename__ = "fake_numbers"
|
38 |
+
id = Column(Integer, primary_key=True, index=True)
|
39 |
+
phone_number = Column(String, unique=True, index=True, nullable=False)
|
40 |
+
reported_at = Column(DateTime, default=datetime.utcnow)
|
41 |
+
|
42 |
+
class AnalysisHistory(Base):
|
43 |
+
__tablename__ = "analysis_history"
|
44 |
+
id = Column(Integer, primary_key=True, index=True)
|
45 |
+
timestamp = Column(DateTime, default=datetime.utcnow)
|
46 |
+
message = Column(String, nullable=False)
|
47 |
+
phone_number = Column(String, nullable=False)
|
48 |
+
analysis = Column(String, nullable=False)
|
49 |
+
risk_assessment = Column(String, nullable=False)
|
50 |
+
recommendations = Column(String, nullable=False)
|
51 |
+
|
52 |
+
class Stats(Base):
|
53 |
+
__tablename__ = "stats"
|
54 |
+
id = Column(Integer, primary_key=True, index=True)
|
55 |
+
total_analyses = Column(Integer, default=0)
|
56 |
+
total_frauds_detected = Column(Integer, default=0)
|
57 |
+
|
58 |
+
# Tworzenie tabel w bazie danych
|
59 |
+
Base.metadata.create_all(bind=engine)
|
60 |
+
|
61 |
+
# Funkcje pomocnicze
|
62 |
+
|
63 |
+
def get_db():
|
64 |
+
db = SessionLocal()
|
65 |
+
try:
|
66 |
+
yield db
|
67 |
+
finally:
|
68 |
+
db.close()
|
69 |
+
|
70 |
+
# Dodanie numeru telefonu do bazy danych
|
71 |
def add_fake_number(phone_number):
|
72 |
+
db = SessionLocal()
|
73 |
try:
|
74 |
+
existing_number = db.query(FakeNumber).filter(FakeNumber.phone_number == phone_number).first()
|
75 |
+
if not existing_number:
|
76 |
+
new_number = FakeNumber(phone_number=phone_number)
|
77 |
+
db.add(new_number)
|
78 |
+
db.commit()
|
79 |
+
db.refresh(new_number)
|
80 |
+
logging.info(f"Numer {phone_number} został pomyślnie dodany do bazy danych.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
return True
|
82 |
+
else:
|
83 |
+
logging.info(f"Numer {phone_number} już istnieje w bazie danych.")
|
84 |
return False
|
85 |
+
except Exception as e:
|
86 |
+
db.rollback()
|
87 |
+
logging.error(f"Nie udało się zapisać numeru {phone_number}: {e}")
|
88 |
+
return False
|
89 |
+
finally:
|
90 |
+
db.close()
|
91 |
|
92 |
+
# Sprawdzenie, czy numer telefonu jest w bazie danych
|
93 |
def is_fake_number(phone_number):
|
94 |
+
db = SessionLocal()
|
95 |
try:
|
96 |
+
exists = db.query(FakeNumber).filter(FakeNumber.phone_number == phone_number).first() is not None
|
|
|
|
|
97 |
logging.info(f"Sprawdzanie numeru {phone_number}: {'znaleziony' if exists else 'nie znaleziony'}.")
|
98 |
return exists
|
99 |
+
except Exception as e:
|
100 |
+
logging.error(f"Nie udało się sprawdzić numeru {phone_number}: {e}")
|
101 |
return False
|
102 |
+
finally:
|
103 |
+
db.close()
|
104 |
|
105 |
# Funkcja do weryfikacji numeru telefonu
|
106 |
def get_phone_info(phone_number):
|
|
|
272 |
|
273 |
# Inicjalizacja pliku statystyk
|
274 |
def init_stats_file():
|
275 |
+
db = SessionLocal()
|
276 |
+
try:
|
277 |
+
stats = db.query(Stats).first()
|
278 |
+
if not stats:
|
279 |
+
new_stats = Stats()
|
280 |
+
db.add(new_stats)
|
281 |
+
db.commit()
|
282 |
+
logging.info("Utworzono nowe statystyki.")
|
283 |
+
except Exception as e:
|
284 |
+
db.rollback()
|
285 |
+
logging.error(f"Nie udało się zainicjalizować statystyk: {e}")
|
286 |
+
finally:
|
287 |
+
db.close()
|
288 |
|
289 |
# Pobranie statystyk
|
290 |
def get_stats():
|
291 |
+
db = SessionLocal()
|
292 |
try:
|
293 |
+
stats = db.query(Stats).first()
|
294 |
+
if stats:
|
295 |
+
logging.info("Statystyki zostały pobrane pomyślnie.")
|
296 |
+
return {"total_analyses": stats.total_analyses, "total_frauds_detected": stats.total_frauds_detected}
|
297 |
+
else:
|
298 |
+
logging.info("Brak statystyk w bazie danych.")
|
299 |
+
return {"total_analyses": 0, "total_frauds_detected": 0}
|
300 |
+
except Exception as e:
|
301 |
+
logging.error(f"Nie udało się pobrać statystyk: {e}")
|
302 |
return {"total_analyses": 0, "total_frauds_detected": 0}
|
303 |
+
finally:
|
304 |
+
db.close()
|
305 |
|
306 |
# Aktualizacja statystyk analizy
|
307 |
def update_stats(fraud_detected=False):
|
308 |
+
db = SessionLocal()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
try:
|
310 |
+
stats = db.query(Stats).first()
|
311 |
+
if not stats:
|
312 |
+
stats = Stats()
|
313 |
+
db.add(stats)
|
314 |
+
|
315 |
+
stats.total_analyses += 1
|
316 |
+
if fraud_detected:
|
317 |
+
stats.total_frauds_detected += 1
|
318 |
+
|
319 |
+
db.commit()
|
320 |
+
logging.info(f"Statystyki zostały zaktualizowane: Analiz {stats.total_analyses}, Oszustw {stats.total_frauds_detected}.")
|
321 |
except Exception as e:
|
322 |
+
db.rollback()
|
323 |
logging.error(f"Nie udało się zaktualizować statystyk: {e}")
|
324 |
+
finally:
|
325 |
+
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
# Dodanie wpisu do historii analiz
|
328 |
def add_to_history(message, phone_number, analysis, risk, recommendations):
|
329 |
+
db = SessionLocal()
|
330 |
try:
|
331 |
+
new_entry = AnalysisHistory(
|
332 |
+
message=message,
|
333 |
+
phone_number=phone_number,
|
334 |
+
analysis=analysis,
|
335 |
+
risk_assessment=risk,
|
336 |
+
recommendations=recommendations
|
337 |
+
)
|
338 |
+
db.add(new_entry)
|
339 |
+
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
340 |
logging.info(f"Dodano wpis do historii analiz dla numeru {phone_number}.")
|
341 |
except Exception as e:
|
342 |
+
db.rollback()
|
343 |
logging.error(f"Nie udało się zapisać historii analiz dla numeru {phone_number}: {e}")
|
344 |
+
finally:
|
345 |
+
db.close()
|
346 |
|
347 |
# Pobranie historii analiz
|
348 |
def get_history():
|
349 |
+
db = SessionLocal()
|
350 |
try:
|
351 |
+
history = db.query(AnalysisHistory).order_by(AnalysisHistory.timestamp.desc()).all()
|
|
|
352 |
logging.info("Historia analiz została pobrana pomyślnie.")
|
353 |
return history
|
354 |
+
except Exception as e:
|
355 |
+
logging.error(f"Nie udało się pobrać historii analiz: {e}")
|
356 |
return []
|
357 |
+
finally:
|
358 |
+
db.close()
|