yoniif commited on
Commit
03f7cd5
·
verified ·
1 Parent(s): 1c445b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -30
app.py CHANGED
@@ -2,69 +2,85 @@ import pandas as pd
2
  import gradio as gr
3
  from sentence_transformers import SentenceTransformer, util
4
 
5
- # Load the CSV you uploaded into the Space
 
 
6
  df = pd.read_csv("top_100_influencers_combined.csv")
7
  df.fillna("", inplace=True)
8
 
 
 
 
 
9
 
10
- # add platform?
11
- df["profile_text"] = df["Name"] + " - " + df["Niche"] + " - " + df["Country"]
12
-
13
  model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
14
  influencer_embeddings = model.encode(df["profile_text"].tolist(), convert_to_tensor=True)
15
 
 
 
16
  def recommend_influencers(brand_description):
17
  query_embedding = model.encode(brand_description, convert_to_tensor=True)
18
  cosine_scores = util.pytorch_cos_sim(query_embedding, influencer_embeddings)[0]
19
  top_indices = cosine_scores.topk(3).indices.tolist()
20
 
21
- recommendations = []
22
  for idx in top_indices:
23
  row = df.iloc[idx]
24
- recommendations.append({
25
- "Influencer": row["Name"],
 
26
  "Niche": row["Niche"],
27
  "Country": row["Country"],
28
- "Engagement Rate": row.get("ER", "N/A"),
29
- "Followers": row.get("Followers", "N/A")
 
30
  })
31
- return recommendations
 
 
32
 
33
  def format_output(brand_input):
34
  recs = recommend_influencers(brand_input)
35
- output = ""
36
  for i, rec in enumerate(recs, 1):
37
- output += f"### {i}. {rec['Name']})\n"
38
- output += f"- Niche: {rec['Niche']}\n"
39
- output += f"- Country: {rec['Country']}\n"
40
- output += f"- Engagement Rate: {rec['ER']}\n"
41
- output += f"- Followers: {rec['Followers']}\n\n"
42
- return output
 
 
 
 
 
43
 
44
- # Build the interface
45
  demo = gr.Interface(
46
  fn=format_output,
47
  inputs=gr.Textbox(
48
  label="Describe your brand or campaign",
49
  placeholder="e.g. Sustainable fashion brand targeting Gen Z in the US",
50
- lines=3
 
51
  ),
52
- outputs=gr.HTML(),
53
  title="InfluMatch – AI-Powered Influencer Recommender",
54
  description="""
55
- <div style='font-size: 1.1em; color: #333;'>
56
- <p><strong>InfluMatch</strong> helps you discover the perfect influencers for your brand or campaign using state-of-the-art AI.</p>
57
- <p>Just describe your brand, and we'll instantly recommend three top influencers who match your niche, audience, and goals.</p>
58
  </div>
59
  """,
 
60
  examples=[
61
- ["Skincare brand for Gen Z women in the US"],
62
- ["Luxury watches targeting high-net-worth individuals in Europe"],
63
- ["Eco-friendly fitness gear for millennials"],
64
- ["Pet food brand seeking TikTok creators in Canada"]
65
- ],
66
- theme="default"
67
  )
68
 
69
  if __name__ == "__main__":
70
- demo.launch()
 
2
  import gradio as gr
3
  from sentence_transformers import SentenceTransformer, util
4
 
5
+ # Load merged influencer dataset
6
+ # Columns: Rank, Name, Followers, ER, Country, Niche, Reach, Source File, Source Path
7
+
8
  df = pd.read_csv("top_100_influencers_combined.csv")
9
  df.fillna("", inplace=True)
10
 
11
+ # Prepare text for embedding
12
+ # You can include 'Reach' or other metrics as needed
13
+ profile_fields = ["Name", "Niche", "Country"]
14
+ df["profile_text"] = df[profile_fields].agg(" - ".join, axis=1)
15
 
16
+ # Load embedding model
 
 
17
  model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
18
  influencer_embeddings = model.encode(df["profile_text"].tolist(), convert_to_tensor=True)
19
 
20
+ # Recommendation logic: find top 3 by cosine similarity
21
+
22
  def recommend_influencers(brand_description):
23
  query_embedding = model.encode(brand_description, convert_to_tensor=True)
24
  cosine_scores = util.pytorch_cos_sim(query_embedding, influencer_embeddings)[0]
25
  top_indices = cosine_scores.topk(3).indices.tolist()
26
 
27
+ recs = []
28
  for idx in top_indices:
29
  row = df.iloc[idx]
30
+ recs.append({
31
+ "Name": row["Name"],
32
+ "Platform": row.get("Platform", ""),
33
  "Niche": row["Niche"],
34
  "Country": row["Country"],
35
+ "ER": row.get("ER", "N/A"),
36
+ "Followers": row["Followers"],
37
+ "Reach": row.get("Reach", "")
38
  })
39
+ return recs
40
+
41
+ # Format recommendations into styled HTML cards
42
 
43
  def format_output(brand_input):
44
  recs = recommend_influencers(brand_input)
45
+ html = ""
46
  for i, rec in enumerate(recs, 1):
47
+ html += f"""
48
+ <div style='padding:1em; margin-bottom:1em; border:1px solid #e0e0e0; border-radius:8px; background:#fff;'>
49
+ <h3 style='margin:0 0 .5em;'>{i}. {rec['Name']} <span style='font-size:0.8em; color:#666;'>({rec['Platform']})</span></h3>
50
+ <p style='margin:0.2em 0;'><strong>Niche:</strong> {rec['Niche']}</p>
51
+ <p style='margin:0.2em 0;'><strong>Country:</strong> {rec['Country']}</p>
52
+ <p style='margin:0.2em 0;'><strong>Engagement Rate:</strong> {rec['ER']}</p>
53
+ <p style='margin:0.2em 0;'><strong>Followers:</strong> {rec['Followers']}</p>
54
+ {f"<p style='margin:0.2em 0;'><strong>Reach:</strong> {rec['Reach']}</p>" if rec['Reach'] else ""}
55
+ </div>
56
+ """
57
+ return html
58
 
59
+ # Build the Gradio interface
60
  demo = gr.Interface(
61
  fn=format_output,
62
  inputs=gr.Textbox(
63
  label="Describe your brand or campaign",
64
  placeholder="e.g. Sustainable fashion brand targeting Gen Z in the US",
65
+ lines=3,
66
+ elem_id="input-box"
67
  ),
68
+ outputs=gr.HTML(elem_id="output-cards"),
69
  title="InfluMatch – AI-Powered Influencer Recommender",
70
  description="""
71
+ <div style='max-width:600px; margin-bottom:1em; color:#333;'>
72
+ <p><strong>InfluMatch</strong> helps you discover the ideal influencers for your brand using AI-driven similarity matching.</p>
73
+ <p>Simply enter a short description of your brand or campaign to get three top influencer recommendations.</p>
74
  </div>
75
  """,
76
+ theme="default",
77
  examples=[
78
+ ["Skincare brand for Gen Z women in the US"],
79
+ ["High-end travel gear targeting adventure enthusiasts in Europe"],
80
+ ["Eco-friendly fitness apparel for millennials"],
81
+ ["Gourmet pet food brand seeking Instagram influencers in Canada"]
82
+ ]
 
83
  )
84
 
85
  if __name__ == "__main__":
86
+ demo.launch(share=True)