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