yoniif's picture
Update app.py
764e3a3 verified
import pandas as pd
import gradio as gr
from sentence_transformers import SentenceTransformer, util
# Load merged influencer dataset
# Columns: Rank, Name, Followers, ER, Country, Niche, Reach, Source File, Source Path
df = pd.read_csv("top_100_influencers_combined.csv")
df.fillna("", inplace=True)
# Extract platform name from Source File (e.g., 'youtube_data_greece.csv' -> 'Youtube')
df['Platform'] = df['Source File'].str.split('_').str[0].str.capitalize()
# Prepare text for embedding (include platform)
profile_fields = ["Name", "Platform", "Niche", "Country"]
df["profile_text"] = df[profile_fields].agg(" - ".join, axis=1)
# Load embedding model
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
influencer_embeddings = model.encode(df["profile_text"].tolist(), convert_to_tensor=True)
# Recommendation logic: find top 3 by cosine similarity
def recommend_influencers(brand_description):
query_embedding = model.encode(brand_description, convert_to_tensor=True)
cosine_scores = util.pytorch_cos_sim(query_embedding, influencer_embeddings)[0]
top_indices = cosine_scores.topk(3).indices.tolist()
recs = []
for idx in top_indices:
row = df.iloc[idx]
recs.append({
"Name": row["Name"],
"Platform": row.get("Platform", ""),
"Niche": row["Niche"],
"Country": row["Country"],
"ER": row.get("ER", "N/A"),
"Followers": row["Followers"],
"Reach": row.get("Reach", "")
})
return recs
# Format recommendations into styled HTML cards
def format_output(brand_input):
recs = recommend_influencers(brand_input)
html = ""
for i, rec in enumerate(recs, 1):
html += f"""
<div style='background:#ffffff; padding:1em; margin-bottom:1em; border-radius:8px; box-shadow:0 2px 6px rgba(0,0,0,0.1);'>
<h3 style='margin:0; color:#0a1f44;'>🎯 {i}. {rec['Name']} <span style='font-size:0.9em; color:#555;'>({rec['Platform']})</span></h3>
<p style='margin:0.5em 0;'><strong>Niche:</strong> {rec['Niche']}</p>
<p style='margin:0.5em 0;'><strong>Country:</strong> {rec['Country']}</p>
<p style='margin:0.5em 0;'><strong>Engagement:</strong> {rec['ER']}</p>
<p style='margin:0.5em 0;'><strong>Followers:</strong> {rec['Followers']}</p>
{f"<p style='margin:0.5em 0;'><strong>Reach:</strong> {rec['Reach']}</p>" if rec['Reach'] else ""}
</div>
"""
return html
# Build the Gradio interface
iface = gr.Interface(
fn=format_output,
inputs=gr.Textbox(
lines=3,
label="πŸ—£οΈ Describe Your Campaign or Brand",
placeholder="e.g., Targeted fitness brand outreach for Gen Z"
),
outputs=gr.HTML(
label="πŸ“ˆ Recommended Influencers"
),
title="πŸ’‘ Targeted Influencer Discovery for Social Media Marketing",
description=(
"Enhance your social media marketing by pinpointing the perfect influencers for your niche.\n\n"
"πŸ› οΈ AI-driven matching based on niche, audience, and engagement metrics β€” get top 3 influencer recommendations instantly."
),
article=(
"**Project:** AI-Powered Influencer Recommender for Social Media Marketing\n"
"**Model:** sentence-transformers/all-MiniLM-L6-v2 for semantic embeddings\n"
"**Dataset:** Top 100 Social Media Influencers 2024 (Countrywise) from Kaggle"
),
examples=[
["Sustainable fashion campaign targeting eco-conscious millennials"],
["Tech gadget launch aimed at early adopters in the US"],
["Healthy snack brand outreach for fitness enthusiasts"],
["Luxury travel experiences for affluent couples in Europe"]
],
theme="soft",
flagging_mode="never"
)
if __name__ == "__main__":
iface.launch(share=True)