Spaces:
Running
Running
Update pages/Analysis.py
Browse files- pages/Analysis.py +158 -44
pages/Analysis.py
CHANGED
@@ -8,7 +8,7 @@ from utils.functions import (
|
|
8 |
update_stats,
|
9 |
add_to_history,
|
10 |
is_fake_number,
|
11 |
-
add_fake_number
|
12 |
)
|
13 |
import os
|
14 |
import re
|
@@ -31,16 +31,19 @@ def check_urls_with_phishtank(urls):
|
|
31 |
'format': 'json',
|
32 |
'url': url
|
33 |
}
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
44 |
return phishing_urls
|
45 |
|
46 |
def check_urls_with_safe_browsing(urls):
|
@@ -61,18 +64,21 @@ def check_urls_with_safe_browsing(urls):
|
|
61 |
'threatEntries': [{'url': url} for url in urls]
|
62 |
}
|
63 |
}
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
return unsafe_urls
|
77 |
|
78 |
def show_analysis(language):
|
@@ -118,12 +124,110 @@ def show_analysis(language):
|
|
118 |
'safe_browsing_warning': "Następujące URL są oznaczone jako niebezpieczne przez Google Safe Browsing:",
|
119 |
'no_safe_browsing_threats': "Brak zagrożeń według Google Safe Browsing.",
|
120 |
'api_key_missing': "Brak klucza API dla usługi: {}",
|
121 |
-
'analyzing_message': "Analizowanie wiadomości za pomocą modelu AI..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
},
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
|
126 |
-
translations = page_translations.get(language, page_translations['
|
127 |
|
128 |
st.header(translations['header'])
|
129 |
st.write(translations['content'])
|
@@ -170,12 +274,14 @@ def show_analysis(language):
|
|
170 |
submitted = st.form_submit_button(translations['analyze_button'])
|
171 |
|
172 |
if submitted:
|
|
|
173 |
if not message.strip():
|
174 |
st.error(translations['input_placeholder'])
|
175 |
elif not phone_number.strip():
|
176 |
st.error(translations['phone_input_label'])
|
177 |
else:
|
178 |
# Informacje o numerze telefonu
|
|
|
179 |
country, operator = get_phone_info(phone_number)
|
180 |
if country and operator:
|
181 |
st.subheader(translations['phone_info_header'])
|
@@ -185,6 +291,7 @@ def show_analysis(language):
|
|
185 |
st.write(translations['unable_to_retrieve'])
|
186 |
|
187 |
# Sprawdzenie, czy numer jest w bazie fałszywych numerów
|
|
|
188 |
if is_fake_number(phone_number):
|
189 |
st.warning(translations['result_positive'])
|
190 |
fraud_detected = True
|
@@ -193,6 +300,7 @@ def show_analysis(language):
|
|
193 |
fraud_detected = False
|
194 |
|
195 |
# Przeprowadź proste sprawdzenia heurystyczne
|
|
|
196 |
warnings = simple_checks(message, language)
|
197 |
if warnings:
|
198 |
st.warning(translations['red_flags_found'])
|
@@ -213,6 +321,7 @@ def show_analysis(language):
|
|
213 |
"""
|
214 |
|
215 |
# Analiza URL
|
|
|
216 |
urls = extract_urls(message)
|
217 |
if urls:
|
218 |
st.subheader(translations['url_analysis_header'])
|
@@ -250,26 +359,31 @@ def show_analysis(language):
|
|
250 |
st.error("API key is missing. Please set the SAMBANOVA_API_KEY environment variable.")
|
251 |
else:
|
252 |
with st.spinner(translations['analyzing_message']):
|
253 |
-
|
254 |
-
message, phone_number, additional_info, api_key, language
|
255 |
-
)
|
256 |
-
st.subheader("📑 Message Content Analysis:")
|
257 |
-
st.markdown(analysis_text)
|
258 |
-
st.subheader("⚠️ Fraud Risk Assessment:")
|
259 |
-
st.markdown(risk_text)
|
260 |
-
st.subheader("✅ User Recommendations:")
|
261 |
-
st.markdown(recommendations_text)
|
262 |
-
|
263 |
-
# Aktualizacja statystyk na podstawie oceny ryzyka
|
264 |
try:
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
-
|
272 |
-
|
|
|
|
|
273 |
|
274 |
st.info(translations['remember_caution'])
|
275 |
|
|
|
8 |
update_stats,
|
9 |
add_to_history,
|
10 |
is_fake_number,
|
11 |
+
add_fake_number
|
12 |
)
|
13 |
import os
|
14 |
import re
|
|
|
31 |
'format': 'json',
|
32 |
'url': url
|
33 |
}
|
34 |
+
try:
|
35 |
+
response = requests.post('https://checkurl.phishtank.com/checkurl/', data=params)
|
36 |
+
if response.status_code == 200:
|
37 |
+
data = response.json()
|
38 |
+
in_database = data.get('results', {}).get('in_database', False)
|
39 |
+
valid = data.get('results', {}).get('valid', False)
|
40 |
+
if in_database and valid:
|
41 |
+
phishing_urls.append(url)
|
42 |
+
else:
|
43 |
+
# Obsługa błędów
|
44 |
+
pass
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"Błąd podczas sprawdzania URL w PhishTank: {e}")
|
47 |
return phishing_urls
|
48 |
|
49 |
def check_urls_with_safe_browsing(urls):
|
|
|
64 |
'threatEntries': [{'url': url} for url in urls]
|
65 |
}
|
66 |
}
|
67 |
+
try:
|
68 |
+
response = requests.post(
|
69 |
+
f'https://safebrowsing.googleapis.com/v4/threatMatches:find?key={api_key}',
|
70 |
+
headers=headers,
|
71 |
+
json=client_body
|
72 |
+
)
|
73 |
+
if response.status_code == 200:
|
74 |
+
data = response.json()
|
75 |
+
matches = data.get('matches', [])
|
76 |
+
unsafe_urls = [match['threat']['url'] for match in matches]
|
77 |
+
else:
|
78 |
+
# Obsługa błędów
|
79 |
+
st.error(f"Błąd podczas komunikacji z Google Safe Browsing API: {response.status_code}")
|
80 |
+
except Exception as e:
|
81 |
+
st.error(f"Błąd podczas sprawdzania URL w Google Safe Browsing: {e}")
|
82 |
return unsafe_urls
|
83 |
|
84 |
def show_analysis(language):
|
|
|
124 |
'safe_browsing_warning': "Następujące URL są oznaczone jako niebezpieczne przez Google Safe Browsing:",
|
125 |
'no_safe_browsing_threats': "Brak zagrożeń według Google Safe Browsing.",
|
126 |
'api_key_missing': "Brak klucza API dla usługi: {}",
|
127 |
+
'analyzing_message': "Analizowanie wiadomości za pomocą modelu AI...",
|
128 |
+
'form_submitted': "Formularz został wysłany.",
|
129 |
+
'starting_phone_analysis': "Rozpoczynam analizę numeru telefonu...",
|
130 |
+
'checking_fake_numbers': "Sprawdzam, czy numer jest w bazie fałszywych numerów...",
|
131 |
+
'performing_simple_checks': "Przeprowadzam proste sprawdzenia heurystyczne...",
|
132 |
+
'starting_url_analysis': "Rozpoczynam analizę URL..."
|
133 |
+
},
|
134 |
+
'German': {
|
135 |
+
'header': "📊 SMS-Analyse",
|
136 |
+
'content': "Hier ist die Schnittstelle zur Analyse von SMS-Nachrichten. Geben Sie unten den Inhalt der Nachricht ein, um zu überprüfen, ob es sich um einen potenziellen Betrug handelt.",
|
137 |
+
'input_placeholder': "Geben Sie den SMS-Inhalt ein...",
|
138 |
+
'analyze_button': "Nachricht analysieren",
|
139 |
+
'result_positive': "Warnung: Die Nachricht könnte ein Betrug sein!",
|
140 |
+
'result_negative': "Die Nachricht scheint sicher zu sein.",
|
141 |
+
'report_button': "Nummer als Betrug melden",
|
142 |
+
'history_header': "Analyseverlauf",
|
143 |
+
'phone_input_label': "Telefonnummer des Absenders (mit Ländervorwahl, z.B. +49123456789):",
|
144 |
+
'additional_info_header': "Zusätzliche Informationen",
|
145 |
+
'frequency_label': "Wie oft erhalten Sie Nachrichten von dieser Nummer?",
|
146 |
+
'frequency_options': ('Zum ersten Mal', 'Gelegentlich', 'Regelmäßig'),
|
147 |
+
'last_received_label': "Wann haben Sie zuletzt eine ähnliche Nachricht erhalten?",
|
148 |
+
'last_received_options': ('Heute', 'In der letzten Woche', 'Im letzten Monat', 'Länger her'),
|
149 |
+
'confidence_level_label': "Wie sicher sind Sie, dass die Nachricht authentisch ist?",
|
150 |
+
'confidence_level_help': "1: Sehr unsicher, 5: Sehr sicher",
|
151 |
+
'unexpected_label': "War der Erhalt dieser Nachricht für Sie unerwartet?",
|
152 |
+
'unexpected_options': ('Ja', 'Nein'),
|
153 |
+
'additional_notes_label': "Zusätzliche Anmerkungen oder Informationen (optional)",
|
154 |
+
'phone_info_header': "Informationen zur Telefonnummer",
|
155 |
+
'country_label': "Land",
|
156 |
+
'operator_label': "Anbieter",
|
157 |
+
'unable_to_retrieve': "Informationen zur Telefonnummer können nicht abgerufen werden.",
|
158 |
+
'red_flags_found': "Folgende potenzielle Warnsignale wurden gefunden:",
|
159 |
+
'remember_caution': "📌 Denken Sie daran: Seien Sie vorsichtig bei Nachrichten von unbekannten Absendern.",
|
160 |
+
'number_reported': "Nummer wurde als Betrug gemeldet.",
|
161 |
+
'number_already_reported': "Nummer wurde bereits als Betrug gemeldet.",
|
162 |
+
'invalid_number': "Die angegebene Telefonnummer ist ungültig.",
|
163 |
+
'failed_to_parse': "Die Telefonnummer konnte nicht verarbeitet werden. Stellen Sie sicher, dass sie im richtigen Format ist.",
|
164 |
+
'url_analysis_header': "🔗 URL-Analyse",
|
165 |
+
'extracted_urls': "Aus der Nachricht extrahierte URLs:",
|
166 |
+
'no_urls_found': "Keine URLs in der Nachricht gefunden.",
|
167 |
+
'checking_urls_phishtank': "Überprüfe URLs mit PhishTank...",
|
168 |
+
'phishtank_warning': "Folgende URLs sind von PhishTank als Phishing markiert:",
|
169 |
+
'no_phishtank_threats': "Keine Phishing-Bedrohungen laut PhishTank.",
|
170 |
+
'checking_urls_safe_browsing': "Überprüfe URLs mit Google Safe Browsing...",
|
171 |
+
'safe_browsing_warning': "Folgende URLs sind von Google Safe Browsing als unsicher markiert:",
|
172 |
+
'no_safe_browsing_threats': "Keine Bedrohungen laut Google Safe Browsing.",
|
173 |
+
'api_key_missing': "API-Schlüssel für Dienst fehlt: {}",
|
174 |
+
'analyzing_message': "Analysiere Nachricht mit AI-Modell...",
|
175 |
+
'form_submitted': "Formular wurde eingereicht.",
|
176 |
+
'starting_phone_analysis': "Beginne Analyse der Telefonnummer...",
|
177 |
+
'checking_fake_numbers': "Überprüfe, ob die Nummer in der Betrugsdatenbank ist...",
|
178 |
+
'performing_simple_checks': "Führe einfache heuristische Prüfungen durch...",
|
179 |
+
'starting_url_analysis': "Beginne URL-Analyse..."
|
180 |
},
|
181 |
+
'English': {
|
182 |
+
'header': "📊 SMS Analysis",
|
183 |
+
'content': "Here is the interface for analyzing SMS messages. Enter the content of the message below to check if it is a potential fraud.",
|
184 |
+
'input_placeholder': "Enter SMS content...",
|
185 |
+
'analyze_button': "Analyze Message",
|
186 |
+
'result_positive': "Warning: The message may be a fraud!",
|
187 |
+
'result_negative': "The message appears to be safe.",
|
188 |
+
'report_button': "Report number as fraud",
|
189 |
+
'history_header': "Analysis History",
|
190 |
+
'phone_input_label': "Sender's phone number (with country code, e.g., +123456789):",
|
191 |
+
'additional_info_header': "Additional Information",
|
192 |
+
'frequency_label': "How often do you receive messages from this number?",
|
193 |
+
'frequency_options': ('First time', 'Occasionally', 'Regularly'),
|
194 |
+
'last_received_label': "When did you last receive a similar message?",
|
195 |
+
'last_received_options': ('Today', 'Within the last week', 'Within the last month', 'Longer ago'),
|
196 |
+
'confidence_level_label': "How confident are you that the message is authentic?",
|
197 |
+
'confidence_level_help': "1: Very unsure, 5: Very confident",
|
198 |
+
'unexpected_label': "Was receiving this message unexpected for you?",
|
199 |
+
'unexpected_options': ('Yes', 'No'),
|
200 |
+
'additional_notes_label': "Additional notes or information (optional)",
|
201 |
+
'phone_info_header': "Phone Number Information",
|
202 |
+
'country_label': "Country",
|
203 |
+
'operator_label': "Operator",
|
204 |
+
'unable_to_retrieve': "Unable to retrieve phone number information.",
|
205 |
+
'red_flags_found': "The following potential red flags were found:",
|
206 |
+
'remember_caution': "📌 Remember: Be cautious with messages from unknown senders.",
|
207 |
+
'number_reported': "Number has been reported as fraud.",
|
208 |
+
'number_already_reported': "Number has already been reported as fraud.",
|
209 |
+
'invalid_number': "The provided phone number is invalid.",
|
210 |
+
'failed_to_parse': "Failed to parse the phone number. Ensure it is in the correct format.",
|
211 |
+
'url_analysis_header': "🔗 URL Analysis",
|
212 |
+
'extracted_urls': "Extracted URLs from the message:",
|
213 |
+
'no_urls_found': "No URLs found in the message.",
|
214 |
+
'checking_urls_phishtank': "Checking URLs with PhishTank...",
|
215 |
+
'phishtank_warning': "The following URLs are flagged as phishing by PhishTank:",
|
216 |
+
'no_phishtank_threats': "No phishing threats detected by PhishTank.",
|
217 |
+
'checking_urls_safe_browsing': "Checking URLs with Google Safe Browsing...",
|
218 |
+
'safe_browsing_warning': "The following URLs are flagged as unsafe by Google Safe Browsing:",
|
219 |
+
'no_safe_browsing_threats': "No threats detected by Google Safe Browsing.",
|
220 |
+
'api_key_missing': "API key missing for service: {}",
|
221 |
+
'analyzing_message': "Analyzing message using AI model...",
|
222 |
+
'form_submitted': "Form has been submitted.",
|
223 |
+
'starting_phone_analysis': "Starting phone number analysis...",
|
224 |
+
'checking_fake_numbers': "Checking if the number is in the fraud database...",
|
225 |
+
'performing_simple_checks': "Performing simple heuristic checks...",
|
226 |
+
'starting_url_analysis': "Starting URL analysis..."
|
227 |
+
}
|
228 |
}
|
229 |
|
230 |
+
translations = page_translations.get(language, page_translations['English'])
|
231 |
|
232 |
st.header(translations['header'])
|
233 |
st.write(translations['content'])
|
|
|
274 |
submitted = st.form_submit_button(translations['analyze_button'])
|
275 |
|
276 |
if submitted:
|
277 |
+
st.write(translations['form_submitted']) # Diagnostyka
|
278 |
if not message.strip():
|
279 |
st.error(translations['input_placeholder'])
|
280 |
elif not phone_number.strip():
|
281 |
st.error(translations['phone_input_label'])
|
282 |
else:
|
283 |
# Informacje o numerze telefonu
|
284 |
+
st.write(translations['starting_phone_analysis']) # Diagnostyka
|
285 |
country, operator = get_phone_info(phone_number)
|
286 |
if country and operator:
|
287 |
st.subheader(translations['phone_info_header'])
|
|
|
291 |
st.write(translations['unable_to_retrieve'])
|
292 |
|
293 |
# Sprawdzenie, czy numer jest w bazie fałszywych numerów
|
294 |
+
st.write(translations['checking_fake_numbers']) # Diagnostyka
|
295 |
if is_fake_number(phone_number):
|
296 |
st.warning(translations['result_positive'])
|
297 |
fraud_detected = True
|
|
|
300 |
fraud_detected = False
|
301 |
|
302 |
# Przeprowadź proste sprawdzenia heurystyczne
|
303 |
+
st.write(translations['performing_simple_checks']) # Diagnostyka
|
304 |
warnings = simple_checks(message, language)
|
305 |
if warnings:
|
306 |
st.warning(translations['red_flags_found'])
|
|
|
321 |
"""
|
322 |
|
323 |
# Analiza URL
|
324 |
+
st.write(translations['starting_url_analysis']) # Diagnostyka
|
325 |
urls = extract_urls(message)
|
326 |
if urls:
|
327 |
st.subheader(translations['url_analysis_header'])
|
|
|
359 |
st.error("API key is missing. Please set the SAMBANOVA_API_KEY environment variable.")
|
360 |
else:
|
361 |
with st.spinner(translations['analyzing_message']):
|
362 |
+
# Upewnij się, że funkcja analyze_message jest zaimplementowana
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
try:
|
364 |
+
analysis_text, risk_text, recommendations_text = analyze_message(
|
365 |
+
message, phone_number, additional_info, api_key, language
|
366 |
+
)
|
367 |
+
st.subheader("📑 " + translations['header'])
|
368 |
+
st.markdown(analysis_text)
|
369 |
+
st.subheader("⚠️ " + translations['red_flags_found'])
|
370 |
+
st.markdown(risk_text)
|
371 |
+
st.subheader("✅ " + translations['remember_caution'])
|
372 |
+
st.markdown(recommendations_text)
|
373 |
+
|
374 |
+
# Aktualizacja statystyk na podstawie oceny ryzyka
|
375 |
+
try:
|
376 |
+
match = re.search(r'(\d+)/10', risk_text)
|
377 |
+
fraud_detected = int(match.group(1)) >= 7 if match else False
|
378 |
+
update_stats(fraud_detected=fraud_detected)
|
379 |
+
except Exception as e:
|
380 |
+
st.error(f"Błąd podczas aktualizacji statystyk: {e}")
|
381 |
+
fraud_detected = False
|
382 |
|
383 |
+
# Dodanie analizy do historii
|
384 |
+
add_to_history(message, phone_number, analysis_text, risk_text, recommendations_text)
|
385 |
+
except Exception as e:
|
386 |
+
st.error(f"Błąd podczas analizy wiadomości: {e}")
|
387 |
|
388 |
st.info(translations['remember_caution'])
|
389 |
|