-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.py
243 lines (210 loc) · 7.6 KB
/
app2.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import requests
from collections import defaultdict
import streamlit as st
import folium
from streamlit_folium import folium_static
import json
from fpdf import FPDF
# Define the Overpass API endpoint
overpass_url = "http://overpass-api.de/api/interpreter"
# Constants for the UI
PRIMARY_COLOR = "#164031" # dark green
SECONDARY_COLOR = "#d99115" # golden yellow
ACCENT_COLOR = "#f16948" # orange
BACKGROUND_COLOR = "#f0ecdf" # background color from the image
DEFAULT_VILLAGE = "Ossana" # Default village for testing
# API configuration for AI analysis
API_URL = 'https://www.chatbase.co/api/v1/chat'
API_HEADERS = {
'Authorization': st.secrets["AUTH"],
'Content-Type': 'application/json'
}
CHATBOT_ID = st.secrets["ID"]
# Villages list
villages = [
"Caldes",
"Cavizzana",
"Terzolas",
"Male",
"Croviana",
"Dimaro Folgarida",
"Commezzadura",
"Mezzana",
"Pellizzano",
"Rabbi",
"Peio",
"Ossana",
"Vermiglio",
"Sóller",
"Port of Sóller",
"Fornalutx/Biniaraix",
"Alavieska",
"Kalajoki",
"Nivala",
"Nevesinje",
"Gacko",
"Bileća",
"Padna",
"Šmarje",
"Agatovo",
"Alexandrovo",
"Brestovo",
"Gorsko Slivovo",
"Kakrina",
"Karpachevo",
"Krushuna",
"Kramolin",
"Tepa"
]
def get_amenities_by_village(village_name):
"""
Fetches amenities around the given village name using Overpass API.
"""
overpass_query = f"""
[out:json];
area["name"="{village_name}"]["boundary"="administrative"]->.searchArea;
(
node["amenity"](area.searchArea);
way["amenity"](area.searchArea);
relation["amenity"](area.searchArea);
);
out body;
>;
out skel qt;
"""
response = requests.get(overpass_url, params={'data': overpass_query})
# Check if response is successful
if response.status_code == 200:
data = response.json()
else:
st.warning(f"API request failed with status code {response.status_code}")
return None
# Group amenities by type
amenities = defaultdict(list)
for element in data.get('elements', []):
if 'tags' in element and 'amenity' in element['tags']:
amenity_type = element['tags']['amenity']
amenities[amenity_type].append(element)
return amenities
def add_markers_to_map(m, amenities):
"""
Adds markers to the map for given amenities.
"""
for amenity_type, elements in amenities.items():
for element in elements:
if 'lat' in element and 'lon' in element:
point_location = [element['lat'], element['lon']]
tooltip = f"{amenity_type}: {element.get('tags', {}).get('name', 'N/A')}"
folium.CircleMarker(
location=point_location,
radius=5,
popup=tooltip,
color=ACCENT_COLOR,
fill=True,
fill_color=ACCENT_COLOR
).add_to(m)
def generate_pdf(text, filename):
"""
Generates a PDF from the provided text and saves it with the specified filename.
"""
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_left_margin(15)
pdf.set_right_margin(15)
pdf.set_font("Arial", size=12)
line_height = pdf.font_size * 2.5
for line in text.split('\n'):
words = line.split(' ')
current_line = ""
for word in words:
if pdf.get_string_width(current_line + word) < (pdf.w - pdf.l_margin - pdf.r_margin):
current_line += f"{word} "
else:
pdf.cell(0, line_height, txt=current_line.strip(), ln=1)
current_line = f"{word} "
pdf.cell(0, line_height, txt=current_line.strip(), ln=1)
pdf.output(filename)
return filename
def main():
st.markdown(
f"""
<style>
.stApp {{
background-color: {BACKGROUND_COLOR};
}}
.stButton>button {{
background-color: {SECONDARY_COLOR};
color: white;
}}
h1 {{
color: {SECONDARY_COLOR};
}}
</style>
""",
unsafe_allow_html=True
)
st.image("logo.png", width=200)
st.markdown(
f"<h1 style='color: {SECONDARY_COLOR};'>Pilots Analyzer</h1>",
unsafe_allow_html=True
)
village_choice = st.selectbox("Choose a Village:", villages, index=villages.index(DEFAULT_VILLAGE))
# Initialize session state for amenities and map
if 'amenities' not in st.session_state:
st.session_state.amenities = None
if 'map' not in st.session_state:
st.session_state.map = None
# Button to show amenities
if st.button('Show Amenities'):
try:
amenities = get_amenities_by_village(village_choice)
if amenities and amenities.keys():
st.session_state.amenities = amenities # Store amenities in session state
# Create a new map and replace the old one in session state
first_amenity = next(iter(amenities.values()))[0]
m = folium.Map(location=[first_amenity['lat'], first_amenity['lon']], zoom_start=14)
add_markers_to_map(m, amenities)
st.session_state.map = m # Update session state with the new map
else:
st.warning(f"No amenities found for {village_choice}.")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
# Display the map (only once, at the end of the section)
if st.session_state.map:
folium_static(st.session_state.map)
st.subheader("AI Assistant")
# Button for AI analysis
if st.button('Analyze'):
if st.session_state.amenities:
try:
# Preparing data for the AI analysis
amenities = st.session_state.amenities
amenities_summary = "\n".join([f"{amenity_type}: {len(elements)}" for amenity_type, elements in amenities.items()])
message_content = f"What is the degree of digitalization, smartness, rural development, or similar in {village_choice} with these facilities:\n{amenities_summary}\nWhat can we do to improve it? Do you have any suggestions?"
data = {
"messages": [
{"content": message_content, "role": "user"}
],
"chatbotId": CHATBOT_ID,
"stream": False,
"temperature": 0
}
response = requests.post(API_URL, headers=API_HEADERS, data=json.dumps(data))
if response.status_code == 200:
json_data = response.json()
response_text = json_data.get('text', 'No text in response')
st.write("Response:", response_text)
pdf_filename = f"AI_Analysis_{village_choice}.pdf"
generate_pdf(response_text, pdf_filename)
with open(pdf_filename, "rb") as pdf_file:
st.download_button("Download Analysis as PDF", pdf_file, file_name=pdf_filename)
else:
error_message = response.json().get('message', 'Unknown error')
st.error(f'Error: {error_message}')
except Exception as e:
st.error(f"An error occurred during AI analysis: {str(e)}")
else:
st.warning("Please show the amenities first by clicking 'Show Amenities'.")
if __name__ == "__main__":
main()