Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import firebase_admin
|
2 |
+
from firebase_admin import credentials
|
3 |
+
from firebase_admin import db
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
# Fetch the credential JSON file from your Firebase project settings
|
8 |
+
# and upload it to Google Colab, then provide the path here
|
9 |
+
cred = credentials.Certificate("/cid1234-firebase-adminsdk-o0jz2-ddc0afe71d.json")
|
10 |
+
|
11 |
+
# Initialize the app with the credentials and your database URL
|
12 |
+
firebase_admin.initialize_app(cred, {
|
13 |
+
'databaseURL': 'https://cid1234-default-rtdb.firebaseio.com/'
|
14 |
+
})
|
15 |
+
|
16 |
+
# Reference to your Firebase database
|
17 |
+
ref = db.reference('/')
|
18 |
+
|
19 |
+
# Listen for changes in the database in real-time
|
20 |
+
def on_change(event):
|
21 |
+
st.write('Data changed:', event.path, event.data)
|
22 |
+
|
23 |
+
ref.listen(on_change)
|
24 |
+
|
25 |
+
|
26 |
+
import requests
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
def get_coordinates(location):
|
33 |
+
"""
|
34 |
+
Get latitude and longitude of a location using OpenStreetMap Nominatim API
|
35 |
+
"""
|
36 |
+
# URL for OpenStreetMap Nominatim API
|
37 |
+
url = f'https://nominatim.openstreetmap.org/search?format=json&q={location}'
|
38 |
+
|
39 |
+
# Send request to the API
|
40 |
+
response = requests.get(url)
|
41 |
+
|
42 |
+
# Parse JSON response
|
43 |
+
data = response.json()
|
44 |
+
|
45 |
+
# Check if response is not empty
|
46 |
+
if data:
|
47 |
+
# Extract latitude and longitude from the first result
|
48 |
+
lat = float(data[0]['lat'])
|
49 |
+
lon = float(data[0]['lon'])
|
50 |
+
return lat, lon
|
51 |
+
else:
|
52 |
+
st.write("No results found for the location.")
|
53 |
+
return None
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
# Example usage
|
60 |
+
location = input("Enter Location: ")
|
61 |
+
latitude, longitude = get_coordinates(location)
|
62 |
+
if latitude and longitude:
|
63 |
+
st.write(f"Latitude: {latitude}, Longitude: {longitude}")
|
64 |
+
else:
|
65 |
+
st.write("Failed to retrieve coordinates for the location.")
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
crimes = pd.DataFrame(columns=['Name', 'Lat', 'Lon', 'Intensity'])
|
71 |
+
|
72 |
+
|
73 |
+
query = ""
|
74 |
+
while(query != 'N'):
|
75 |
+
location = input("Enter Location: ")
|
76 |
+
crime = input("Enter Crime: ")
|
77 |
+
intensity = int(input("Enter Intensity: "))
|
78 |
+
latitude, longitude = get_coordinates(location)
|
79 |
+
data = [{'Name': crime, 'Lat': latitude, 'Lon': longitude, 'Intensity': intensity}]
|
80 |
+
query = input("Enter Y to continue and N to stop: ")
|
81 |
+
crimes = pd.concat([crimes, pd.DataFrame(data)], ignore_index=True)
|
82 |
+
|
83 |
+
|
84 |
+
st.write(crimes)
|