Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import faiss
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
|
6 |
+
model=SentenceTransformer("Prashasst/anime-recommendation-model")
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
embeddings = np.load('data/embeddings.npy')
|
11 |
+
embeddings_id = np.load('data/embeddings_id.npy')
|
12 |
+
|
13 |
+
index=faiss.read_index('data/anime_faiss.index')
|
14 |
+
|
15 |
+
def recommend_anime(query, k=5):
|
16 |
+
"""
|
17 |
+
Recommends anime based on a query using a FAISS index and a Prashasst's SentenceTransformer model.
|
18 |
+
|
19 |
+
Args:
|
20 |
+
query (str): The input query to find similar anime.
|
21 |
+
k (int): The number of recommendations to return.
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
List[str]: A list of recommended anime ids.
|
25 |
+
"""
|
26 |
+
|
27 |
+
|
28 |
+
# Encode the query
|
29 |
+
query_embedding = model.encode(query).reshape(1, -1) # Reshape to 2D array
|
30 |
+
|
31 |
+
# Search for similar anime
|
32 |
+
distances, indices = index.search(query_embedding, k=k)
|
33 |
+
|
34 |
+
# Get the anime titles
|
35 |
+
recommended_anime = []
|
36 |
+
for i in indices[0]:
|
37 |
+
anime_id = embeddings_id[i]
|
38 |
+
# anime_name = df.loc[df['id'] == anime_id, 'title_english'].values[0]
|
39 |
+
# if pd.isna(anime_name):
|
40 |
+
# anime_name = df.loc[df['id'] == anime_id, 'title_romaji'].values[0]
|
41 |
+
recommended_anime.append(anime_id)
|
42 |
+
|
43 |
+
return {"ids":recommended_anime}
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
# Create the Gradio app
|
48 |
+
with gr.Blocks() as app:
|
49 |
+
gr.Markdown("## Anime Recommendation System")
|
50 |
+
|
51 |
+
with gr.Row():
|
52 |
+
query = gr.Textbox(label="Enter your anime preferences or query:")
|
53 |
+
top_k = gr.Slider(1, 10, value=5, label="Number of Recommendations")
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
recommend_button = gr.Button("Get Recommendations")
|
57 |
+
output = gr.JSON(label="Recommended Anime")
|
58 |
+
|
59 |
+
recommend_button.click(recommend_anime, inputs=[query, top_k], outputs=output)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
app.launch(share=True)
|