Spaces:
Configuration error
Configuration error
Update app.py
Browse filesmade an update to fix runtime error
app.py
CHANGED
@@ -1,31 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
-
import pickle
|
3 |
import pandas as pd
|
|
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
|
6 |
-
# Load
|
7 |
-
|
8 |
-
model = pickle.load(f)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
vectorizer = model["vectorizer"] # for transforming user input
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
sims = cosine_similarity(user_vec, post_embeddings)[0]
|
18 |
-
top_idxs = sims.argsort()[-5:][::-1]
|
19 |
-
top_posts = posts_df.iloc[top_idxs]["post_text"].tolist()
|
20 |
-
return "\n\n".join(top_posts)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
)
|
30 |
|
31 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import pandas as pd
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
|
6 |
+
# Load your trained SentenceTransformer model
|
7 |
+
model = SentenceTransformer("all-MiniLM-L6-v2") # Or use your custom model path if you trained a new one
|
|
|
8 |
|
9 |
+
# Load the posts
|
10 |
+
posts = pd.read_csv("posts.csv")
|
|
|
11 |
|
12 |
+
# Precompute embeddings for all posts
|
13 |
+
post_texts = posts["post_text"].astype(str).tolist()
|
14 |
+
post_embeddings = model.encode(post_texts, convert_to_tensor=False)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Recommendation function
|
17 |
+
def recommend(user_input):
|
18 |
+
user_embedding = model.encode([user_input], convert_to_tensor=False)
|
19 |
+
similarities = cosine_similarity(user_embedding, post_embeddings)[0]
|
20 |
+
top_indices = similarities.argsort()[-5:][::-1]
|
21 |
+
recommended = posts.iloc[top_indices]["post_text"].tolist()
|
22 |
+
return "\n\n".join(recommended)
|
|
|
23 |
|
24 |
+
# Launch Gradio app
|
25 |
+
gr.Interface(fn=recommend, inputs="text", outputs="text", title="AI Post Recommender").launch()
|