Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
import folium
|
| 5 |
+
from folium import plugins
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client with your API key
|
| 10 |
+
client = Groq(api_key="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")
|
| 11 |
+
|
| 12 |
+
# Function to get AI response (Groq model)
|
| 13 |
+
def get_response(user_input):
|
| 14 |
+
"""Get response from Groq AI model."""
|
| 15 |
+
if 'messages' not in gradio_state:
|
| 16 |
+
gradio_state['messages'] = []
|
| 17 |
+
|
| 18 |
+
gradio_state['messages'].append({"role": "user", "content": user_input})
|
| 19 |
+
|
| 20 |
+
# Call Groq API to get the AI's response
|
| 21 |
+
chat_completion = client.chat.completions.create(
|
| 22 |
+
messages=gradio_state['messages'],
|
| 23 |
+
model="llama3-8b-8192" # Specify model you want to use from Groq
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
ai_message = chat_completion.choices[0].message.content
|
| 27 |
+
gradio_state['messages'].append({"role": "assistant", "content": ai_message})
|
| 28 |
+
|
| 29 |
+
return ai_message
|
| 30 |
+
|
| 31 |
+
# Emergency call function
|
| 32 |
+
def emergency_call():
|
| 33 |
+
time.sleep(2) # Simulating a short delay
|
| 34 |
+
return "Emergency services have been contacted. Help is on the way!"
|
| 35 |
+
|
| 36 |
+
# Dangerous Area Map function
|
| 37 |
+
def dangerous_area_map():
|
| 38 |
+
data = {
|
| 39 |
+
'latitude': [40.7128, 34.0522, 51.5074, 48.8566, 35.6762],
|
| 40 |
+
'longitude': [-74.0060, -118.2437, -0.1278, 2.3522, 139.6503],
|
| 41 |
+
'area': ['New York', 'Los Angeles', 'London', 'Paris', 'Tokyo'],
|
| 42 |
+
'danger_level': ['High', 'Medium', 'Low', 'High', 'Medium']
|
| 43 |
+
}
|
| 44 |
+
df = pd.DataFrame(data)
|
| 45 |
+
map_center = [df['latitude'].mean(), df['longitude'].mean()]
|
| 46 |
+
m = folium.Map(location=map_center, zoom_start=2)
|
| 47 |
+
|
| 48 |
+
def get_color(danger_level):
|
| 49 |
+
if danger_level == 'High':
|
| 50 |
+
return 'red'
|
| 51 |
+
elif danger_level == 'Medium':
|
| 52 |
+
return 'orange'
|
| 53 |
+
else:
|
| 54 |
+
return 'green'
|
| 55 |
+
|
| 56 |
+
for index, row in df.iterrows():
|
| 57 |
+
danger_color = get_color(row['danger_level'])
|
| 58 |
+
folium.CircleMarker(
|
| 59 |
+
location=[row['latitude'], row['longitude']],
|
| 60 |
+
radius=10,
|
| 61 |
+
color=danger_color,
|
| 62 |
+
fill=True,
|
| 63 |
+
fill_color=danger_color,
|
| 64 |
+
fill_opacity=0.7,
|
| 65 |
+
popup=f"Area: {row['area']}<br> Danger Level: {row['danger_level']}"
|
| 66 |
+
).add_to(m)
|
| 67 |
+
|
| 68 |
+
heat_data = [[row['latitude'], row['longitude']] for index, row in df.iterrows()]
|
| 69 |
+
plugins.HeatMap(heat_data).add_to(m)
|
| 70 |
+
|
| 71 |
+
map_html = m._repr_html_()
|
| 72 |
+
return map_html
|
| 73 |
+
|
| 74 |
+
# ORS Route function
|
| 75 |
+
def ors_route(start_lat, start_lon, end_lat, end_lon):
|
| 76 |
+
api_key = '5b3ce3597851110001cf6248678e77a7fc474afbbb5ec203d721079c'
|
| 77 |
+
start_point = f'{start_lon},{start_lat}' # ORS expects lon, lat
|
| 78 |
+
end_point = f'{end_lon},{end_lat}'
|
| 79 |
+
|
| 80 |
+
# API request to OpenRouteService
|
| 81 |
+
url = f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={start_point}&end={end_point}'
|
| 82 |
+
response = requests.get(url)
|
| 83 |
+
|
| 84 |
+
if response.status_code == 200:
|
| 85 |
+
data = response.json()
|
| 86 |
+
# Extract the route information
|
| 87 |
+
route = data['features'][0]['geometry']['coordinates']
|
| 88 |
+
route_map = folium.Map(
|