import gzip import pickle import gradio as gr # Load model with gzip.open("pivot.pkl.gz", "rb") as f: pivot = pickle.load(f) with gzip.open("knn_model.pkl.gz", "rb") as f: model = pickle.load(f) def recommend(anime_name): if not anime_name or len(anime_name.strip()) < 2: return ["❌ Anime not found"] anime_name = anime_name.lower().strip() matches = [anime for anime in pivot.index if anime_name in anime.lower()] if not matches: return ["❌ Anime not found"] anime_name = matches[0] index = pivot.index.get_loc(anime_name) distances, indices = model.kneighbors( pivot.iloc[index, :].values.reshape(1, -1), n_neighbors=6 ) recommendations = [pivot.index[i] for i in indices[0][1:]] return recommendations # 🎨 Better UI with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# 🎌 Anime Recommendation System") gr.Markdown("Enter an anime name and get similar recommendations") inp = gr.Textbox(placeholder="Type anime name (e.g., Naruto)") btn = gr.Button("Get Recommendations") output = gr.Textbox(label="Recommendations") def run(x): result = recommend(x) return "\n".join(result) btn.click(fn=run, inputs=inp, outputs=output) demo.launch()