Inspiration

In response to the pressing issue of insecurity plaguing Mexico City, exacerbated by findings that a significant portion of the population feels unsafe in their daily commutes, our mission is to develop a safe route software solution. Despite being an essential hub of economic, social, and cultural development in Mexico, the city has faced increasing insecurity, as evidenced by a national public security survey revealing high levels of perceived danger among its residents. Leveraging the increasing adoption of technology and widespread internet accessibility in Mexico City, our goal is to utilize pathfinding algorithms to create a program that offers safe navigation routes to users, addressing a critical need for safety in daily commuting.

Recognizing the limitations of existing navigation apps, which often prioritize time optimization over safety and may inadvertently guide users through dangerous areas, our aim is to provide a solution that prioritizes safety without compromising efficiency. By replacing distance metrics in route calculations with crime statistics provided by the government of Mexico City, we aim to predict safer routes and mitigate the risks associated with navigating through insecure areas. Through the application of Dijkstra's algorithm and careful parsing of crime data, we seek to offer users a reliable tool for navigating the city safely, thereby addressing a significant societal concern and contributing to the overall well-being of Mexico City's population.

What it does

SafeNav is a platform that allows users to discover the safest route in Mexico City. Our aim is to empower the citizens of the city alongside the tourists to commute safely, knowing the risks and measures they should take during their trip. All of this is done through our Streamlit website, that enables the user to either write or speak their desired path. Once the path is specified an LLM model is used to process the request and handle it over to the pathfinding algorithm that by using Dijkstra’s algorithm iterates over the custom multidirected-weighted graph to determine the best path alongside the amount of risk of such commute. Finally, the results are printed for the user, showing the best path to follow. Due to time constraints this was achieved on a big geographic scale using city sectors, but it could also be done on a more specific approach by using city quadrants. Our iteration is designed to work as a proof of concept so that it inspires more people to tackle the same problems and develop more in-depth solutions.

How we built it

We opted for Streamlit as our development framework primarily due to its robust capabilities in data visualization, which align perfectly with our goal of delivering visual insights to end-users while maintaining an intuitive interface. In conjunction with this, we used the OpenAI API to create a virtual assistant, empowering users with seamless file retrieval and filtered responses tailored to their specific needs. To enhance efficiency, we integrated the Google Sheets API for data retrieval. Furthermore, we incorporated streamlit-mic-recorder to introduce speech functionality within the application, adding another layer of accessibility and user engagement.

Below lies the project structure

streamlit_app 
├─ home.py
├─ .streamlit
│   └─ secrets.toml
│   └─ gcloud.json
├─ algorithms
├─ api
├─ assets
│  └─ files
│  └─ images
├─ pages
│  └─ report_bug.py
│  └─ get_started.py
│  └─ route.py
└─ requirements.txt

Challenges we ran into

So, at first, we were kind of in the dark about which framework to use. Mainly because we wanted to implement some data structures and processing using Python. Then, my teammate suggested Streamlit, and man, it just clicked! But, we had some issues with OpenAI credits, so we had to optimize our prompt to manage out with the fewest tokens possible while still keeping our algorithm on point. After some tinkering, we nailed it. On top of that, we had to improve the runtime for our Speech-to-Text feature by tapping into the OpenAI assistant via API and crunching the results using Dijkstra's algorithm.

How did we implement Data Structures

Overview

The data structure we implemented was a graph, specifically, a multidirected weighted graph that represents Mexico City divided in 72 sectors that are represented as nodes.

Design

The first step was to create a graph of Mexico City that shows the nodes as individual sectors that was achieved by using a Geojson map of the city, which was passed through the method Queen.from_dataframe that created an undirected unweighted graph of the city by determining the connections between nodes as polygons with borders touching.
Mexico City divided in sectors

Data

To determine a sector’s security our approach was to gather statistics about the number of crimes committed in the city, which was achieved through the website hoyodecrimen.com. This website gathers open government data, cleans it, and makes it available through an API. The data gathered was the number of crimes committed last year that are indexed in 20 categories. For each sector, the total number of crimes was considered its data criminality score.

Dijkstra

Dijkstra requires a weighted graph to work on, for our implementation this was done by iterating over all the nodes of the original unweighted graph and consulting the criminality index data frame, assigning such indexes as the weights to arrive at a certain node. This implementation makes a clever twist to Dijkstra’s algorithm as it is originally used for calculating the shortest path, but instead this implementation calculates the safest path. We encountered problems with the Networkx’s shortest path function as it sometimes retrieved faulty paths, to fix this we implemented a Dijkstra algorithm from Geeks for Geeks and slightly modified it to show the total weight of the path traveled along with the nodes required to pass through. This results in a list of nodes traversed, which is iterated again to print the names of the actual sectors, thus creating a safer Waze.
Mexico City represented as nodes

How does SafeNav uses Speech to Text

Initially, we contemplated integrating OpenAI's tool, Whisper, for Speech to Text functionality. However, upon further consideration, we opted to explore alternative solutions that could be directly implemented within Streamlit. The following steps outline the process behind the method implemented:

  1. When called, speech_to_text, it creates a new button in Streamlit app that the user can click to start recording audio.
  2. Once the user clicks the button, the browser asks for permission to access the microphone. If the user grants permission, the recording starts.
  3. The user can stop the recording by clicking the button again. The audio is then sent to the server running the Streamlit app.
  4. On the server, the audio is converted to the FLAC format, which is required by the Google Speech-to-Text API.
  5. The FLAC audio is sent to the Google Speech-to-Text API, which transcribes the audio to text.
  6. The transcribed text is returned by the speech_to_text function.

How we hacked with ChatGPT API

We developed an interactive assistant utilizing the ChatGPT API, tailored to locate entities in Mexico City stored in a .txt file along with associated node numbers. (See example below)

0   ABASTO-REFORMA
1   ALPES
2   ANGEL-ZONA ROSA
3   ARAGON
4   ARENAL
5   ASTURIAS

This assistant is accessible remotely from our codebase. It operates by receiving predefined system instructions that we initially established. Subsequently, it utilizes the most recent user input, whether it's in the form of speech or text, as the input prompt to generate our desired result. This process results in an output resembling the following:

{ "start_node": 68, "end_node": 10 }

This filtered data is what we use in our Pathfinder() class to provide the most optimal and safest route

Accomplishments that we're proud of

We are proud in the implementation of our solution, which is now readily available for our classmates and family to utilize starting today. We believe this tool will be invaluable, particularly for foreigners, as it provides access to the safest and most optimal routes for travel. Our solution not only addresses a pressing need but also showcases our commitment to developing practical and impactful solutions for our community.

How did we used Github Copilot using our Student Account

We mostly used Copilot to ensure that all the secrets of our app where protected correctly, specially when using a new framework as Streamlit, we discovered several things, such as the secrets.toml file.

Github Copilot Implementation

What we learned

We learned how to fully integrate an LLM model into a live application using only python and Streamlit, as we had not done an integration like that before. Also, we discovered how to create constant updates to a database, in this case using google sheets, to create a backlog of the GPT outputs. This backlog is crucial as with the rise of LLM chatbot integrations it is crucial to have the output data stored to run security and quality checks, ensuring a great experience for customers.

What's next for test

It would be interesting to expand the pathfinding capabilities by using a quadrant division system instead of the proposed sector alongside a gps integration as that would enable the user to only say their destination. Finally, a future iteration would be nice if it could have voice assisted instructions that dictate the path of the user through the city. We believe that this iteration unveils a steppingstone for future hackers to develop new approaches and discover new ways to calculate safer routes in their cities!

Built With

Share this project:

Updates