|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import joblib |
|
|
import matplotlib.pyplot as plt |
|
|
import io |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model = joblib.load("influencer_model.pkl") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def predict_influencers(file): |
|
|
|
|
|
data = pd.read_excel(file) |
|
|
|
|
|
|
|
|
for col in ['likes_count', 'shares_count', 'comments_count', |
|
|
'degree_centrality', 'betweenness_centrality', 'closeness_centrality']: |
|
|
if col not in data.columns: |
|
|
data[col] = 0 |
|
|
|
|
|
feature_cols = ['degree_centrality', 'betweenness_centrality', 'closeness_centrality', |
|
|
'likes_count', 'shares_count', 'comments_count'] |
|
|
X_new = data[feature_cols] |
|
|
|
|
|
|
|
|
data['predicted_label'] = model.predict(X_new) |
|
|
data['influence_score'] = model.predict_proba(X_new)[:, 1] |
|
|
|
|
|
|
|
|
top_influencers = data.sort_values(by='influence_score', ascending=False).head(10) |
|
|
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(8, 4)) |
|
|
ax.hist(data['influence_score'], bins=20, color='skyblue', edgecolor='black') |
|
|
ax.set_xlabel("Influence Score") |
|
|
ax.set_ylabel("Number of Users") |
|
|
ax.set_title("Distribution of Influence Scores") |
|
|
buf = io.BytesIO() |
|
|
plt.savefig(buf, format="png") |
|
|
buf.seek(0) |
|
|
|
|
|
|
|
|
total_users = len(data) |
|
|
high_influencers = data['predicted_label'].sum() |
|
|
summary = f"Total Users: {total_users}\nPredicted Influential Users: {high_influencers}\nInfluence Ratio: {(high_influencers/total_users)*100:.2f}%" |
|
|
|
|
|
return top_influencers[['user_id','likes_count','shares_count','comments_count','influence_score']], buf, summary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=predict_influencers, |
|
|
inputs=gr.File(label="Upload Facebook Dataset (.xlsx)"), |
|
|
outputs=[ |
|
|
gr.Dataframe(headers=["user_id","likes_count","shares_count","comments_count","influence_score"], label="Top 10 Influential Users"), |
|
|
gr.Image(type="pil", label="Influence Score Distribution"), |
|
|
gr.Textbox(label="Summary Statistics") |
|
|
], |
|
|
title="๐ Facebook Influential User Detection", |
|
|
description="Automatically predicts influential Facebook users based on likes, comments, and shares." |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |
|
|
|