Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,41 @@
|
|
| 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 |
-
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Load posts dataset
|
| 15 |
-
posts_df = pd.read_csv("posts_cleaned.csv")
|
| 16 |
-
post_texts = posts_df["post_text"].astype(str).tolist()
|
| 17 |
-
|
| 18 |
-
except Exception as e:
|
| 19 |
-
raise gr.Error(f"Error loading files: {str(e)}")
|
| 20 |
|
| 21 |
-
# Cache embeddings
|
| 22 |
post_embeddings = None
|
| 23 |
|
| 24 |
def get_embeddings():
|
| 25 |
global post_embeddings
|
| 26 |
if post_embeddings is None:
|
| 27 |
-
print("Computing embeddings
|
| 28 |
post_embeddings = model.encode(post_texts, convert_to_tensor=False)
|
| 29 |
-
print("Embeddings computed!")
|
| 30 |
return post_embeddings
|
| 31 |
|
| 32 |
-
# Prediction function
|
| 33 |
def recommend_from_input(user_text):
|
| 34 |
if not user_text.strip():
|
| 35 |
return []
|
| 36 |
|
| 37 |
-
# Get embeddings (computes only once)
|
| 38 |
embeddings = get_embeddings()
|
| 39 |
-
|
| 40 |
-
# Encode user input
|
| 41 |
user_vec = model.encode([user_text])
|
| 42 |
-
|
| 43 |
-
# Calculate similarities
|
| 44 |
sims = cosine_similarity(user_vec, embeddings)[0]
|
| 45 |
-
top_idxs = sims.argsort()[-5:][::-1]
|
| 46 |
-
|
| 47 |
-
# Return as list of strings
|
| 48 |
return posts_df.iloc[top_idxs]["post_text"].tolist()
|
| 49 |
|
| 50 |
-
# Gradio Interface
|
| 51 |
interface = gr.Interface(
|
| 52 |
fn=recommend_from_input,
|
| 53 |
-
inputs=gr.Textbox(label="What are you interested in?"
|
| 54 |
-
outputs=gr.Dataframe(
|
| 55 |
-
|
| 56 |
-
datatype=["str"],
|
| 57 |
-
col_count=(1, "fixed")
|
| 58 |
-
),
|
| 59 |
-
title="🔍 AI Content Recommender",
|
| 60 |
-
description="Enter a topic or interest to get personalized post recommendations",
|
| 61 |
-
examples=[
|
| 62 |
-
["Blockchain scalability solutions"],
|
| 63 |
-
["Latest breakthroughs in AI"],
|
| 64 |
-
["How to write smart contracts"]
|
| 65 |
-
],
|
| 66 |
-
allow_flagging="never"
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Launch with queue for stability
|
| 70 |
interface.launch(share=False)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
|
| 7 |
+
# ✅ Correct way to load the model
|
| 8 |
+
model = SentenceTransformer("recommender") # Replace with your model name/path
|
| 9 |
+
|
| 10 |
+
# Load posts dataset
|
| 11 |
+
posts_df = pd.read_csv("posts_cleaned.csv")
|
| 12 |
+
post_texts = posts_df["post_text"].astype(str).tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Cache embeddings
|
| 15 |
post_embeddings = None
|
| 16 |
|
| 17 |
def get_embeddings():
|
| 18 |
global post_embeddings
|
| 19 |
if post_embeddings is None:
|
| 20 |
+
print("Computing embeddings...")
|
| 21 |
post_embeddings = model.encode(post_texts, convert_to_tensor=False)
|
|
|
|
| 22 |
return post_embeddings
|
| 23 |
|
|
|
|
| 24 |
def recommend_from_input(user_text):
|
| 25 |
if not user_text.strip():
|
| 26 |
return []
|
| 27 |
|
|
|
|
| 28 |
embeddings = get_embeddings()
|
|
|
|
|
|
|
| 29 |
user_vec = model.encode([user_text])
|
|
|
|
|
|
|
| 30 |
sims = cosine_similarity(user_vec, embeddings)[0]
|
| 31 |
+
top_idxs = sims.argsort()[-5:][::-1]
|
|
|
|
|
|
|
| 32 |
return posts_df.iloc[top_idxs]["post_text"].tolist()
|
| 33 |
|
|
|
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=recommend_from_input,
|
| 36 |
+
inputs=gr.Textbox(label="What are you interested in?"),
|
| 37 |
+
outputs=gr.Dataframe(headers=["Recommended Posts"]),
|
| 38 |
+
title="🔍 AI Content Recommender"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
interface.launch(share=False)
|