Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# --- CHANGE THIS ---
|
| 7 |
+
YOUR_HF_USERNAME = "maliksahib1"
|
| 8 |
+
# -------------------
|
| 9 |
+
|
| 10 |
+
# 1. Load the Model from the Hugging Face Hub
|
| 11 |
+
# This will download our trained model file into the app's environment
|
| 12 |
+
try:
|
| 13 |
+
model_path = hf_hub_download(
|
| 14 |
+
repo_id=f"{YOUR_HF_USERNAME}/tiktok-transparency-tool",
|
| 15 |
+
filename="tiktok_fake_follower_model.joblib"
|
| 16 |
+
)
|
| 17 |
+
model = joblib.load(model_path)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
# If the model fails to load, show an error message in the app
|
| 20 |
+
# This helps in debugging if the username or repo name is wrong
|
| 21 |
+
raise gr.Error(f"Failed to load the model. Please check your Hugging Face repo and username. Error: {e}")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# 2. This is the main function that runs when the user clicks "Analyze"
|
| 25 |
+
def analyze_profile(username):
|
| 26 |
+
if not username:
|
| 27 |
+
return "Please enter a TikTok username.", None
|
| 28 |
+
|
| 29 |
+
# --- THIS IS WHERE THE MAGIC HAPPENS ---
|
| 30 |
+
# In a real app, you would scrape TikTok for data here.
|
| 31 |
+
# For this demo, we use FAKE data to show how the model works.
|
| 32 |
+
# This ensures our app works without a complex scraper.
|
| 33 |
+
print(f"Analyzing {username}...")
|
| 34 |
+
|
| 35 |
+
# MOCK DATA: A list of fake follower profiles to analyze
|
| 36 |
+
mock_follower_data = [
|
| 37 |
+
{'handle': '@user198237912', 'video_count': 0, 'follower_count': 5, 'following_count': 1500, 'has_bio': 0, 'is_generic_username': 1},
|
| 38 |
+
{'handle': '@user987654321', 'video_count': 0, 'follower_count': 2, 'following_count': 500, 'has_bio': 0, 'is_generic_username': 1},
|
| 39 |
+
{'handle': '@real_creator_from_demo', 'video_count': 50, 'follower_count': 15000, 'following_count': 200, 'has_bio': 1, 'is_generic_username': 0},
|
| 40 |
+
{'handle': '@another_creator_demo', 'video_count': 120, 'follower_count': 250000, 'following_count': 350, 'has_bio': 1, 'is_generic_username': 0},
|
| 41 |
+
{'handle': '@suspicious_but_not_generic', 'video_count': 1, 'follower_count': 12, 'following_count': 800, 'has_bio': 0, 'is_generic_username': 0},
|
| 42 |
+
{'handle': '@another_bot_account_123', 'video_count': 0, 'follower_count': 1, 'following_count': 100, 'has_bio': 0, 'is_generic_username': 0},
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# Convert the fake data into a format our model understands
|
| 46 |
+
df = pd.DataFrame(mock_follower_data)
|
| 47 |
+
features = ['video_count', 'follower_count', 'following_count', 'has_bio', 'is_generic_username']
|
| 48 |
+
X_predict = df[features]
|
| 49 |
+
|
| 50 |
+
# Use the model to predict the probability of being fake (1)
|
| 51 |
+
predictions_proba = model.predict_proba(X_predict)[:, 1]
|
| 52 |
+
df['suspicion_score'] = predictions_proba
|
| 53 |
+
|
| 54 |
+
# 3. Generate a user-friendly report
|
| 55 |
+
total_followers_analyzed = len(df)
|
| 56 |
+
# We'll flag accounts with a suspicion score > 70%
|
| 57 |
+
suspicious_followers = df[df['suspicion_score'] > 0.70]
|
| 58 |
+
num_suspicious = len(suspicious_followers)
|
| 59 |
+
suspicion_rate = (num_suspicious / total_followers_analyzed) * 100
|
| 60 |
+
|
| 61 |
+
report_text = f"""
|
| 62 |
+
### Analysis Report for @{username}
|
| 63 |
+
- **Followers Analyzed:** {total_followers_analyzed} (Using sample data for this demo)
|
| 64 |
+
- **Highly Suspicious Followers Found:** {num_suspicious}
|
| 65 |
+
- **Overall Suspicion Rate:** **{suspicion_rate:.1f}%**
|
| 66 |
+
|
| 67 |
+
*This analysis is for educational purposes and uses pre-loaded sample data, not live data from TikTok.*
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
# Create a table to show the most suspicious accounts
|
| 71 |
+
output_df = suspicious_followers[['handle', 'suspicion_score']]
|
| 72 |
+
output_df = output_df.sort_values(by='suspicion_score', ascending=False).reset_index(drop=True)
|
| 73 |
+
|
| 74 |
+
return report_text, output_df
|
| 75 |
+
|
| 76 |
+
# 4. Create the Gradio web interface
|
| 77 |
+
iface = gr.Interface(
|
| 78 |
+
fn=analyze_profile,
|
| 79 |
+
inputs=gr.Textbox(label="TikTok Username", placeholder="e.g., charlidamelio"),
|
| 80 |
+
outputs=[
|
| 81 |
+
gr.Markdown(label="Analysis Summary"),
|
| 82 |
+
gr.DataFrame(label="Top Suspicious Follower Accounts (from sample)")
|
| 83 |
+
],
|
| 84 |
+
title="TikTok Transparency Tool (T3)",
|
| 85 |
+
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.",
|
| 86 |
+
allow_flagging="never"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Launch the app!
|
| 90 |
+
iface.launch()
|