Spaces:
Configuration error
Configuration error
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 | |
model = SentenceTransformer("all-MiniLM-L6-v2") # Or use your custom model path if you trained a new one | |
# Load the posts | |
posts = pd.read_csv("posts.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() | |