import os import pandas as pd from azure.storage.blob import BlobServiceClient import json import datetime from gradio_calendar import Calendar import plotly.express as px import gradio as gr import plotly.graph_objects as go import folium from folium.plugins import FloatImage import base64 from folium.plugins import HeatMap import matplotlib.pyplot as plt import seaborn as sns from dotenv import load_dotenv load_dotenv() DL_CONNECTION_STRING = os.getenv("DL_CONNECTION_STRING") def filter_records(cat): blob_service_client = BlobServiceClient.from_connection_string(DL_CONNECTION_STRING) dl_loc = "/gold/omni/categories/classified_crime_categories.csv" blob_client = blob_service_client.get_blob_client(container="stg", blob=dl_loc) download_stream = blob_client.download_blob() csv_data = download_stream.readall().decode('utf-8-sig') from io import StringIO df = pd.read_csv(StringIO(csv_data)) print("df shape ",df.shape) null_value = 0.0 DEFAULT_LOCATIONS = { "colorado springs": {"DEFAULT_LAT": 38.8339, "DEFAULT_LON": -104.8214}, "tampa": {"DEFAULT_LAT": 27.9517, "DEFAULT_LON": -82.4588}, "seattle": {"DEFAULT_LAT": 47.6061, "DEFAULT_LON": -122.3328}, "las vegas": {"DEFAULT_LAT": 36.1716, "DEFAULT_LON": -115.1391}, "orlando": {"DEFAULT_LAT": 28.5384, "DEFAULT_LON": -81.3789} } df = df[df['city'] == 'orlando'] df['lat'] = df['lat'].astype(float) df['lon'] = df['lon'].astype(float) df = df[(df['lat'] != null_value)] df = df[(df['lon'] != null_value)] df['category1'] = df['type'].apply(lambda x: 'crime' if x not in ['political_demographics', 'weather'] else x) df = df[df['category1'] == 'crime'] df = df[df['category'].isin(cat) ] if df.empty: return None, None, None, None, None, None, None else: source = df['source'].unique() print(f"Source : {source}") median_lat = df['lat'].astype(float).median() median_lon = df['lon'].astype(float).median() map1 = folium.Map(location=[median_lat, median_lon], zoom_start=12) num_rows = df.shape[0] for i in range(num_rows): lat = df.iloc[i]['lat'] lon = df.iloc[i]['lon'] folium.Marker( location=[lat, lon]).add_to(map1) heat_data = [[row['lat'], row['lon']] for index, row in df.iterrows()] map2 = folium.Map(location=[median_lat, median_lon], zoom_start=12) HeatMap(heat_data).add_to(map2) crime_counts = df['category'].value_counts() df.drop(['Unnamed: 0', 'utc_date', 'type', 'lat', 'lon', 'address', 'hash_id', 'retrieved_date', 'utc_retrieved', 'category1'], axis=1, inplace=True) df1 = pd.DataFrame(crime_counts).reset_index() df1.columns = ['crime_type', 'count'] df1 = df1.sort_values(by='count', ascending=False) plt.figure(figsize=(10, 6)) sns.barplot(x=df1["crime_type"], y= df1["count"],palette="viridis") plt.title("Count of Crimes by Type") plt.xlabel("Crime Type") plt.ylabel("Count") plt.xticks(rotation=45) plt.tight_layout() bar_chart_path1 = "bar_chart1.png" plt.savefig(bar_chart_path1) plt.close() plt.figure(figsize=(8, 8)) plt.pie(df1["count"], labels=df1["crime_type"], autopct='%1.1f%%', startangle=140, colors=sns.color_palette("viridis", len(crime_counts))) plt.title("Proportion of Crimes by Type") pie_chart_path1 = "pie_chart1.png" plt.savefig(pie_chart_path1) plt.close() map1_html = map1._repr_html_() map2_html = map2._repr_html_() return map1_html, map2_html,df,source,bar_chart_path1,pie_chart_path1 categories = ['Civil Unrest', 'Cyber', 'Economic', 'Extremism', 'Hate-Discrimination', 'Health & Safety', 'Infrastructure/Transportation', 'Government', 'Information Infrastructure / Technology', 'Natural Threat ', 'Non-Violent Crime', 'Organized Crime/Gang', 'Other', 'Physical Security', 'Terrorism/War', 'Violent Crime'] with gr.Blocks() as demo: with gr.Column(): with gr.Row(): gr.HTML("""