File size: 2,065 Bytes
25ffa44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7b0d5b
e4a5fc7
 
 
25ffa44
 
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
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)

    with gr.Row():
        view_recommendation_button = gr.Button("View All recommendations")
        view_recommendation_button.click(fn=None, inputs=None, outputs=None, js="() => { window.location.href = 'https://prashasst-will-find-your-anime.vercel.app/'; }")
        
# Launch the app
app.launch(share=True)