Spaces:
Build error
Build error
changed file system to be more streamlined
Browse files- app.py +39 -0
- data/cat.jpg +0 -0
- functions/loader.py +25 -0
- functions/logic.py +25 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import faiss
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from functions.loader import data, index
|
| 5 |
+
from functions.logic import load_image, find_similar_shows
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
st.title('TV Show Recommender')
|
| 10 |
+
|
| 11 |
+
# User input for the show description
|
| 12 |
+
user_description = st.text_area("Describe the TV show you're looking for:")
|
| 13 |
+
|
| 14 |
+
# Slider for the number of recommendations
|
| 15 |
+
num_recommendations = st.slider('Number of recommendations:', min_value=1, max_value=10, value=5)
|
| 16 |
+
|
| 17 |
+
# Button to get recommendations
|
| 18 |
+
if st.button('Recommend') and user_description:
|
| 19 |
+
try:
|
| 20 |
+
recommended_shows = find_similar_shows(user_description, index, num_recommendations)
|
| 21 |
+
|
| 22 |
+
for idx in recommended_shows.index:
|
| 23 |
+
with st.container():
|
| 24 |
+
link = data.loc[idx, 'url']
|
| 25 |
+
poster_url = data.loc[idx, 'poster']
|
| 26 |
+
title = data.loc[idx, 'title']
|
| 27 |
+
description = data.loc[idx, 'description']
|
| 28 |
+
|
| 29 |
+
img = load_image(poster_url) # Use the load_image function
|
| 30 |
+
|
| 31 |
+
col1, col2 = st.columns([1, 2])
|
| 32 |
+
with col1:
|
| 33 |
+
st.image(img, caption=title, use_column_width='always')
|
| 34 |
+
with col2:
|
| 35 |
+
st.write(description)
|
| 36 |
+
st.markdown(f"[More Info]({link})", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error(f"An error occurred: {e}")
|
data/cat.jpg
ADDED
|
functions/loader.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import faiss
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@st.cache_data
|
| 9 |
+
def load_model():
|
| 10 |
+
model = SentenceTransformer('cointegrated/rubert-tiny2')
|
| 11 |
+
return model
|
| 12 |
+
|
| 13 |
+
@st.cache_data
|
| 14 |
+
def load_faiss_index():
|
| 15 |
+
index = faiss.read_index('data/faiss_index.index')
|
| 16 |
+
return index
|
| 17 |
+
|
| 18 |
+
@st.cache_data
|
| 19 |
+
def load_dataset():
|
| 20 |
+
data = pd.read_csv('data/cleaned_data.csv')
|
| 21 |
+
return data
|
| 22 |
+
|
| 23 |
+
model = load_model()
|
| 24 |
+
index = load_faiss_index()
|
| 25 |
+
data = load_dataset()
|
functions/logic.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from functions.loader import model, data
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def load_image(url):
|
| 9 |
+
try:
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
img = Image.open(BytesIO(response.content))
|
| 12 |
+
except Exception:
|
| 13 |
+
img = Image.open("data/cat.jpg")
|
| 14 |
+
return img
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def vectorize(descriptions):
|
| 18 |
+
embeddings = model.encode(descriptions)
|
| 19 |
+
return embeddings
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def find_similar_shows(user_description, index, k=5):
|
| 23 |
+
query_vector = vectorize([user_description])
|
| 24 |
+
_, indices = index.search(query_vector, k)
|
| 25 |
+
return data.iloc[indices.flatten()]
|