| import gradio as gr |
| import pandas as pd |
| import gradio as gr |
| import plotly.express as px |
| import pandas as pd |
| import numpy as np |
| from scipy.optimize import curve_fit |
| import plotly.graph_objects as go |
| import os |
| import datetime |
| import logging |
|
|
| |
| tiktok_csv_path = "tiktok_histogram.csv" |
| facebook_csv_path = "facebook_histogram.csv" |
| video_folder = "videos" |
| image_folder = "images" |
|
|
| |
| def load_masterpiece_video_and_plot(): |
| masterpiece_video_path = os.path.join("starperformer_normal.mp4") |
|
|
| |
| actual_outcome = "normal" |
|
|
| |
| fig, x_min, x_max, y_min, y_max = load_and_plot_csv(tiktok_csv_path) |
|
|
| |
| fig = highlight_quintile(fig, actual_outcome, x_max) |
|
|
| return masterpiece_video_path, fig |
|
|
|
|
| |
| def extract_prediction_label(filename, media_type): |
| if media_type == "video": |
| return filename.split('_')[3] |
| elif media_type == "image": |
| return filename.split('_')[3] |
|
|
| |
| def extract_version_number(filename, media_type): |
| if media_type == "video": |
| version_with_v = filename.split('_')[1] |
| elif media_type == "image": |
| version_with_v = filename.split('_')[2] |
| |
| version_number = version_with_v[1:] |
| return version_number |
|
|
| |
| def get_highest_prediction_for_media(folder_path, media_type, media_identifier): |
| highest_file = None |
| highest_score = None |
|
|
| |
| for filename in os.listdir(folder_path): |
| if (media_type == "video" and filename.endswith('.mp4')) or (media_type == "image" and filename.endswith('.jpeg')): |
| if media_identifier in filename: |
| |
| prediction_label = extract_prediction_label(filename, media_type) |
|
|
| label_map = {"normal": 1, "good": 2, "very good": 3, "exceptional": 4, "viral": 5} |
| score = label_map.get(prediction_label.lower(), 1) |
|
|
| |
| if highest_score is None or score > highest_score: |
| highest_score = score |
| highest_file = filename |
|
|
| return highest_file |
|
|
| |
| def load_and_plot_csv(csv_path): |
| df = pd.read_csv(csv_path) |
|
|
| |
| x_data = df['Engagement'] |
| y_data = df['Density'] |
|
|
| |
| x_max = max(x_data) |
| y_max = max(y_data) |
|
|
| |
| fig = go.Figure() |
| fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='lines', name="Actual Data")) |
|
|
| return fig, x_data.min(), x_max, y_data.min(), y_max |
|
|
| |
| def highlight_quintile(fig, label, x_max): |
| label_map = {"normal": 1, "good": 2, "very good": 3, "exceptional": 4, "viral": 5} |
| quintile = label_map.get(label.lower(), 1) |
|
|
| |
| quintile_width = x_max / 5 |
| x0 = (quintile - 1) * quintile_width |
| x1 = quintile * quintile_width |
|
|
| |
| fig.add_vrect( |
| x0=x0, x1=x1, |
| fillcolor="green", opacity=0.25, line_width=0, |
| annotation_text=f"Predicted: {label.capitalize()}" |
| ) |
| return fig |
|
|
| |
| def load_data_and_plot(media_type, media_identifier, csv_path, folder_path): |
| |
| media_file = get_highest_prediction_for_media(folder_path, media_type, media_identifier) |
|
|
| |
| if media_file: |
| prediction_label = extract_prediction_label(media_file, media_type) |
| version_number = extract_version_number(media_file, media_type) |
|
|
| |
| media_path = os.path.join(folder_path, media_file) |
|
|
| |
| fig, x_min, x_max, y_min, y_max = load_and_plot_csv(csv_path) |
|
|
| |
| fig = highlight_quintile(fig, prediction_label, x_max) |
|
|
| |
| fig.update_layout( |
| title=f"Predicted Engagement for {media_identifier} (Version {version_number})", |
| xaxis=dict(range=[0, x_max]), |
| yaxis=dict(range=[0, y_max]) |
| ) |
|
|
| return media_path, fig |
| return None, None |
|
|
|
|
|
|
| |
| def calculate_follower_increase(): |
| csv_path = "Followers.csv" |
| df = pd.read_csv(csv_path) |
|
|
| |
| df.columns = df.columns.str.strip() |
|
|
| |
| df['Date'] = pd.to_datetime(df['Date']) |
| df = df.sort_values(by='Date', ascending=False) |
|
|
| |
| recent_data = df.head(2) |
|
|
| |
| tiktok_increase = recent_data.iloc[0]['TikTok Followers'] - recent_data.iloc[1]['TikTok Followers'] |
| facebook_increase = recent_data.iloc[0]['Facebook Followers'] - recent_data.iloc[1]['Facebook Followers'] |
|
|
| |
| total_increase = tiktok_increase + facebook_increase |
|
|
| return f"Leonardo got you {int(total_increase)} more followers across TikTok & Facebook!" |
|
|
| |
| def calculate_engagement_increase(): |
| csv_path = "Total Engagement.csv" |
| df = pd.read_csv(csv_path) |
|
|
| |
| df.columns = df.columns.str.strip() |
|
|
| |
| df['Date'] = pd.to_datetime(df['Date']) |
| df = df.sort_values(by='Date', ascending=False) |
|
|
| |
| recent_data = df.head(2) |
|
|
| |
| engagement_increase = recent_data.iloc[0]['Total Engagement Across Our Posts'] - recent_data.iloc[1]['Total Engagement Across Our Posts'] |
|
|
| return f"Leonardo got you {int(engagement_increase)} more engagements than last week on Instagram!" |
|
|
| |
| with gr.Blocks() as dashboard: |
| |
| gr.Markdown("# Leonardo's Masterpiece from Last Week") |
| |
| |
| masterpiece_video, masterpiece_graph = load_masterpiece_video_and_plot() |
| |
| |
| gr.Video(masterpiece_video, label="Star Performer TikTok Video") |
| |
| |
| gr.Plot(masterpiece_graph, label="Star Performer TikTok Performance") |
| |
| |
| gr.Markdown("## Leonardo's Follower Impact") |
|
|
| |
| follower_text = gr.Textbox(label="Follower Update", value="Click the button to see the increase.", interactive=False) |
|
|
| |
| with gr.Row(): |
| gr.Button("See New Followers").click(fn=calculate_follower_increase, inputs=[], outputs=[follower_text]) |
|
|
| |
| gr.Markdown("## Leonardo's Engagement Impact") |
|
|
| |
| engagement_text = gr.Textbox(label="Engagement Update", value="Click the button to see the increase.", interactive=False) |
|
|
| |
| with gr.Row(): |
| gr.Button("See New Engagement").click(fn=calculate_engagement_increase, inputs=[], outputs=[engagement_text]) |
|
|
| |
| gr.Markdown("# Leonardo's Plans for This Week") |
|
|
| |
| with gr.Row(): |
| with gr.Column(): |
| video_path_1, graph_1 = load_data_and_plot("video", "TikTok1", tiktok_csv_path, video_folder) |
| if video_path_1: |
| gr.Video(video_path_1, label="TikTok 1 Video") |
| gr.Plot(graph_1, label="TikTok 1 Performance") |
|
|
| with gr.Column(): |
| video_path_2, graph_2 = load_data_and_plot("video", "TikTok2", tiktok_csv_path, video_folder) |
| if video_path_2: |
| gr.Video(video_path_2, label="TikTok 2 Video") |
| gr.Plot(graph_2, label="TikTok 2 Performance") |
|
|
| with gr.Column(): |
| video_path_3, graph_3 = load_data_and_plot("video", "TikTok3", tiktok_csv_path, video_folder) |
| if video_path_3: |
| gr.Video(video_path_3, label="TikTok 3 Video") |
| gr.Plot(graph_3, label="TikTok 3 Performance") |
|
|
| |
| with gr.Row(): |
| with gr.Column(): |
| image_path_1, graph_4 = load_data_and_plot("image", "Static1", facebook_csv_path, image_folder) |
| if image_path_1: |
| gr.Image(image_path_1, label="Facebook Static 1") |
| gr.Plot(graph_4, label="Facebook Static 1 Performance") |
|
|
| with gr.Column(): |
| image_path_2, graph_5 = load_data_and_plot("image", "Static2", facebook_csv_path, image_folder) |
| if image_path_2: |
| gr.Image(image_path_2, label="Facebook Static 2") |
| gr.Plot(graph_5, label="Facebook Static 2 Performance") |
|
|
| with gr.Column(): |
| image_path_3, graph_6 = load_data_and_plot("image", "Static3", facebook_csv_path, image_folder) |
| if image_path_3: |
| gr.Image(image_path_3, label="Facebook Static 3") |
| gr.Plot(graph_6, label="Facebook Static 3 Performance") |
|
|
| |
| dashboard.launch(share=True, debug=True) |