Spaces:
Sleeping
Sleeping
Changed entrypoint
Browse files- src/app.py +0 -75
- src/streamlit_app.py +69 -34
src/app.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import folium
|
| 3 |
-
from streamlit_folium import st_folium
|
| 4 |
-
from collections import defaultdict
|
| 5 |
-
# from joblib import Parallel, delayed
|
| 6 |
-
from post_assesment import get_sentiment, emotions
|
| 7 |
-
from post_search import search_posts_parallel
|
| 8 |
-
|
| 9 |
-
emotion_to_color = {
|
| 10 |
-
'no_emotion': "#666666",
|
| 11 |
-
'joy': "#33cc33",
|
| 12 |
-
'sadness': "#0066ff",
|
| 13 |
-
'surprise': "#ff9900",
|
| 14 |
-
'fear': "#aa2fd6",
|
| 15 |
-
'anger': "#ff0000"
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
POSTS_CNT = 500
|
| 19 |
-
# TOP_CITIES_CNT = 2
|
| 20 |
-
NUM_OF_WORKERS = 8
|
| 21 |
-
# top_cities = cities_db.nlargest(n=TOP_CITIES_CNT, columns="population")
|
| 22 |
-
|
| 23 |
-
# === Beginning of the page ===
|
| 24 |
-
st.title("Sentiment analysis")
|
| 25 |
-
topic = st.text_input("Enter your topic:", "котики")
|
| 26 |
-
button = st.button("Start!")
|
| 27 |
-
|
| 28 |
-
if button:
|
| 29 |
-
st.session_state["running"] = True
|
| 30 |
-
st.session_state.pop("results", None)
|
| 31 |
-
st.session_state.pop("posts", None)
|
| 32 |
-
|
| 33 |
-
if st.session_state.get("running", False):
|
| 34 |
-
st.text("Processing query...")
|
| 35 |
-
st.session_state["posts"] = search_posts_parallel(topic, POSTS_CNT)
|
| 36 |
-
# posts_per_city = Parallel(n_jobs=NUM_OF_WORKERS) \
|
| 37 |
-
# (
|
| 38 |
-
# delayed(search_posts_by_pos)(topic, POSTS_CNT, city_row["city"], city_row["lat"], city_row["lon"])
|
| 39 |
-
# for ind, city_row in top_cities.iterrows()
|
| 40 |
-
# )
|
| 41 |
-
# posts = [post for city_list in posts_per_city for post in city_list]
|
| 42 |
-
# print(*[post.owner_id for post in posts], sep='\n', flush=True)
|
| 43 |
-
# st.session_state["posts"] = posts
|
| 44 |
-
st.text("Gathered posts...")
|
| 45 |
-
st.session_state["results"] = get_sentiment(st.session_state["posts"])
|
| 46 |
-
# st.write(st.session_state["results"])
|
| 47 |
-
st.session_state["running"] = False
|
| 48 |
-
|
| 49 |
-
if "results" in st.session_state:
|
| 50 |
-
print("Got results!", flush=True)
|
| 51 |
-
posts = st.session_state["posts"]
|
| 52 |
-
results = st.session_state["results"]
|
| 53 |
-
scores = defaultdict(lambda: {e: 0.0 for e in emotions})
|
| 54 |
-
cnt = defaultdict(int)
|
| 55 |
-
names = {}
|
| 56 |
-
for i in range(len(posts)):
|
| 57 |
-
pos = posts[i].geolocation
|
| 58 |
-
names[pos] = posts[i].city_of_origin
|
| 59 |
-
cnt[pos] += 1
|
| 60 |
-
# for label, score in results[i].items():
|
| 61 |
-
# scores[pos][label] = score
|
| 62 |
-
scores[pos][results[i]["label"]] = results[i]["score"] if results[i]["label"] != "no_emotion" else 0.001
|
| 63 |
-
colors = {pos: emotion_to_color[max(score, key=score.get)] for pos, score in scores.items()}
|
| 64 |
-
map_table = {
|
| 65 |
-
"lon": [pos[0] for pos in cnt.keys()],
|
| 66 |
-
"lat": [pos[1] for pos in cnt.keys()],
|
| 67 |
-
"color": colors,
|
| 68 |
-
"size": cnt.values()
|
| 69 |
-
}
|
| 70 |
-
m = folium.Map()
|
| 71 |
-
for pos in cnt.keys():
|
| 72 |
-
# print(pos)
|
| 73 |
-
folium.CircleMarker((float(pos[0]), float(pos[1])), radius=cnt[pos] / POSTS_CNT * 100, color=colors[pos]).add_to(m)
|
| 74 |
-
st_folium(m, width=725, returned_objects=[])
|
| 75 |
-
# st.map(map_table, latitude="lat", longitude="lon", color="color", size="size")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import folium
|
| 3 |
+
from streamlit_folium import st_folium
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
# from joblib import Parallel, delayed
|
| 6 |
+
from post_assesment import get_sentiment, emotions
|
| 7 |
+
from post_search import search_posts_parallel
|
| 8 |
|
| 9 |
+
emotion_to_color = {
|
| 10 |
+
'no_emotion': "#666666",
|
| 11 |
+
'joy': "#33cc33",
|
| 12 |
+
'sadness': "#0066ff",
|
| 13 |
+
'surprise': "#ff9900",
|
| 14 |
+
'fear': "#aa2fd6",
|
| 15 |
+
'anger': "#ff0000"
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
POSTS_CNT = 500
|
| 19 |
+
# TOP_CITIES_CNT = 2
|
| 20 |
+
NUM_OF_WORKERS = 8
|
| 21 |
+
# top_cities = cities_db.nlargest(n=TOP_CITIES_CNT, columns="population")
|
| 22 |
|
| 23 |
+
# === Beginning of the page ===
|
| 24 |
+
st.title("Sentiment analysis")
|
| 25 |
+
topic = st.text_input("Enter your topic:", "котики")
|
| 26 |
+
button = st.button("Start!")
|
| 27 |
|
| 28 |
+
if button:
|
| 29 |
+
st.session_state["running"] = True
|
| 30 |
+
st.session_state.pop("results", None)
|
| 31 |
+
st.session_state.pop("posts", None)
|
| 32 |
|
| 33 |
+
if st.session_state.get("running", False):
|
| 34 |
+
st.text("Processing query...")
|
| 35 |
+
st.session_state["posts"] = search_posts_parallel(topic, POSTS_CNT)
|
| 36 |
+
# posts_per_city = Parallel(n_jobs=NUM_OF_WORKERS) \
|
| 37 |
+
# (
|
| 38 |
+
# delayed(search_posts_by_pos)(topic, POSTS_CNT, city_row["city"], city_row["lat"], city_row["lon"])
|
| 39 |
+
# for ind, city_row in top_cities.iterrows()
|
| 40 |
+
# )
|
| 41 |
+
# posts = [post for city_list in posts_per_city for post in city_list]
|
| 42 |
+
# print(*[post.owner_id for post in posts], sep='\n', flush=True)
|
| 43 |
+
# st.session_state["posts"] = posts
|
| 44 |
+
st.text("Gathered posts...")
|
| 45 |
+
st.session_state["results"] = get_sentiment(st.session_state["posts"])
|
| 46 |
+
# st.write(st.session_state["results"])
|
| 47 |
+
st.session_state["running"] = False
|
| 48 |
+
|
| 49 |
+
if "results" in st.session_state:
|
| 50 |
+
print("Got results!", flush=True)
|
| 51 |
+
posts = st.session_state["posts"]
|
| 52 |
+
results = st.session_state["results"]
|
| 53 |
+
scores = defaultdict(lambda: {e: 0.0 for e in emotions})
|
| 54 |
+
cnt = defaultdict(int)
|
| 55 |
+
names = {}
|
| 56 |
+
for i in range(len(posts)):
|
| 57 |
+
pos = posts[i].geolocation
|
| 58 |
+
names[pos] = posts[i].city_of_origin
|
| 59 |
+
cnt[pos] += 1
|
| 60 |
+
# for label, score in results[i].items():
|
| 61 |
+
# scores[pos][label] = score
|
| 62 |
+
scores[pos][results[i]["label"]] = results[i]["score"] if results[i]["label"] != "no_emotion" else 0.001
|
| 63 |
+
colors = {pos: emotion_to_color[max(score, key=score.get)] for pos, score in scores.items()}
|
| 64 |
+
map_table = {
|
| 65 |
+
"lon": [pos[0] for pos in cnt.keys()],
|
| 66 |
+
"lat": [pos[1] for pos in cnt.keys()],
|
| 67 |
+
"color": colors,
|
| 68 |
+
"size": cnt.values()
|
| 69 |
+
}
|
| 70 |
+
m = folium.Map()
|
| 71 |
+
for pos in cnt.keys():
|
| 72 |
+
# print(pos)
|
| 73 |
+
folium.CircleMarker((float(pos[0]), float(pos[1])), radius=cnt[pos] / POSTS_CNT * 100, color=colors[pos]).add_to(m)
|
| 74 |
+
st_folium(m, width=725, returned_objects=[])
|
| 75 |
+
# st.map(map_table, latitude="lat", longitude="lon", color="color", size="size")
|