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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Function to calculate the follower increase from the CSV
|
| 6 |
+
def calculate_follower_increase():
|
| 7 |
+
csv_path = "Followers.csv" # Use the hardcoded path directly
|
| 8 |
+
df = pd.read_csv(csv_path)
|
| 9 |
+
|
| 10 |
+
# Ensure proper column naming and strip out any extra spaces
|
| 11 |
+
df.columns = df.columns.str.strip()
|
| 12 |
+
|
| 13 |
+
# Sort by date to get the two most recent entries
|
| 14 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 15 |
+
df = df.sort_values(by='Date', ascending=False)
|
| 16 |
+
|
| 17 |
+
# Get the two most recent entries
|
| 18 |
+
recent_data = df.head(2)
|
| 19 |
+
|
| 20 |
+
# Calculate the differences for each platform
|
| 21 |
+
tiktok_increase = recent_data.iloc[0]['TikTok Followers'] - recent_data.iloc[1]['TikTok Followers']
|
| 22 |
+
facebook_increase = recent_data.iloc[0]['Facebook Followers'] - recent_data.iloc[1]['Facebook Followers']
|
| 23 |
+
|
| 24 |
+
# Calculate the total increase across platforms
|
| 25 |
+
total_increase = tiktok_increase + facebook_increase
|
| 26 |
+
|
| 27 |
+
return f"Leonardo got you {int(total_increase)} more followers across TikTok & Facebook!"
|
| 28 |
+
|
| 29 |
+
# Function to calculate the engagement increase from the CSV
|
| 30 |
+
def calculate_engagement_increase():
|
| 31 |
+
csv_path = "Total Engagement.csv" # Path to the uploaded CSV
|
| 32 |
+
df = pd.read_csv(csv_path)
|
| 33 |
+
|
| 34 |
+
# Ensure proper column naming and strip out any extra spaces
|
| 35 |
+
df.columns = df.columns.str.strip()
|
| 36 |
+
|
| 37 |
+
# Sort by date to get the two most recent entries
|
| 38 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 39 |
+
df = df.sort_values(by='Date', ascending=False)
|
| 40 |
+
|
| 41 |
+
# Get the two most recent entries
|
| 42 |
+
recent_data = df.head(2)
|
| 43 |
+
|
| 44 |
+
# Calculate the difference in total engagement
|
| 45 |
+
engagement_increase = recent_data.iloc[0]['Total Engagement Across Our Posts'] - recent_data.iloc[1]['Total Engagement Across Our Posts']
|
| 46 |
+
|
| 47 |
+
return f"Leonardo got you {int(engagement_increase)} more engagements across TikTok & Facebook!"
|
| 48 |
+
|
| 49 |
+
# Function to extract the prediction label from filenames
|
| 50 |
+
def extract_prediction_label_from_masterpiece(filename):
|
| 51 |
+
return filename.split('_')[-1].replace('.mp4', '')
|
| 52 |
+
|
| 53 |
+
# Function to highlight the quintile based on prediction label for the masterpiece
|
| 54 |
+
def highlight_masterpiece_quintile(fig, actual_outcome, x_max):
|
| 55 |
+
label_map = {"normal": 1, "good": 2, "very good": 3, "exceptional": 4, "viral": 5}
|
| 56 |
+
quintile = label_map.get(actual_outcome.lower(), 1)
|
| 57 |
+
|
| 58 |
+
# Dynamically adjust the x0 and x1 ranges based on x_max
|
| 59 |
+
quintile_width = x_max / 5 # Divide x_max into 5 quintiles
|
| 60 |
+
x0 = (quintile - 1) * quintile_width
|
| 61 |
+
x1 = quintile * quintile_width
|
| 62 |
+
|
| 63 |
+
# Add a shaded region in the graph corresponding to the quintile
|
| 64 |
+
fig.add_vrect(
|
| 65 |
+
x0=x0, x1=x1,
|
| 66 |
+
fillcolor="green", opacity=0.25, line_width=0,
|
| 67 |
+
annotation_text=f"Actual Outcome: {actual_outcome.capitalize()}"
|
| 68 |
+
)
|
| 69 |
return fig
|
| 70 |
|
| 71 |
+
# Function to load and plot the masterpiece data (video and graph)
|
| 72 |
+
def load_masterpiece_video_and_plot():
|
| 73 |
+
masterpiece_video_path = "starperformer_viral.mp4"
|
| 74 |
+
|
| 75 |
+
# Replace [actual outcome] with the correct label
|
| 76 |
+
actual_outcome = extract_prediction_label_from_masterpiece(masterpiece_video_path)
|
| 77 |
+
|
| 78 |
+
# Load the data for the TikTok graph (simulated example)
|
| 79 |
+
tiktok_csv_path = "tiktok_histogram.csv"
|
| 80 |
+
df = pd.read_csv(tiktok_csv_path)
|
| 81 |
+
|
| 82 |
+
# Assuming the CSV has columns 'Engagement' and 'Density'
|
| 83 |
+
x_data = df['Engagement']
|
| 84 |
+
y_data = df['Density']
|
| 85 |
+
|
| 86 |
+
# Get x_max for the quintile calculation
|
| 87 |
+
x_max = max(x_data)
|
| 88 |
+
|
| 89 |
+
# Create the plot with actual data
|
| 90 |
+
fig = go.Figure()
|
| 91 |
+
fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='lines', name="Actual Data"))
|
| 92 |
+
|
| 93 |
+
# Highlight the quintile based on the actual outcome
|
| 94 |
+
fig = highlight_masterpiece_quintile(fig, actual_outcome, x_max)
|
| 95 |
+
|
| 96 |
+
# Return the video path and graph
|
| 97 |
+
return masterpiece_video_path, fig
|
| 98 |
|
| 99 |
+
# Create the Gradio interface
|
| 100 |
+
with gr.Blocks() as dashboard:
|
| 101 |
+
|
| 102 |
+
# Section for Leonardo's Last-Week Masterpiece
|
| 103 |
+
gr.Markdown("## Leonardo's Last-Week Masterpiece")
|
| 104 |
+
with gr.Row():
|
| 105 |
+
masterpiece_video, masterpiece_graph = load_masterpiece_video_and_plot()
|
| 106 |
+
gr.Video(masterpiece_video, label="Star Performer TikTok Video")
|
| 107 |
+
gr.Plot(masterpiece_graph, label="Star Performer TikTok Performance")
|
| 108 |
+
|
| 109 |
+
# Add a header for follower impact
|
| 110 |
+
gr.Markdown("## Leonardo's Follower Impact")
|
| 111 |
+
|
| 112 |
+
# Create the textbox for follower count (initial message)
|
| 113 |
+
follower_text = gr.Textbox(label="Follower Update", value="Click the button to see the increase.", interactive=False)
|
| 114 |
+
|
| 115 |
+
# Add the button and set it to update the follower count
|
| 116 |
+
with gr.Row():
|
| 117 |
+
gr.Button("See New Followers").click(fn=calculate_follower_increase, inputs=[], outputs=[follower_text])
|
| 118 |
+
|
| 119 |
+
# Add a header for engagement impact
|
| 120 |
+
gr.Markdown("## Leonardo's Engagement Impact")
|
| 121 |
+
|
| 122 |
+
# Create the textbox for engagement count (initial message)
|
| 123 |
+
engagement_text = gr.Textbox(label="Engagement Update", value="Click the button to see the increase.", interactive=False)
|
| 124 |
+
|
| 125 |
+
# Add the button and set it to update the engagement count
|
| 126 |
with gr.Row():
|
| 127 |
+
gr.Button("See New Engagement").click(fn=calculate_engagement_increase, inputs=[], outputs=[engagement_text])
|
| 128 |
+
|
| 129 |
+
# Add the main title
|
| 130 |
+
gr.Markdown("# Leonardo's Plans for This Week")
|
| 131 |
+
|
| 132 |
+
# Section for TikTok projections (Videos)
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column():
|
| 135 |
+
video_path_1, graph_1 = load_data_and_plot("video", "TikTok1", tiktok_csv_path, video_folder)
|
| 136 |
+
if video_path_1:
|
| 137 |
+
gr.Video(video_path_1, label="TikTok 1 Video")
|
| 138 |
+
gr.Plot(graph_1, label="TikTok 1 Performance")
|
| 139 |
+
|
| 140 |
+
with gr.Column():
|
| 141 |
+
video_path_2, graph_2 = load_data_and_plot("video", "TikTok2", tiktok_csv_path, video_folder)
|
| 142 |
+
if video_path_2:
|
| 143 |
+
gr.Video(video_path_2, label="TikTok 2 Video")
|
| 144 |
+
gr.Plot(graph_2, label="TikTok 2 Performance")
|
| 145 |
+
|
| 146 |
+
with gr.Column():
|
| 147 |
+
video_path_3, graph_3 = load_data_and_plot("video", "TikTok3", tiktok_csv_path, video_folder)
|
| 148 |
+
if video_path_3:
|
| 149 |
+
gr.Video(video_path_3, label="TikTok 3 Video")
|
| 150 |
+
gr.Plot(graph_3, label="TikTok 3 Performance")
|
| 151 |
+
|
| 152 |
+
# Section for Facebook projections (Statics)
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column():
|
| 155 |
+
image_path_1, graph_4 = load_data_and_plot("image", "Static1", facebook_csv_path, image_folder)
|
| 156 |
+
if image_path_1:
|
| 157 |
+
gr.Image(image_path_1, label="Facebook Static 1")
|
| 158 |
+
gr.Plot(graph_4, label="Facebook Static 1 Performance")
|
| 159 |
+
|
| 160 |
+
with gr.Column():
|
| 161 |
+
image_path_2, graph_5 = load_data_and_plot("image", "Static2", facebook_csv_path, image_folder)
|
| 162 |
+
if image_path_2:
|
| 163 |
+
gr.Image(image_path_2, label="Facebook Static 2")
|
| 164 |
+
gr.Plot(graph_5, label="Facebook Static 2 Performance")
|
| 165 |
+
|
| 166 |
+
with gr.Column():
|
| 167 |
+
image_path_3, graph_6 = load_data_and_plot("image", "Static3", facebook_csv_path, image_folder)
|
| 168 |
+
if image_path_3:
|
| 169 |
+
gr.Image(image_path_3, label="Facebook Static 3")
|
| 170 |
+
gr.Plot(graph_6, label="Facebook Static 3 Performance")
|
| 171 |
|
| 172 |
+
# Launch the dashboard
|
| 173 |
+
dashboard.launch(debug=True)
|