Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -125,35 +125,52 @@ def get_country_coordinates():
|
|
125 |
'Mauritania': [21.0079, -10.9408]
|
126 |
}
|
127 |
def create_topic_map(summaries):
|
128 |
-
"""Create an interactive map showing topic distribution"""
|
129 |
coordinates = get_country_coordinates()
|
130 |
-
|
131 |
-
# Create base map centered on Arab world
|
132 |
m = folium.Map(location=[25.0, 30.0], zoom_start=4)
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
for summary in summaries:
|
135 |
-
|
136 |
-
if
|
137 |
-
# Get
|
138 |
-
|
139 |
-
|
140 |
|
141 |
# Create popup content
|
142 |
popup_content = f"""
|
143 |
-
<b>{
|
144 |
-
|
145 |
-
|
|
|
|
|
146 |
Total Poems: {summary['total_poems']}
|
147 |
"""
|
148 |
|
149 |
# Add marker
|
150 |
folium.CircleMarker(
|
151 |
-
location=coordinates[
|
152 |
radius=10,
|
153 |
popup=folium.Popup(popup_content, max_width=300),
|
154 |
-
color=
|
155 |
fill=True
|
156 |
).add_to(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
return m
|
159 |
|
@@ -514,9 +531,8 @@ if uploaded_file is not None:
|
|
514 |
st.write(f"• Topic {row['Topic']}: {topic_name} ({row['Count']} poems)")
|
515 |
|
516 |
with tab3:
|
517 |
-
st.subheader("Topic Distribution Map")
|
518 |
topic_map = create_topic_map(summaries)
|
519 |
-
# Display the map
|
520 |
st.components.v1.html(topic_map._repr_html_(), height=600)
|
521 |
|
522 |
except Exception as e:
|
|
|
125 |
'Mauritania': [21.0079, -10.9408]
|
126 |
}
|
127 |
def create_topic_map(summaries):
|
|
|
128 |
coordinates = get_country_coordinates()
|
|
|
|
|
129 |
m = folium.Map(location=[25.0, 30.0], zoom_start=4)
|
130 |
|
131 |
+
# Color mapping for sentiments
|
132 |
+
sentiment_colors = {
|
133 |
+
'Positive': 'green',
|
134 |
+
'Negative': 'red',
|
135 |
+
'Neutral': 'blue'
|
136 |
+
}
|
137 |
+
|
138 |
for summary in summaries:
|
139 |
+
country_en = COUNTRY_MAPPING.get(summary['country'])
|
140 |
+
if country_en and country_en in coordinates:
|
141 |
+
# Get dominant sentiment
|
142 |
+
dominant_emotion = summary['top_emotions'][0]['emotion'] if summary['top_emotions'] else "Neutral"
|
143 |
+
circle_color = sentiment_colors.get(dominant_emotion, 'gray')
|
144 |
|
145 |
# Create popup content
|
146 |
popup_content = f"""
|
147 |
+
<b>{country_en}</b><br>
|
148 |
+
<b>Sentiment Distribution:</b><br>
|
149 |
+
{'<br>'.join(f"• {e['emotion']}: {e['count']}" for e in summary['top_emotions'][:3])}<br>
|
150 |
+
<b>Top Topic:</b><br>
|
151 |
+
{summary['top_topics'][0]['topic'] if summary['top_topics'] else 'No topics'}<br>
|
152 |
Total Poems: {summary['total_poems']}
|
153 |
"""
|
154 |
|
155 |
# Add marker
|
156 |
folium.CircleMarker(
|
157 |
+
location=coordinates[country_en],
|
158 |
radius=10,
|
159 |
popup=folium.Popup(popup_content, max_width=300),
|
160 |
+
color=circle_color,
|
161 |
fill=True
|
162 |
).add_to(m)
|
163 |
+
|
164 |
+
# Add legend
|
165 |
+
legend_html = """
|
166 |
+
<div style="position: fixed; bottom: 50px; left: 50px; z-index: 1000; background-color: white; padding: 10px; border: 2px solid grey; border-radius: 5px">
|
167 |
+
<p><b>Sentiment:</b></p>
|
168 |
+
<p><span style="color: green;">●</span> Positive</p>
|
169 |
+
<p><span style="color: red;">●</span> Negative</p>
|
170 |
+
<p><span style="color: blue;">●</span> Neutral</p>
|
171 |
+
</div>
|
172 |
+
"""
|
173 |
+
m.get_root().html.add_child(folium.Element(legend_html))
|
174 |
|
175 |
return m
|
176 |
|
|
|
531 |
st.write(f"• Topic {row['Topic']}: {topic_name} ({row['Count']} poems)")
|
532 |
|
533 |
with tab3:
|
534 |
+
st.subheader("Topic and Sentiment Distribution Map")
|
535 |
topic_map = create_topic_map(summaries)
|
|
|
536 |
st.components.v1.html(topic_map._repr_html_(), height=600)
|
537 |
|
538 |
except Exception as e:
|