Spaces:
Running
Running
Update pages/Statistics.py
Browse files- pages/Statistics.py +106 -59
pages/Statistics.py
CHANGED
@@ -4,8 +4,51 @@ import plotly.express as px
|
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Funkcja do pobierania statystyk
|
11 |
def get_stats():
|
@@ -27,60 +70,64 @@ def get_history():
|
|
27 |
except (json.JSONDecodeError, FileNotFoundError):
|
28 |
return []
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
else
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
4 |
import json
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
# Definiowanie t艂umacze艅 dla zak艂adki "Statystyki"
|
8 |
+
page_translations = {
|
9 |
+
'Polish': {
|
10 |
+
'page_title': "馃搳 Statystyki",
|
11 |
+
'page_icon': "馃搱",
|
12 |
+
'header': "馃搳 Statystyki Aplikacji",
|
13 |
+
'description': "Poni偶ej znajduj膮 si臋 statystyki analizy wiadomo艣ci w aplikacji.",
|
14 |
+
'total_analyses': "Liczba przeanalizowanych wiadomo艣ci",
|
15 |
+
'total_frauds_detected': "Wykryte oszustwa",
|
16 |
+
'fraud_percentage': "Procent oszustw",
|
17 |
+
'history_title': "Historia analizowanych wiadomo艣ci",
|
18 |
+
'analyses_over_time': "Liczba analizowanych wiadomo艣ci w czasie",
|
19 |
+
'frauds_over_time': "Liczba wykrytych oszustw w czasie",
|
20 |
+
'risk_distribution': "Rozk艂ad ocen ryzyka oszustwa",
|
21 |
+
'no_data': "Brak dost臋pnych danych do wy艣wietlenia."
|
22 |
+
},
|
23 |
+
'German': {
|
24 |
+
'page_title': "馃搳 Statistiken",
|
25 |
+
'page_icon': "馃搱",
|
26 |
+
'header': "馃搳 Anwendungsstatistiken",
|
27 |
+
'description': "Nachfolgend finden Sie die Statistiken zur Nachrichtenanalyse in der Anwendung.",
|
28 |
+
'total_analyses': "Anzahl der analysierten Nachrichten",
|
29 |
+
'total_frauds_detected': "Erkannte Betr眉gereien",
|
30 |
+
'fraud_percentage': "Betrugsprozentsatz",
|
31 |
+
'history_title': "Analyseverlauf der Nachrichten",
|
32 |
+
'analyses_over_time': "Anzahl der analysierten Nachrichten im Laufe der Zeit",
|
33 |
+
'frauds_over_time': "Anzahl der erkannten Betr眉gereien im Laufe der Zeit",
|
34 |
+
'risk_distribution': "Verteilung der Betrugsrisikobewertungen",
|
35 |
+
'no_data': "Keine Daten zur Anzeige verf眉gbar."
|
36 |
+
},
|
37 |
+
'English': {
|
38 |
+
'page_title': "馃搳 Statistics",
|
39 |
+
'page_icon': "馃搱",
|
40 |
+
'header': "馃搳 Application Statistics",
|
41 |
+
'description': "Below are the statistics of message analysis in the app.",
|
42 |
+
'total_analyses': "Total Messages Analyzed",
|
43 |
+
'total_frauds_detected': "Frauds Detected",
|
44 |
+
'fraud_percentage': "Fraud Percentage",
|
45 |
+
'history_title': "History of Analyzed Messages",
|
46 |
+
'analyses_over_time': "Number of Analyzed Messages Over Time",
|
47 |
+
'frauds_over_time': "Number of Detected Frauds Over Time",
|
48 |
+
'risk_distribution': "Distribution of Fraud Risk Scores",
|
49 |
+
'no_data': "No data available to display."
|
50 |
+
}
|
51 |
+
}
|
52 |
|
53 |
# Funkcja do pobierania statystyk
|
54 |
def get_stats():
|
|
|
70 |
except (json.JSONDecodeError, FileNotFoundError):
|
71 |
return []
|
72 |
|
73 |
+
# G艂贸wna funkcja zak艂adki "Statystyki"
|
74 |
+
def main(language):
|
75 |
+
translations = page_translations[language]
|
76 |
+
|
77 |
+
# Pobieranie danych z plik贸w
|
78 |
+
stats = get_stats()
|
79 |
+
history = get_history()
|
80 |
+
|
81 |
+
# Kluczowe metryki
|
82 |
+
total_analyses = stats["total_analyses"]
|
83 |
+
total_frauds_detected = stats["total_frauds_detected"]
|
84 |
+
|
85 |
+
# Wy艣wietlenie metryk
|
86 |
+
st.title(translations['header'])
|
87 |
+
st.markdown(translations['description'])
|
88 |
+
|
89 |
+
col1, col2, col3 = st.columns(3)
|
90 |
+
col1.metric(label=translations['total_analyses'], value=total_analyses)
|
91 |
+
col2.metric(label=translations['total_frauds_detected'], value=total_frauds_detected)
|
92 |
+
|
93 |
+
# Obs艂uga dzielenia przez zero
|
94 |
+
if total_analyses > 0:
|
95 |
+
fraud_percentage = (total_frauds_detected / total_analyses) * 100
|
96 |
+
else:
|
97 |
+
fraud_percentage = 0 # Ustawienie na 0% w przypadku braku analiz
|
98 |
+
|
99 |
+
col3.metric(label=translations['fraud_percentage'], value=f"{fraud_percentage:.2f}%")
|
100 |
+
|
101 |
+
# Wy艣wietlenie historii analiz w tabeli
|
102 |
+
if history:
|
103 |
+
st.markdown(f"### {translations['history_title']}")
|
104 |
+
df_history = pd.DataFrame(history)
|
105 |
+
|
106 |
+
# Formatowanie daty
|
107 |
+
df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
|
108 |
+
|
109 |
+
# Wy艣wietlenie tabeli historii
|
110 |
+
st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
|
111 |
+
|
112 |
+
# Wizualizacja liczby analiz w czasie
|
113 |
+
st.markdown(f"### {translations['analyses_over_time']}")
|
114 |
+
df_history['date'] = df_history['timestamp'].dt.date
|
115 |
+
analyses_over_time = df_history.groupby('date').size().reset_index(name='counts')
|
116 |
+
fig_analyses_over_time = px.line(analyses_over_time, x='date', y='counts', title=translations['analyses_over_time'])
|
117 |
+
st.plotly_chart(fig_analyses_over_time)
|
118 |
+
|
119 |
+
# Wizualizacja oszustw w czasie
|
120 |
+
st.markdown(f"### {translations['frauds_over_time']}")
|
121 |
+
df_history['fraud_detected'] = df_history['risk_assessment'].apply(lambda x: 'Detected Fraud' if '10/10' in x else 'No Fraud')
|
122 |
+
frauds_over_time = df_history.groupby(['date', 'fraud_detected']).size().reset_index(name='counts')
|
123 |
+
fig_frauds_over_time = px.bar(frauds_over_time, x='date', y='counts', color='fraud_detected', title=translations['frauds_over_time'])
|
124 |
+
st.plotly_chart(fig_frauds_over_time)
|
125 |
+
else:
|
126 |
+
st.info(translations['no_data'])
|
127 |
+
|
128 |
+
# Wizualizacje ryzyka oszustwa (je艣li s膮 dost臋pne dane)
|
129 |
+
if history:
|
130 |
+
st.markdown(f"### {translations['risk_distribution']}")
|
131 |
+
df_history['risk_score'] = df_history['risk_assessment'].apply(lambda x: int(x.split('/')[0]) if '/' in x else 0)
|
132 |
+
fig_risk_distribution = px.histogram(df_history, x='risk_score', nbins=10, title=translations['risk_distribution'])
|
133 |
+
st.plotly_chart(fig_risk_distribution)
|