File size: 982 Bytes
05af8c2
 
dcf3bcc
05af8c2
 
dcf3bcc
 
05af8c2
dcf3bcc
 
05af8c2
dcf3bcc
 
 
05af8c2
dcf3bcc
 
 
 
 
 
 
05af8c2
dcf3bcc
 
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
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()