MalikSahib1's picture
Create app.py
eca65c3 verified
import gradio as gr
import joblib
import pandas as pd
from huggingface_hub import hf_hub_download
# --- CHANGE THIS ---
YOUR_HF_USERNAME = "maliksahib1"
# -------------------
# 1. Load the Model from the Hugging Face Hub
# This will download our trained model file into the app's environment
try:
model_path = hf_hub_download(
repo_id=f"{YOUR_HF_USERNAME}/tiktok-transparency-tool",
filename="tiktok_fake_follower_model.joblib"
)
model = joblib.load(model_path)
except Exception as e:
# If the model fails to load, show an error message in the app
# This helps in debugging if the username or repo name is wrong
raise gr.Error(f"Failed to load the model. Please check your Hugging Face repo and username. Error: {e}")
# 2. This is the main function that runs when the user clicks "Analyze"
def analyze_profile(username):
if not username:
return "Please enter a TikTok username.", None
# --- THIS IS WHERE THE MAGIC HAPPENS ---
# In a real app, you would scrape TikTok for data here.
# For this demo, we use FAKE data to show how the model works.
# This ensures our app works without a complex scraper.
print(f"Analyzing {username}...")
# MOCK DATA: A list of fake follower profiles to analyze
mock_follower_data = [
{'handle': '@user198237912', 'video_count': 0, 'follower_count': 5, 'following_count': 1500, 'has_bio': 0, 'is_generic_username': 1},
{'handle': '@user987654321', 'video_count': 0, 'follower_count': 2, 'following_count': 500, 'has_bio': 0, 'is_generic_username': 1},
{'handle': '@real_creator_from_demo', 'video_count': 50, 'follower_count': 15000, 'following_count': 200, 'has_bio': 1, 'is_generic_username': 0},
{'handle': '@another_creator_demo', 'video_count': 120, 'follower_count': 250000, 'following_count': 350, 'has_bio': 1, 'is_generic_username': 0},
{'handle': '@suspicious_but_not_generic', 'video_count': 1, 'follower_count': 12, 'following_count': 800, 'has_bio': 0, 'is_generic_username': 0},
{'handle': '@another_bot_account_123', 'video_count': 0, 'follower_count': 1, 'following_count': 100, 'has_bio': 0, 'is_generic_username': 0},
]
# Convert the fake data into a format our model understands
df = pd.DataFrame(mock_follower_data)
features = ['video_count', 'follower_count', 'following_count', 'has_bio', 'is_generic_username']
X_predict = df[features]
# Use the model to predict the probability of being fake (1)
predictions_proba = model.predict_proba(X_predict)[:, 1]
df['suspicion_score'] = predictions_proba
# 3. Generate a user-friendly report
total_followers_analyzed = len(df)
# We'll flag accounts with a suspicion score > 70%
suspicious_followers = df[df['suspicion_score'] > 0.70]
num_suspicious = len(suspicious_followers)
suspicion_rate = (num_suspicious / total_followers_analyzed) * 100
report_text = f"""
### Analysis Report for @{username}
- **Followers Analyzed:** {total_followers_analyzed} (Using sample data for this demo)
- **Highly Suspicious Followers Found:** {num_suspicious}
- **Overall Suspicion Rate:** **{suspicion_rate:.1f}%**
*This analysis is for educational purposes and uses pre-loaded sample data, not live data from TikTok.*
"""
# Create a table to show the most suspicious accounts
output_df = suspicious_followers[['handle', 'suspicion_score']]
output_df = output_df.sort_values(by='suspicion_score', ascending=False).reset_index(drop=True)
return report_text, output_df
# 4. Create the Gradio web interface
iface = gr.Interface(
fn=analyze_profile,
inputs=gr.Textbox(label="TikTok Username", placeholder="e.g., charlidamelio"),
outputs=[
gr.Markdown(label="Analysis Summary"),
gr.DataFrame(label="Top Suspicious Follower Accounts (from sample)")
],
title="TikTok Transparency Tool (T3)",
description="Enter a TikTok username to analyze a sample of its followers. This tool uses a machine learning model to detect suspicious accounts based on public profile metrics.",
allow_flagging="never"
)
# Launch the app!
iface.launch()