prasant.goswivt commited on
Commit
fd334ba
β€’
1 Parent(s): 4f15b7a

added sentiment analysis

Browse files
Files changed (1) hide show
  1. app.py +77 -2
app.py CHANGED
@@ -1,4 +1,79 @@
1
  import streamlit as st
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import random
3
+ import pickle
4
+ from sentiment import get_sentiment
5
 
6
+ # Load the data
7
+ novel_list = pickle.load(open('D:/projects/Recon/data/novel_list.pkl', 'rb'))
8
+ novel_list['english_publisher'] = novel_list['english_publisher'].fillna('unknown')
9
+ name_list = novel_list['name'].values
10
+
11
+ def recommend(novel, slider_start):
12
+ try:
13
+ similarity = pickle.load(open('D:/projects/Recon/data/similarity.pkl', 'rb'))
14
+ novel_index = novel_list[novel_list['name'] == novel].index[0]
15
+ distances = similarity[novel_index]
16
+ new_novel_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[slider_start:slider_start+9]
17
+ except IndexError:
18
+ return None
19
+
20
+ recommend_novel = [{'name': novel_list.iloc[i[0]]['name'], 'image_url': novel_list.iloc[i[0]]['image_url'], 'english_publisher': novel_list.iloc[i[0]]['english_publisher']} for i in new_novel_list]
21
+ return recommend_novel
22
+
23
+ def main():
24
+ st.title("πŸ“š Novel Recommender System")
25
+
26
+ # Input fields and buttons
27
+ selected_novel_name = st.text_input("πŸ”Ž Choose a Novel to get Recommendations", "Mother of Learning")
28
+ slider_value = st.slider("Slider", 1, 100, 1)
29
+
30
+ col1, col2, col3 = st.columns(3) # Create three columns to place buttons side by side
31
+ with col1:
32
+ btn_recommend = st.button("πŸ’‘ Recommend")
33
+ with col2:
34
+ btn_random = st.button("🎲 Random")
35
+ with col3:
36
+ btn_analysis = st.button("Analysis")
37
+
38
+ if btn_recommend:
39
+ recommendations = recommend(selected_novel_name, slider_value)
40
+ if recommendations:
41
+ for i in range(0, len(recommendations), 3): # Process 3 recommendations at a time
42
+ cols = st.columns(3)
43
+ for j in range(3):
44
+ if i + j < len(recommendations):
45
+ novel = recommendations[i + j]
46
+ with cols[j]:
47
+ st.image(novel["image_url"], use_column_width=True)
48
+ st.write(novel["name"])
49
+ else:
50
+ st.warning("Novel not found in our database. Please try another one.")
51
+
52
+ if btn_random:
53
+ random_novels = random.sample(list(name_list), 9)
54
+ for i in range(0, len(random_novels), 3):
55
+ cols = st.columns(3)
56
+ for j in range(3):
57
+ if i + j < len(random_novels):
58
+ novel_name = random_novels[i + j]
59
+ novel_img = novel_list[novel_list['name'] == novel_name]['image_url'].values[0]
60
+ with cols[j]:
61
+ st.image(novel_img, use_column_width=True)
62
+ st.write(novel_name)
63
+
64
+ if btn_analysis:
65
+ try:
66
+ positive, negative, wordcloud = get_sentiment(selected_novel_name)
67
+ st.write(f"😊 {positive}% Positive")
68
+ st.write(f"☹️ {negative}% Negative")
69
+ print(wordcloud)
70
+
71
+
72
+
73
+ st.image(wordcloud)
74
+
75
+ except Exception as e:
76
+ st.error("An error occurred during sentiment analysis.")
77
+
78
+ if __name__ == "__main__":
79
+ main()