kambris commited on
Commit
98d4e8c
·
verified ·
1 Parent(s): 5913d75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -15
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
- country = summary['country']
136
- if country in coordinates:
137
- # Get top topic
138
- top_topic = summary['top_topics'][0]['topic'] if summary['top_topics'] else "No topics"
139
- top_emotion = summary['top_emotions'][0]['emotion'] if summary['top_emotions'] else "No emotion"
140
 
141
  # Create popup content
142
  popup_content = f"""
143
- <b>{country}</b><br>
144
- Top Topic: {top_topic}<br>
145
- Main Emotion: {top_emotion}<br>
 
 
146
  Total Poems: {summary['total_poems']}
147
  """
148
 
149
  # Add marker
150
  folium.CircleMarker(
151
- location=coordinates[country],
152
  radius=10,
153
  popup=folium.Popup(popup_content, max_width=300),
154
- color='red',
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: