-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (55 loc) · 2.02 KB
/
Copy pathapp.py
File metadata and controls
67 lines (55 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# app.py
from flask import Flask, render_template, request
import pandas as pd
import geopandas as gpd
import folium
from utils import summarize_cluster
from chat import get_chat_response
app = Flask(__name__)
df = pd.read_csv('data/clustered_dataset.csv')
@app.route('/')
def index():
return render_template('map.html') # Render the template with a loading message
@app.route('/map_html')
def map_html():
gdf = gpd.GeoDataFrame(
df,
geometry=gpd.points_from_xy(df.LONGITUDE, df.LATITUDE),
crs='EPSG:4326'
)
counts = gdf.groupby('cluster').size()
valid_clusters = counts[counts > 2000].index
filtered = gdf[gdf['cluster'].isin(valid_clusters)]
dissolved = filtered.dissolve(by='cluster')
dissolved_wgs = dissolved.to_crs(epsg=4326)
centroids = dissolved_wgs.geometry.centroid
m = folium.Map(
location=[df['LATITUDE'].mean(), df['LONGITUDE'].mean()],
zoom_start=11
)
for cluster_id, centroid in centroids.items():
if cluster_id == -1:
continue
count = counts[cluster_id]
popup_html = f'<button onclick="window.parent.clusterClicked({cluster_id})">View Info</button><br>Cluster ID: {cluster_id}' # updated callback
folium.CircleMarker(
location=[centroid.y, centroid.x],
radius=3 + (count**0.5) * 0.02,
color='red',
fill=True,
fill_opacity=0.6,
popup=popup_html # modified marker to include popup
).add_to(m)
return m._repr_html_()
@app.route('/ai_response/<int:cluster_id>')
def ai_response(cluster_id):
stats = summarize_cluster(df, cluster_id)
response = get_chat_response(stats)
return {'cluster_id': cluster_id, 'stats': stats, 'response': response}
@app.route('/followup', methods=['POST'])
def followup():
user_message = request.get_json().get('user_message', '')
response = get_chat_response(user_message=user_message)
return {'response': response}
if __name__ == '__main__':
app.run(debug=True, port=5001)