SerZak05 commited on
Commit
1367d73
·
1 Parent(s): 538569e

Changed entrypoint

Browse files
Files changed (2) hide show
  1. src/app.py +0 -75
  2. 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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/streamlit_app.py CHANGED
@@ -1,40 +1,75 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
 
 
 
 
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
 
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
 
 
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
 
 
18
 
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")