Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
import pickle
|
3 |
import pandas as pd
|
|
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
6 |
-
|
7 |
-
# Load model and dataset
|
8 |
-
with open("recommender_model.pkl", "rb") as f:
|
9 |
-
model = pickle.load(f)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Predict function
|
18 |
def recommend_from_input(user_text):
|
19 |
-
|
|
|
|
|
20 |
sims = cosine_similarity(user_vec, post_embeddings)[0]
|
21 |
-
top_idxs = sims.argsort()[-5:][::-1]
|
22 |
-
|
23 |
-
return "\n\n".join(top_posts)
|
24 |
|
25 |
# Gradio UI
|
26 |
interface = gr.Interface(
|
27 |
fn=recommend_from_input,
|
28 |
-
inputs="
|
29 |
-
outputs="
|
30 |
title="AI Content Recommender",
|
31 |
-
description="Enter a
|
|
|
32 |
)
|
33 |
|
34 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pickle
|
3 |
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load model and data with error handling
|
8 |
+
try:
|
9 |
+
with open("recommender_model.pkl", "rb") as f:
|
10 |
+
model = pickle.load(f)
|
11 |
+
posts_df = pd.read_csv("posts_cleaned.csv")
|
12 |
+
post_texts = posts_df["post_text"].astype(str).tolist()
|
13 |
+
post_embeddings = np.load("post_embeddings.npy") # Precomputed
|
14 |
+
except Exception as e:
|
15 |
+
raise gr.Error(f"Error loading files: {str(e)}")
|
16 |
|
17 |
# Predict function
|
18 |
def recommend_from_input(user_text):
|
19 |
+
if not user_text.strip():
|
20 |
+
return "Please enter valid text."
|
21 |
+
user_vec = model.encode([user_text])
|
22 |
sims = cosine_similarity(user_vec, post_embeddings)[0]
|
23 |
+
top_idxs = sims.argsort()[-5:][::-1] # Top 5 posts
|
24 |
+
return posts_df.iloc[top_idxs]["post_text"].tolist()
|
|
|
25 |
|
26 |
# Gradio UI
|
27 |
interface = gr.Interface(
|
28 |
fn=recommend_from_input,
|
29 |
+
inputs=gr.Textbox(label="Describe your interests"),
|
30 |
+
outputs=gr.Dataframe(headers=["Recommended Posts"]),
|
31 |
title="AI Content Recommender",
|
32 |
+
description="Enter a topic or post to get recommendations",
|
33 |
+
examples=[["Web3 security"], ["Machine learning trends"]]
|
34 |
)
|
35 |
|
36 |
+
interface.launch()
|