Spaces:
Running
Running
Update utils/functions.py
Browse files- utils/functions.py +90 -206
utils/functions.py
CHANGED
@@ -1,14 +1,15 @@
|
|
|
|
|
|
1 |
import phonenumbers
|
2 |
-
from phonenumbers import geocoder, carrier
|
3 |
import re
|
4 |
import requests
|
5 |
import os
|
6 |
from datetime import datetime
|
7 |
import logging
|
8 |
import json
|
9 |
-
|
10 |
-
|
11 |
-
import pytesseract # Upewnij si臋, 偶e modu艂 'pytesseract' jest zainstalowany: pip install pytesseract
|
12 |
|
13 |
# Konfiguracja logowania
|
14 |
logging.basicConfig(
|
@@ -24,10 +25,8 @@ FAKE_NUMBERS_FILE = os.path.join(DATA_DIR, 'fake_numbers.json')
|
|
24 |
HISTORY_FILE = os.path.join(DATA_DIR, 'history.json')
|
25 |
STATS_FILE = os.path.join(DATA_DIR, 'stats.json')
|
26 |
|
27 |
-
# Upewnij si臋, 偶e katalog 'data' istnieje
|
28 |
-
os.makedirs(DATA_DIR, exist_ok=True)
|
29 |
-
|
30 |
# Funkcje pomocnicze
|
|
|
31 |
def load_json(file_path):
|
32 |
"""艁aduje dane z pliku JSON. Je艣li plik nie istnieje, zwraca pust膮 list臋 lub domy艣lny obiekt."""
|
33 |
if not os.path.exists(file_path):
|
@@ -51,19 +50,10 @@ def save_json(file_path, data):
|
|
51 |
json.dump(data, file, ensure_ascii=False, indent=4)
|
52 |
logging.info(f"Dane zosta艂y zapisane do {file_path}.")
|
53 |
|
54 |
-
# Funkcja aktualizacji statystyk
|
55 |
-
def update_stats(fraud_detected):
|
56 |
-
"""Aktualizuje statystyki na podstawie wyniku analizy."""
|
57 |
-
stats = load_json(STATS_FILE)
|
58 |
-
stats["total_analyses"] += 1
|
59 |
-
if fraud_detected:
|
60 |
-
stats["total_frauds_detected"] += 1
|
61 |
-
save_json(STATS_FILE, stats)
|
62 |
-
logging.info(f"Statystyki zaktualizowane: {stats}.")
|
63 |
-
|
64 |
-
# Funkcje zwi膮zane z fa艂szywymi numerami telefon贸w
|
65 |
def add_fake_number(phone_number):
|
66 |
-
"""
|
|
|
|
|
67 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
68 |
if phone_number not in fake_numbers:
|
69 |
fake_numbers.append(phone_number)
|
@@ -75,21 +65,88 @@ def add_fake_number(phone_number):
|
|
75 |
return False
|
76 |
|
77 |
def is_fake_number(phone_number):
|
78 |
-
"""
|
|
|
|
|
79 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
80 |
exists = phone_number in fake_numbers
|
81 |
logging.info(f"Sprawdzanie numeru {phone_number}: {'znaleziony' if exists else 'nie znaleziony'}.")
|
82 |
return exists
|
83 |
|
84 |
def get_fake_numbers():
|
85 |
-
"""
|
|
|
|
|
86 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
87 |
return fake_numbers
|
88 |
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def simple_checks(message, language):
|
91 |
-
"""
|
|
|
|
|
92 |
warnings = []
|
|
|
93 |
scam_keywords = {
|
94 |
'Polish': ['pieni膮dze', 'przelew', 'has艂o', 'kod', 'nagroda', 'wygrana', 'pilne', 'pomoc', 'op艂ata', 'bank', 'karta', 'konto', 'logowanie', 'transakcja', 'weryfikacja', 'dane osobowe', 'szybka p艂atno艣膰', 'blokada konta', 'powiadomienie'],
|
95 |
'German': ['Geld', '脺berweisung', 'Passwort', 'Code', 'Preis', 'Gewinn', 'dringend', 'Hilfe', 'Geb眉hr', 'Bank', 'Karte', 'Konto', 'Anmeldung', 'Transaktion', 'Verifizierung', 'pers枚nliche Daten', 'schnelle Zahlung', 'Kontosperrung', 'Benachrichtigung'],
|
@@ -97,6 +154,7 @@ def simple_checks(message, language):
|
|
97 |
}
|
98 |
|
99 |
selected_keywords = scam_keywords.get(language, scam_keywords['English'])
|
|
|
100 |
message_lower = message.lower()
|
101 |
|
102 |
if any(keyword.lower() in message_lower for keyword in selected_keywords):
|
@@ -107,13 +165,15 @@ def simple_checks(message, language):
|
|
107 |
warnings.append("Wiadomo艣膰 zawiera pro艣b臋 o poufne informacje.")
|
108 |
return warnings
|
109 |
|
110 |
-
def analyze_message(
|
111 |
-
"""
|
|
|
|
|
112 |
if not api_key:
|
113 |
logging.error("Brak klucza API.")
|
114 |
return "Brak klucza API.", "Brak klucza API.", "Brak klucza API."
|
115 |
|
116 |
-
url = "https://api.sambanova.ai/v1/chat/completions"
|
117 |
headers = {
|
118 |
"Authorization": f"Bearer {api_key}",
|
119 |
"Content-Type": "application/json"
|
@@ -197,152 +257,12 @@ Your response should be formatted exactly as specified above, using the <analysi
|
|
197 |
"""
|
198 |
}
|
199 |
|
200 |
-
system_prompt = system_prompts.get(language, system_prompts['English'])
|
201 |
|
202 |
user_prompt = f"""Analyze the following message for potential fraud:
|
203 |
|
204 |
-
Message: "{
|
205 |
-
Sender
|
206 |
-
|
207 |
-
Additional Information:
|
208 |
-
{additional_info}
|
209 |
-
|
210 |
-
Provide your analysis and conclusions following the guidelines above."""
|
211 |
-
|
212 |
-
payload = {
|
213 |
-
"model": "Meta-Llama-3.1-8B-Instruct",
|
214 |
-
"messages": [
|
215 |
-
{"role": "system", "content": system_prompt},
|
216 |
-
{"role": "user", "content": user_prompt}
|
217 |
-
],
|
218 |
-
"max_tokens": 1000,
|
219 |
-
"temperature": 0.2,
|
220 |
-
"top_p": 0.9,
|
221 |
-
"stop": ["<|eot_id|>"]
|
222 |
-
}
|
223 |
-
|
224 |
-
try:
|
225 |
-
response = requests.post(url, headers=headers, json=payload)
|
226 |
-
if response.status_code == 200:
|
227 |
-
data = response.json()
|
228 |
-
ai_response = data['choices'][0]['message']['content']
|
229 |
-
# Parsowanie odpowiedzi
|
230 |
-
analysis = re.search(r'<analysis>(.*?)</analysis>', ai_response, re.DOTALL)
|
231 |
-
risk_assessment = re.search(r'<risk_assessment>(.*?)</risk_assessment>', ai_response, re.DOTALL)
|
232 |
-
recommendations = re.search(r'<recommendations>(.*?)</recommendations>', ai_response, re.DOTALL)
|
233 |
-
|
234 |
-
analysis_text = analysis.group(1).strip() if analysis else "Brak analizy."
|
235 |
-
risk_text = risk_assessment.group(1).strip() if risk_assessment else "Brak oceny ryzyka."
|
236 |
-
recommendations_text = recommendations.group(1).strip() if recommendations else "Brak zalece艅."
|
237 |
-
|
238 |
-
return analysis_text, risk_text, recommendations_text
|
239 |
-
else:
|
240 |
-
logging.error(f"B艂膮d API: {response.status_code} - {response.text}")
|
241 |
-
return f"B艂膮d API: {response.status_code} - {response.text}", "B艂膮d analizy.", "B艂膮d analizy."
|
242 |
-
except Exception as e:
|
243 |
-
logging.error(f"B艂膮d po艂膮czenia z API: {e}")
|
244 |
-
return f"B艂膮d po艂膮czenia z API: {e}", "B艂膮d analizy.", "B艂膮d analizy."
|
245 |
-
|
246 |
-
# Funkcje analizy email
|
247 |
-
def analyze_email_message(content, sender_info, additional_info, api_key, language):
|
248 |
-
"""Analizuje tre艣膰 wiadomo艣ci email za pomoc膮 modelu AI, wykorzystuj膮c system prompts."""
|
249 |
-
if not api_key:
|
250 |
-
logging.error("Brak klucza API.")
|
251 |
-
return "Brak klucza API.", "Brak klucza API.", "Brak klucza API."
|
252 |
-
|
253 |
-
url = "https://api.sambanova.ai/v1/chat/completions"
|
254 |
-
headers = {
|
255 |
-
"Authorization": f"Bearer {api_key}",
|
256 |
-
"Content-Type": "application/json"
|
257 |
-
}
|
258 |
-
|
259 |
-
system_prompts = {
|
260 |
-
'Polish': """
|
261 |
-
Jeste艣 zaawansowanym asystentem AI specjalizuj膮cym si臋 w identyfikacji fa艂szywych wiadomo艣ci email. Twoim zadaniem jest przeprowadzenie szczeg贸艂owej analizy poni偶szej wiadomo艣ci email, wykorzystuj膮c g艂臋boki proces my艣lenia i dostarczaj膮c kompleksow膮 ocen臋. Twoja odpowied藕 powinna by膰 podzielona na trzy sekcje:
|
262 |
-
|
263 |
-
<analysis>
|
264 |
-
**Analiza Tre艣ci Wiadomo艣ci:**
|
265 |
-
- Przeprowad藕 szczeg贸艂ow膮 analiz臋 tre艣ci wiadomo艣ci email, identyfikuj膮c potencjalne czerwone flagi, takie jak b艂臋dy j臋zykowe, podejrzane linki, pro艣by o dane osobowe, pilne pro艣by o kontakt itp.
|
266 |
-
- Oce艅 autentyczno艣膰 adresu email nadawcy.
|
267 |
-
- Opisz kontekst j臋zykowy i kulturowy wiadomo艣ci.
|
268 |
-
- Zidentyfikuj wszelkie elementy, kt贸re mog膮 sugerowa膰, 偶e wiadomo艣膰 jest pr贸b膮 phishingu lub oszustwa.
|
269 |
-
</analysis>
|
270 |
-
|
271 |
-
<risk_assessment>
|
272 |
-
**Ocena Ryzyka Oszustwa:**
|
273 |
-
- Na podstawie analizy tre艣ci i dost臋pnych informacji oce艅 prawdopodobie艅stwo, 偶e wiadomo艣膰 email jest oszustwem. U偶yj skali od 1 do 10, gdzie 1 oznacza bardzo niskie ryzyko, a 10 bardzo wysokie ryzyko.
|
274 |
-
- Wyja艣nij, jakie czynniki wp艂ywaj膮 na t臋 ocen臋.
|
275 |
-
</risk_assessment>
|
276 |
-
|
277 |
-
<recommendations>
|
278 |
-
**Zalecenia dla U偶ytkownika:**
|
279 |
-
- Podaj jasne i konkretne zalecenia dotycz膮ce dalszych krok贸w, kt贸re u偶ytkownik powinien podj膮膰.
|
280 |
-
- Uwzgl臋dnij sugestie dotycz膮ce bezpiecze艅stwa, takie jak nieklikanie w podejrzane linki, nieotwieranie za艂膮cznik贸w, zg艂aszanie wiadomo艣ci do odpowiednich instytucji itp.
|
281 |
-
- Je艣li to mo偶liwe, zasugeruj dodatkowe 艣rodki ostro偶no艣ci, kt贸re u偶ytkownik mo偶e podj膮膰, aby chroni膰 swoje dane osobowe i finansowe.
|
282 |
-
</recommendations>
|
283 |
-
|
284 |
-
Twoja odpowied藕 powinna by膰 sformatowana dok艂adnie w powy偶szy spos贸b, u偶ywaj膮c znacznik贸w <analysis>, <risk_assessment> i <recommendations>. Upewnij si臋, 偶e ka偶da sekcja jest wype艂niona kompletnie i szczeg贸艂owo.
|
285 |
-
""",
|
286 |
-
'German': """
|
287 |
-
Du bist ein fortgeschrittener KI-Assistent, spezialisiert auf die Identifizierung gef盲lschter Nachrichtenemail. Deine Aufgabe ist es, eine detaillierte Analyse der folgenden Nachricht email durchzuf眉hren, indem du einen tiefgreifenden Denkprozess nutzt und eine umfassende Bewertung lieferst. Deine Antwort sollte in drei Abschnitte unterteilt sein:
|
288 |
-
|
289 |
-
<analysis>
|
290 |
-
**Nachrichteninhaltsanalyse:**
|
291 |
-
- F眉hre eine detaillierte Analyse des Nachrichteninhalts durch und identifiziere potenzielle rote Flaggen wie sprachliche Fehler, verd盲chtige Links, Aufforderungen zur Preisgabe pers枚nlicher Daten, dringende Kontaktanfragen usw.
|
292 |
-
- Beurteile die Authentizit盲t der E-Mail-Adresse des Absenders.
|
293 |
-
- Beschreibe den sprachlichen und kulturellen Kontext der Nachricht.
|
294 |
-
- Identifiziere alle Elemente, die darauf hindeuten k枚nnten, dass die Nachricht ein Versuch von Phishing oder Betrug ist.
|
295 |
-
</analysis>
|
296 |
-
|
297 |
-
<risk_assessment>
|
298 |
-
**Betrugsrisikobewertung:**
|
299 |
-
- Basierend auf der Inhaltsanalyse und den verf眉gbaren Informationen, bewerte die Wahrscheinlichkeit, dass die Nachricht email ein Betrug ist. Verwende eine Skala von 1 bis 10, wobei 1 sehr geringes Risiko und 10 sehr hohes Risiko bedeutet.
|
300 |
-
- Erkl盲re, welche Faktoren diese Bewertung beeinflussen.
|
301 |
-
</risk_assessment>
|
302 |
-
|
303 |
-
<recommendations>
|
304 |
-
**Empfehlungen f眉r den Benutzer:**
|
305 |
-
- Gib klare und konkrete Empfehlungen zu den n盲chsten Schritten, die der Benutzer unternehmen sollte.
|
306 |
-
- Ber眉cksichtige Sicherheitsempfehlungen wie das Nicht-Klicken auf verd盲chtige Links, das Nicht-Ot枚ffnen von Anh盲ngen, das Melden der Nachricht an entsprechende Beh枚rden usw.
|
307 |
-
- Wenn m枚glich, schlage zus盲tzliche Vorsichtsma脽nahmen vor, die der Benutzer ergreifen kann, um seine pers枚nlichen und finanziellen Daten zu sch眉tzen.
|
308 |
-
</recommendations>
|
309 |
-
|
310 |
-
Deine Antwort sollte genau nach den oben genannten Richtlinien formatiert sein und die Markierungen <analysis>, <risk_assessment> und <recommendations> verwenden. Stelle sicher, dass jeder Abschnitt vollst盲ndig und detailliert ausgef眉llt ist.
|
311 |
-
""",
|
312 |
-
'English': """
|
313 |
-
You are an advanced AI assistant specializing in identifying fake email messages. Your task is to conduct a detailed analysis of the following email message, utilizing a deep thinking process and providing a comprehensive assessment. Your response should be divided into three sections:
|
314 |
-
|
315 |
-
<analysis>
|
316 |
-
**Message Content Analysis:**
|
317 |
-
- Conduct a detailed analysis of the email message content, identifying potential red flags such as language errors, suspicious links, requests for personal information, urgent contact requests, etc.
|
318 |
-
- Assess the authenticity of the sender's email address.
|
319 |
-
- Describe the linguistic and cultural context of the message.
|
320 |
-
- Identify any elements that may suggest the message is an attempt at phishing or fraud.
|
321 |
-
</analysis>
|
322 |
-
|
323 |
-
<risk_assessment>
|
324 |
-
**Fraud Risk Assessment:**
|
325 |
-
- Based on the content analysis and available information, assess the likelihood that the email message is fraudulent. Use a scale from 1 to 10, where 1 indicates very low risk and 10 indicates very high risk.
|
326 |
-
- Explain the factors that influence this assessment.
|
327 |
-
</risk_assessment>
|
328 |
-
|
329 |
-
<recommendations>
|
330 |
-
**User Recommendations:**
|
331 |
-
- Provide clear and concrete recommendations regarding the next steps the user should take.
|
332 |
-
- Include security suggestions such as not clicking on suspicious links, not opening attachments, reporting the message to appropriate authorities, etc.
|
333 |
-
- If possible, suggest additional precautionary measures the user can take to protect their personal and financial information.
|
334 |
-
</recommendations>
|
335 |
-
|
336 |
-
Your response should be formatted exactly as specified above, using the <analysis>, <risk_assessment>, and <recommendations> tags. Ensure that each section is thoroughly and comprehensively filled out.
|
337 |
-
"""
|
338 |
-
}
|
339 |
-
|
340 |
-
system_prompt = system_prompts.get(language, system_prompts['English'])
|
341 |
-
|
342 |
-
user_prompt = f"""Analyze the following email message for potential fraud:
|
343 |
-
|
344 |
-
Email Content: "{content}"
|
345 |
-
Sender Information: "{sender_info}"
|
346 |
|
347 |
Additional Information:
|
348 |
{additional_info}
|
@@ -350,7 +270,7 @@ Additional Information:
|
|
350 |
Provide your analysis and conclusions following the guidelines above."""
|
351 |
|
352 |
payload = {
|
353 |
-
"model": "Meta-Llama-3.1-8B-Instruct",
|
354 |
"messages": [
|
355 |
{"role": "system", "content": system_prompt},
|
356 |
{"role": "user", "content": user_prompt}
|
@@ -382,39 +302,3 @@ Provide your analysis and conclusions following the guidelines above."""
|
|
382 |
except Exception as e:
|
383 |
logging.error(f"B艂膮d po艂膮czenia z API: {e}")
|
384 |
return f"B艂膮d po艂膮czenia z API: {e}", "B艂膮d analizy.", "B艂膮d analizy."
|
385 |
-
|
386 |
-
# Funkcja analizy stron internetowych
|
387 |
-
def analyze_website(url, language):
|
388 |
-
"""Analizuje zawarto艣膰 strony internetowej."""
|
389 |
-
if not api_key:
|
390 |
-
logging.error("Brak klucza API.")
|
391 |
-
return "Brak klucza API.", "Brak klucza API."
|
392 |
-
|
393 |
-
try:
|
394 |
-
response = requests.get(url)
|
395 |
-
if response.status_code == 200:
|
396 |
-
content = response.text
|
397 |
-
logging.info(f"Zawarto艣膰 strony {url} zosta艂a pobrana pomy艣lnie.")
|
398 |
-
return content
|
399 |
-
else:
|
400 |
-
logging.error(f"B艂膮d podczas pobierania zawarto艣ci strony {url}: {response.status_code}")
|
401 |
-
return f"B艂膮d: {response.status_code}", "B艂膮d analizy strony.", "B艂膮d analizy strony."
|
402 |
-
except Exception as e:
|
403 |
-
logging.error(f"B艂膮d po艂膮czenia z {url}: {e}")
|
404 |
-
return f"B艂膮d po艂膮czenia z {url}: {e}", "B艂膮d analizy strony.", "B艂膮d analizy strony."
|
405 |
-
|
406 |
-
def get_phone_info(phone_number):
|
407 |
-
"""Weryfikuje numer telefonu i zwraca informacje o kraju i operatorze."""
|
408 |
-
try:
|
409 |
-
parsed_number = phonenumbers.parse(phone_number, None)
|
410 |
-
country = geocoder.description_for_number(parsed_number, 'pl')
|
411 |
-
operator = carrier.name_for_number(parsed_number, 'pl')
|
412 |
-
if not country:
|
413 |
-
country = "Nieznany"
|
414 |
-
if not operator:
|
415 |
-
operator = "Nieznany"
|
416 |
-
logging.info(f"Numer {phone_number} - Kraj: {country}, Operator: {operator}.")
|
417 |
-
return country, operator
|
418 |
-
except NumberParseException as e:
|
419 |
-
logging.error(f"Nie uda艂o si臋 przetworzy膰 numeru telefonu {phone_number}: {e}")
|
420 |
-
return "Nieznany", "Nieznany"
|
|
|
1 |
+
# utils/functions.py
|
2 |
+
|
3 |
import phonenumbers
|
4 |
+
from phonenumbers import geocoder, carrier
|
5 |
import re
|
6 |
import requests
|
7 |
import os
|
8 |
from datetime import datetime
|
9 |
import logging
|
10 |
import json
|
11 |
+
|
12 |
+
import pycountry # Upewnij si臋, 偶e zainstalowa艂e艣 t臋 bibliotek臋: pip install pycountry
|
|
|
13 |
|
14 |
# Konfiguracja logowania
|
15 |
logging.basicConfig(
|
|
|
25 |
HISTORY_FILE = os.path.join(DATA_DIR, 'history.json')
|
26 |
STATS_FILE = os.path.join(DATA_DIR, 'stats.json')
|
27 |
|
|
|
|
|
|
|
28 |
# Funkcje pomocnicze
|
29 |
+
|
30 |
def load_json(file_path):
|
31 |
"""艁aduje dane z pliku JSON. Je艣li plik nie istnieje, zwraca pust膮 list臋 lub domy艣lny obiekt."""
|
32 |
if not os.path.exists(file_path):
|
|
|
50 |
json.dump(data, file, ensure_ascii=False, indent=4)
|
51 |
logging.info(f"Dane zosta艂y zapisane do {file_path}.")
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
def add_fake_number(phone_number):
|
54 |
+
"""
|
55 |
+
Dodaje numer telefonu do pliku fake_numbers.json jako fa艂szywy, je艣li jeszcze go tam nie ma.
|
56 |
+
"""
|
57 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
58 |
if phone_number not in fake_numbers:
|
59 |
fake_numbers.append(phone_number)
|
|
|
65 |
return False
|
66 |
|
67 |
def is_fake_number(phone_number):
|
68 |
+
"""
|
69 |
+
Sprawdza, czy dany numer telefonu jest oznaczony jako fa艂szywy w pliku fake_numbers.json.
|
70 |
+
"""
|
71 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
72 |
exists = phone_number in fake_numbers
|
73 |
logging.info(f"Sprawdzanie numeru {phone_number}: {'znaleziony' if exists else 'nie znaleziony'}.")
|
74 |
return exists
|
75 |
|
76 |
def get_fake_numbers():
|
77 |
+
"""
|
78 |
+
Pobiera list臋 fa艂szywych numer贸w z pliku fake_numbers.json.
|
79 |
+
"""
|
80 |
fake_numbers = load_json(FAKE_NUMBERS_FILE)
|
81 |
return fake_numbers
|
82 |
|
83 |
+
def add_to_history(message, phone_number, analysis, risk, recommendations):
|
84 |
+
"""
|
85 |
+
Dodaje wpis do historii analiz w pliku history.json.
|
86 |
+
"""
|
87 |
+
history = load_json(HISTORY_FILE)
|
88 |
+
history.append({
|
89 |
+
"timestamp": datetime.now().isoformat(),
|
90 |
+
"message": message,
|
91 |
+
"phone_number": phone_number,
|
92 |
+
"analysis": analysis,
|
93 |
+
"risk_assessment": risk,
|
94 |
+
"recommendations": recommendations
|
95 |
+
})
|
96 |
+
save_json(HISTORY_FILE, history)
|
97 |
+
logging.info(f"Dodano wpis do history.json dla numeru {phone_number}.")
|
98 |
+
|
99 |
+
def get_history():
|
100 |
+
"""
|
101 |
+
Pobiera histori臋 analiz z pliku history.json jako list臋 s艂ownik贸w.
|
102 |
+
"""
|
103 |
+
history = load_json(HISTORY_FILE)
|
104 |
+
logging.info("Historia analiz zosta艂a pobrana pomy艣lnie.")
|
105 |
+
return history
|
106 |
+
|
107 |
+
def update_stats(fraud_detected=False):
|
108 |
+
"""
|
109 |
+
Aktualizuje statystyki analiz w pliku stats.json.
|
110 |
+
"""
|
111 |
+
stats = load_json(STATS_FILE)
|
112 |
+
stats["total_analyses"] += 1
|
113 |
+
if fraud_detected:
|
114 |
+
stats["total_frauds_detected"] += 1
|
115 |
+
save_json(STATS_FILE, stats)
|
116 |
+
logging.info(f"Statystyki zosta艂y zaktualizowane: Analiz {stats['total_analyses']}, Oszustw {stats['total_frauds_detected']}.")
|
117 |
+
|
118 |
+
def get_stats():
|
119 |
+
"""
|
120 |
+
Pobiera statystyki analiz z pliku stats.json.
|
121 |
+
"""
|
122 |
+
stats = load_json(STATS_FILE)
|
123 |
+
logging.info("Statystyki zosta艂y pobrane pomy艣lnie.")
|
124 |
+
return stats
|
125 |
+
|
126 |
+
def get_phone_info(phone_number):
|
127 |
+
"""
|
128 |
+
Weryfikuje numer telefonu i zwraca informacje o kraju i operatorze.
|
129 |
+
"""
|
130 |
+
try:
|
131 |
+
parsed_number = phonenumbers.parse(phone_number, None)
|
132 |
+
country = geocoder.description_for_number(parsed_number, 'pl') # Zmiana na 'pl' dla polskiego
|
133 |
+
operator = carrier.name_for_number(parsed_number, 'pl') # Zmiana na 'pl' dla polskiego
|
134 |
+
if not country:
|
135 |
+
country = "Nieznany"
|
136 |
+
if not operator:
|
137 |
+
operator = "Nieznany"
|
138 |
+
logging.info(f"Numer {phone_number} - Kraj: {country}, Operator: {operator}.")
|
139 |
+
return country, operator
|
140 |
+
except phonenumbers.NumberParseException as e:
|
141 |
+
logging.error(f"Nie uda艂o si臋 przetworzy膰 numeru telefonu {phone_number}: {e}")
|
142 |
+
return "Nieznany", "Nieznany"
|
143 |
+
|
144 |
def simple_checks(message, language):
|
145 |
+
"""
|
146 |
+
Przeprowadza proste sprawdzenia heurystyczne wiadomo艣ci SMS.
|
147 |
+
"""
|
148 |
warnings = []
|
149 |
+
# Baza s艂贸w kluczowych (polski, niemiecki, angielski)
|
150 |
scam_keywords = {
|
151 |
'Polish': ['pieni膮dze', 'przelew', 'has艂o', 'kod', 'nagroda', 'wygrana', 'pilne', 'pomoc', 'op艂ata', 'bank', 'karta', 'konto', 'logowanie', 'transakcja', 'weryfikacja', 'dane osobowe', 'szybka p艂atno艣膰', 'blokada konta', 'powiadomienie'],
|
152 |
'German': ['Geld', '脺berweisung', 'Passwort', 'Code', 'Preis', 'Gewinn', 'dringend', 'Hilfe', 'Geb眉hr', 'Bank', 'Karte', 'Konto', 'Anmeldung', 'Transaktion', 'Verifizierung', 'pers枚nliche Daten', 'schnelle Zahlung', 'Kontosperrung', 'Benachrichtigung'],
|
|
|
154 |
}
|
155 |
|
156 |
selected_keywords = scam_keywords.get(language, scam_keywords['English'])
|
157 |
+
|
158 |
message_lower = message.lower()
|
159 |
|
160 |
if any(keyword.lower() in message_lower for keyword in selected_keywords):
|
|
|
165 |
warnings.append("Wiadomo艣膰 zawiera pro艣b臋 o poufne informacje.")
|
166 |
return warnings
|
167 |
|
168 |
+
def analyze_message(message, phone_number, additional_info, api_key, language):
|
169 |
+
"""
|
170 |
+
Analizuje wiadomo艣膰 SMS za pomoc膮 API SambaNova.
|
171 |
+
"""
|
172 |
if not api_key:
|
173 |
logging.error("Brak klucza API.")
|
174 |
return "Brak klucza API.", "Brak klucza API.", "Brak klucza API."
|
175 |
|
176 |
+
url = "https://api.sambanova.ai/v1/chat/completions" # Upewnij si臋, 偶e to poprawny URL
|
177 |
headers = {
|
178 |
"Authorization": f"Bearer {api_key}",
|
179 |
"Content-Type": "application/json"
|
|
|
257 |
"""
|
258 |
}
|
259 |
|
260 |
+
system_prompt = system_prompts.get(language, system_prompts['English']) # Domy艣lnie angielski, je艣li j臋zyk nie jest obs艂ugiwany
|
261 |
|
262 |
user_prompt = f"""Analyze the following message for potential fraud:
|
263 |
|
264 |
+
Message: "{message}"
|
265 |
+
Sender's Phone Number: "{phone_number}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
Additional Information:
|
268 |
{additional_info}
|
|
|
270 |
Provide your analysis and conclusions following the guidelines above."""
|
271 |
|
272 |
payload = {
|
273 |
+
"model": "Meta-Llama-3.1-8B-Instruct", # Upewnij si臋, 偶e to poprawny model API
|
274 |
"messages": [
|
275 |
{"role": "system", "content": system_prompt},
|
276 |
{"role": "user", "content": user_prompt}
|
|
|
302 |
except Exception as e:
|
303 |
logging.error(f"B艂膮d po艂膮czenia z API: {e}")
|
304 |
return f"B艂膮d po艂膮czenia z API: {e}", "B艂膮d analizy.", "B艂膮d analizy."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|