ScamDetector / pages /Statistics.py
rafaldembski's picture
Update pages/Statistics.py
faeea2e verified
raw
history blame
7.3 kB
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json
import re
from datetime import datetime
# Definiowanie t艂umacze艅 dla zak艂adki "Statystyki"
page_translations = {
'Polish': {
'page_title': "馃搳 Statystyki",
'page_icon': "馃搱",
'header': "馃搳 Statystyki Aplikacji",
'description': "Poni偶ej znajduj膮 si臋 statystyki analizy wiadomo艣ci w aplikacji.",
'total_analyses': "Liczba przeanalizowanych wiadomo艣ci",
'total_frauds_detected': "Wykryte oszustwa",
'fraud_percentage': "Procent oszustw",
'history_title': "Historia analizowanych wiadomo艣ci",
'frauds_over_time': "Liczba wykrytych oszustw w czasie",
'risk_distribution': "Rozk艂ad ocen ryzyka oszustwa",
'fraud_country_distribution': "Rozk艂ad oszustw wed艂ug kraj贸w",
'heatmap_title': "Mapa ciep艂a oszustw w czasie",
'fraud_vs_nonfraud': "Procentowy podzia艂: Oszustwa vs Bezpieczne",
'no_data': "Brak dost臋pnych danych do wy艣wietlenia."
},
'German': {
'page_title': "馃搳 Statistiken",
'page_icon': "馃搱",
'header': "馃搳 Anwendungsstatistiken",
'description': "Nachfolgend finden Sie die Statistiken zur Nachrichtenanalyse in der Anwendung.",
'total_analyses': "Anzahl der analysierten Nachrichten",
'total_frauds_detected': "Erkannte Betr眉gereien",
'fraud_percentage': "Betrugsprozentsatz",
'history_title': "Analyseverlauf der Nachrichten",
'frauds_over_time': "Anzahl der erkannten Betr眉gereien im Laufe der Zeit",
'risk_distribution': "Verteilung der Betrugsrisikobewertungen",
'fraud_country_distribution': "Betrug nach L盲ndern",
'heatmap_title': "Heatmap der Betr眉gereien im Laufe der Zeit",
'fraud_vs_nonfraud': "Prozentanteil: Betrug vs Sichere Nachrichten",
'no_data': "Keine Daten zur Anzeige verf眉gbar."
},
'English': {
'page_title': "馃搳 Statistics",
'page_icon': "馃搱",
'header': "馃搳 Application Statistics",
'description': "Below are the statistics of message analysis in the app.",
'total_analyses': "Total Messages Analyzed",
'total_frauds_detected': "Frauds Detected",
'fraud_percentage': "Fraud Percentage",
'history_title': "History of Analyzed Messages",
'frauds_over_time': "Number of Detected Frauds Over Time",
'risk_distribution': "Distribution of Fraud Risk Scores",
'fraud_country_distribution': "Fraud Distribution by Countries",
'heatmap_title': "Fraud Heatmap Over Time",
'fraud_vs_nonfraud': "Fraud vs Safe Messages Percentage",
'no_data': "No data available to display."
}
}
# Funkcja do pobierania statystyk
def get_stats():
stats_file = 'stats.json'
try:
with open(stats_file, 'r') as f:
stats = json.load(f)
return stats
except (json.JSONDecodeError, FileNotFoundError):
return {"total_analyses": 0, "total_frauds_detected": 0}
# Funkcja do pobierania historii analiz
def get_history():
history_file = 'history.json'
try:
with open(history_file, 'r') as f:
history = json.load(f)
return history
except (json.JSONDecodeError, FileNotFoundError):
return []
# G艂贸wna funkcja zak艂adki "Statystyki"
def main(language):
translations = page_translations[language]
# Pobieranie danych z plik贸w
stats = get_stats()
history = get_history()
# Kluczowe metryki
total_analyses = stats["total_analyses"]
total_frauds_detected = stats["total_frauds_detected"]
# Wy艣wietlenie metryk
st.title(translations['header'])
st.markdown(translations['description'])
col1, col2, col3 = st.columns(3)
col1.metric(label=translations['total_analyses'], value=total_analyses)
col2.metric(label=translations['total_frauds_detected'], value=total_frauds_detected)
# Obs艂uga dzielenia przez zero
if total_analyses > 0:
fraud_percentage = (total_frauds_detected / total_analyses) * 100
else:
fraud_percentage = 0 # Ustawienie na 0% w przypadku braku analiz
col3.metric(label=translations['fraud_percentage'], value=f"{fraud_percentage:.2f}%")
# Wizualizacja procentowego podzia艂u oszustw
fraud_data = [total_frauds_detected, total_analyses - total_frauds_detected]
fraud_labels = ['Fraud', 'Non-Fraud']
fig_fraud_pie = go.Figure(data=[go.Pie(labels=fraud_labels, values=fraud_data, hole=.3, marker_colors=['#FF6347', '#4682B4'])])
fig_fraud_pie.update_layout(title_text=translations['fraud_vs_nonfraud'])
st.plotly_chart(fig_fraud_pie)
# Wy艣wietlenie historii analiz w tabeli
if history:
st.markdown(f"### {translations['history_title']}")
df_history = pd.DataFrame(history)
# Konwersja timestamp na dat臋 i dodanie kolumny 'date'
df_history['timestamp'] = pd.to_datetime(df_history['timestamp'])
df_history['date'] = df_history['timestamp'].dt.date # Dodanie kolumny date
# Wy艣wietlenie tabeli historii
st.dataframe(df_history[['timestamp', 'phone_number', 'risk_assessment']], height=300)
# Wykres ko艂owy dla ocen ryzyka
st.markdown(f"### {translations['risk_distribution']}")
# U偶ycie wyra偶enia regularnego do wyodr臋bnienia liczby z tekstu oceny ryzyka
def extract_risk_score(risk_assessment):
match = re.search(r'(\d+)/10', risk_assessment)
return int(match.group(1)) if match else 0
df_history['risk_score'] = df_history['risk_assessment'].apply(extract_risk_score)
risk_data = df_history['risk_score'].value_counts().sort_index()
risk_labels = [f'Risk {i}/10' for i in risk_data.index]
fig_risk_pie = go.Figure(data=[go.Pie(labels=risk_labels, values=risk_data, hole=.3, marker_colors=px.colors.sequential.RdBu)])
fig_risk_pie.update_layout(title_text=translations['risk_distribution'])
st.plotly_chart(fig_risk_pie)
# Wizualizacja mapy ciep艂a (heatmap)
st.markdown(f"### {translations['heatmap_title']}")
heatmap_data = df_history.groupby('date').size().reset_index(name='count')
fig_heatmap = px.density_heatmap(heatmap_data, x='date', y='count', nbinsx=20, nbinsy=20, title=translations['heatmap_title'], color_continuous_scale='Blues')
st.plotly_chart(fig_heatmap)
# Dodanie mapy Europy (wymaga danych kraj贸w dla numer贸w telefon贸w)
st.markdown(f"### {translations['fraud_country_distribution']}")
if 'country' in df_history.columns:
country_data = df_history.groupby('country').size().reset_index(name='counts')
fig_map = px.choropleth(country_data, locations='country', locationmode='country names', color='counts',
title=translations['fraud_country_distribution'], color_continuous_scale=px.colors.sequential.Plasma)
st.plotly_chart(fig_map)
else:
st.info("Brak danych o krajach numer贸w telefon贸w.")
else:
st.info(translations['no_data'])
# Nie dodawaj "if __name__ == '__main__':" w podstronach