LinDee's picture
Update app.py
c663d7d verified
raw
history blame
944 Bytes
import gradio as gr
import pandas as pd
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
# Load your trained SentenceTransformer model
import joblib
model = joblib.load("recommender_model.pkl")
# Load the posts
posts = pd.read_csv("posts_cleaned.csv")
# Precompute embeddings for all posts
post_texts = posts["post_text"].astype(str).tolist()
post_embeddings = model.encode(post_texts, convert_to_tensor=False)
# Recommendation function
def recommend(user_input):
user_embedding = model.encode([user_input], convert_to_tensor=False)
similarities = cosine_similarity(user_embedding, post_embeddings)[0]
top_indices = similarities.argsort()[-5:][::-1]
recommended = posts.iloc[top_indices]["post_text"].tolist()
return "\n\n".join(recommended)
# Launch Gradio app
gr.Interface(fn=recommend, inputs="text", outputs="text", title="AI Post Recommender").launch()