yoniif commited on
Commit
e815416
·
verified ·
1 Parent(s): 21b10e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py CHANGED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🔧 Install dependencies first (uncomment for local testing)
2
+ # !pip install gradio pandas sentence-transformers
3
+
4
+ import gradio as gr
5
+ import pandas as pd
6
+ from sentence_transformers import SentenceTransformer, util
7
+
8
+ # Load influencer dataset (replace path with uploaded HF dataset if needed)
9
+ df = pd.read_csv("top_100_influencers.csv") # <- upload to HF Space alongside this script
10
+
11
+ # Fill NA just in case
12
+ df.fillna("", inplace=True)
13
+
14
+ # Combine fields for embedding
15
+ df["profile_text"] = df["Name"] + " - " + df["Platform"] + " - " + df["Niche"] + " - " + df["Country"]
16
+
17
+ # Load embedding model
18
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
19
+
20
+ # Precompute embeddings
21
+ influencer_embeddings = model.encode(df["profile_text"].tolist(), convert_to_tensor=True)
22
+
23
+ # 🔍 Recommendation Function
24
+ def recommend_influencers(brand_description):
25
+ query_embedding = model.encode(brand_description, convert_to_tensor=True)
26
+ cosine_scores = util.pytorch_cos_sim(query_embedding, influencer_embeddings)[0]
27
+ top_indices = cosine_scores.topk(3).indices.tolist()
28
+
29
+ recommendations = []
30
+ for idx in top_indices:
31
+ row = df.iloc[idx]
32
+ recommendations.append({
33
+ "Influencer": row["Name"],
34
+ "Platform": row["Platform"],
35
+ "Niche": row["Niche"],
36
+ "Country": row["Country"],
37
+ "Engagement Rate": row.get("Engagement Rate", "N/A"),
38
+ "Followers": row.get("Followers", "N/A")
39
+ })
40
+ return recommendations
41
+
42
+ # 🖼️ Gradio UI
43
+ def format_output(brand_input):
44
+ recs = recommend_influencers(brand_input)
45
+ output = ""
46
+ for i, rec in enumerate(recs, 1):
47
+ output += f"### {i}. {rec['Influencer']} ({rec['Platform']})\n"
48
+ output += f"- Niche: {rec['Niche']}\n"
49
+ output += f"- Country: {rec['Country']}\n"
50
+ output += f"- Engagement Rate: {rec['Engagement Rate']}\n"
51
+ output += f"- Followers: {rec['Followers']}\n\n"
52
+ return output
53
+
54
+ demo = gr.Interface(
55
+ fn=format_output,
56
+ inputs=gr.Textbox(label="Enter your brand description (e.g. 'Sustainable fashion for Gen Z')", placeholder="Describe your brand..."),
57
+ outputs=gr.Markdown(label="Top 3 Influencer Matches"),
58
+ title="InfluMatch: Influencer Recommender",
59
+ description="Describe your brand or campaign and get 3 matching influencer suggestions.",
60
+ examples=[
61
+ ["Tech gadgets for millennial men"],
62
+ ["Skincare brand for Gen Z in the US"],
63
+ ["Luxury travel experiences for couples"],
64
+ ["Eco-friendly fashion accessories"]
65
+ ]
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch()