Ravishankarsharma's picture
Update app.py
045595d verified
# ============================================================
# Title: Influential Facebook User Detection (Gradio - Hugging Face)
# Author: Ravi Shankar Sharma
# ============================================================
import gradio as gr
import pandas as pd
import joblib
import matplotlib.pyplot as plt
import io
# ------------------------------------------------------------
# Load Pretrained Model
# ------------------------------------------------------------
model = joblib.load("influencer_model.pkl") # Updated model file name
# ------------------------------------------------------------
# Prediction Function
# ------------------------------------------------------------
def predict_influencers(file):
# Read uploaded Excel file
data = pd.read_excel(file)
# Ensure required columns exist
for col in ['likes_count', 'shares_count', 'comments_count',
'degree_centrality', 'betweenness_centrality', 'closeness_centrality']:
if col not in data.columns:
data[col] = 0 # fill missing features with 0
feature_cols = ['degree_centrality', 'betweenness_centrality', 'closeness_centrality',
'likes_count', 'shares_count', 'comments_count']
X_new = data[feature_cols]
# Predict influence
data['predicted_label'] = model.predict(X_new)
data['influence_score'] = model.predict_proba(X_new)[:, 1]
# Top 10 influencers
top_influencers = data.sort_values(by='influence_score', ascending=False).head(10)
# Influence histogram
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)
# Summary stats
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
# ------------------------------------------------------------
# Gradio Interface
# ------------------------------------------------------------
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."
)
# Launch the app
iface.launch()