Spaces:
Running
Running
import gradio as gr | |
import numpy as np | |
import faiss | |
from sentence_transformers import SentenceTransformer | |
model=SentenceTransformer("Prashasst/anime-recommendation-model") | |
embeddings = np.load('data/embeddings.npy') | |
embeddings_id = np.load('data/embeddings_id.npy') | |
index=faiss.read_index('data/anime_faiss.index') | |
def recommend_anime(query, k=5): | |
""" | |
Recommends anime based on a query using a FAISS index and a Prashasst's SentenceTransformer model. | |
Args: | |
query (str): The input query to find similar anime. | |
k (int): The number of recommendations to return. | |
Returns: | |
List[str]: A list of recommended anime ids. | |
""" | |
# Encode the query | |
query_embedding = model.encode(query).reshape(1, -1) # Reshape to 2D array | |
# Search for similar anime | |
distances, indices = index.search(query_embedding, k=k) | |
# Get the anime titles | |
recommended_anime = [] | |
for i in indices[0]: | |
anime_id = embeddings_id[i] | |
# anime_name = df.loc[df['id'] == anime_id, 'title_english'].values[0] | |
# if pd.isna(anime_name): | |
# anime_name = df.loc[df['id'] == anime_id, 'title_romaji'].values[0] | |
recommended_anime.append(anime_id) | |
return {"ids":recommended_anime} | |
# Create the Gradio app | |
with gr.Blocks() as app: | |
gr.Markdown("## Anime Recommendation System") | |
with gr.Row(): | |
query = gr.Textbox(label="Enter your anime preferences or query:") | |
top_k = gr.Slider(1, 10, value=5, label="Number of Recommendations") | |
with gr.Row(): | |
recommend_button = gr.Button("Get Recommendations") | |
output = gr.JSON(label="Recommended Anime") | |
recommend_button.click(recommend_anime, inputs=[query, top_k], outputs=output) | |
# Launch the app | |
app.launch(share=True) | |