File size: 35,160 Bytes
64e353a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8b63b9
64e353a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8b63b9
 
 
 
 
 
 
 
 
 
 
64e353a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8b63b9
 
 
 
 
 
64e353a
a8b63b9
64e353a
a8b63b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64e353a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8b63b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import streamlit as st
import pandas as pd
import numpy as np
import datetime
import plotly.express as px
import plotly.graph_objects as go
import statsmodels.api as sm
from millify import millify
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from st_aggrid import AgGrid
import io
import re
import emoji
from collections import Counter
import openpyxl
from gensim.models import Word2Vec
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#from st_pages import Page, show_pages, add_page_title

# Set page title
st.set_page_config(page_title="Content - Tiktok Analytics Dashboard", page_icon = "📊", layout = "centered", initial_sidebar_state = "auto")

st.header("Content")
st.markdown("""Upload your files here to load your data!

*'Trending videos', 'Video Posts' (xlsx or csv format)*
""")

def plot_chart(data, chart_type, x_var, y_var, z_var=None, show_regression_line=False, show_r_squared=False):
    scatter_marker_color = 'green'
    regression_line_color = 'red'
    if chart_type == "line":
        fig = px.line(data, x=x_var, y=y_var)

    elif chart_type == "bar":
        fig = px.bar(data, x=x_var, y=y_var)

    elif chart_type == "scatter":
        fig = px.scatter(data, x=x_var, y=y_var, color_discrete_sequence=[scatter_marker_color])

        if show_regression_line:
            X = data[x_var].values.reshape(-1, 1)
            y = data[y_var].values.reshape(-1, 1)
            model = LinearRegression().fit(X, y)
            y_pred = model.predict(X)
            r_squared = r2_score(y, y_pred)  # Calculate R-squared value

            fig.add_trace(
                go.Scatter(x=data[x_var], y=y_pred[:, 0], mode='lines', name='Regression Line', line=dict(color=regression_line_color))
            )

            # Add R-squared value as a text annotation
            fig.add_annotation(
                x=data[x_var].max(),
                y=y_pred[-1, 0],
                text=f"R-squared: {r_squared:.4f}",
                showarrow=False,
                font=dict(size=14),
                bgcolor='rgba(255, 255, 255, 0.8)',
                bordercolor='black',
                borderwidth=1,
                borderpad=4
            )

    elif chart_type == "heatmap":
        fig = px.imshow(data, color_continuous_scale='Inferno')

    elif chart_type == "scatter_3d":
        if z_var is not None:
            fig = px.scatter_3d(data, x=x_var, y=y_var, z=z_var, color=data.columns[0])
        else:
            st.warning("Please select Z variable for 3D line plot.")
            return

    elif chart_type == "line_3d":
        if z_var is not None:
            fig = go.Figure(data=[go.Scatter3d(x=data[x_var], y=data[y_var], z=data[z_var], mode='lines')])
            fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var))  # Set the axis name
        else:
            st.warning("Please select Z variable for 3D line plot.")
            return

    elif chart_type == "surface_3d":
        if z_var is not None:
            fig = go.Figure(data=[go.Surface(z=data.values)])
            fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var))  # Set the axis name
        else:
            st.warning("Please select Z variable for 3D line plot.")
            return

    elif chart_type == "radar":
        fig = go.Figure()
        for col in data.columns[1:]:
            fig.add_trace(go.Scatterpolar(r=data[col], theta=data[x_var], mode='lines', name=col))
        fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[data[data.columns[1:]].min().min(), data[data.columns[1:]].max().max()])))

    st.plotly_chart(fig)

def plot_radar_chart(data, columns):
    df = data[columns]
    fig = go.Figure()

    for i in range(len(df)):
        date_label = data.loc[i, 'Date']
        fig.add_trace(go.Scatterpolar(
            r=df.loc[i].values,
            theta=df.columns,
            fill='toself',
            name=date_label
        ))

    fig.update_layout(
        polar=dict(
            radialaxis=dict(
                visible=True,
                range=[0, df.max().max()]
            )
        ),
        showlegend=True
    )

    st.plotly_chart(fig)

uploaded_files = st.file_uploader(
    "Choose CSV or Excel files to upload",
    accept_multiple_files=True,
    type=['csv', 'xlsx'])

if uploaded_files:
    data_list = []
    
        # read the file
    with st.expander("View uploaded data - data covered over 7-day period only"):
        for uploaded_file in uploaded_files:
            st.write("▾ Filename:", uploaded_file.name)
            bytes_data = uploaded_file.read()
            data = None
            if uploaded_file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
                data = pd.read_excel(io.BytesIO(bytes_data))
                AgGrid(data)
            else:
                data = pd.read_csv(io.StringIO(bytes_data.decode('utf-8')))
                AgGrid(data)

            data_list.append(data)
        # get common columns
        common_columns = set.intersection(*(set(data.columns) for data in data_list))

        # keep only common columns
        dataframes = [data[common_columns] for data in data_list]

        # concatenate all dataframes
        data = pd.concat(data_list)

        # drop duplicates
        data = data.drop_duplicates()
        #st.write(data_list)

    #tab1, tab2 = st.tabs(["Trending Videos", "Video Posts"])
    for data in data_list:
        #st.write(data.columns)
        #st.write(data)
        #with tab1:
            if 'video_view_within_days' in data.columns: #Trending Videos
                # Extract hashtags using a regex pattern
                def extract_hashtags(title):
                    return re.findall(r'#\w+', title)

                # Extract emojis using the emoji library
                #def extract_emojis(title):
                    #return [c for c in title if c in emoji.emojize(c)]
                
                # Remove emojis and hashtags from the title
                def clean_title(title):
                    #title_without_emojis = emoji.get_emoji_regexp().sub(u'', title)
                    title_without_hashtags = re.sub(r'#\w+', '', title)
                    return title_without_hashtags.strip()

                data['Hashtags'] = data['Video title'].apply(extract_hashtags)
                #data['Emojis'] = data['Video title'].apply(extract_emojis)
                data['Cleaned_title'] = data['Video title'].apply(clean_title)
                # Add a new column to store the hashtag count
                data['Hashtag_count'] = data['Hashtags'].apply(len)
                # Convert the 'post time' column to datetime format
                data['Post time'] = pd.to_datetime(data['Post time'])
                # Create new columns for 'weekday', 'hour' and 'minute'
                data['weekday_posted'] = data['Post time'].dt.day_name()
                data['hour_posted'] = data['Post time'].dt.hour
                data['min_posted'] = data['Post time'].dt.minute
                st.write(data)
                options = ["Summary", "Total views", "Total shares", "Total likes", "Total comments", "Number of Hashtags", "Hashtag Performance"]
                selected_feature = st.selectbox(label="Select feature", options=options, index=0)
                
                if selected_feature == "Summary":
                    x_var = st.sidebar.selectbox("Select X variable", data.columns)
                    y_var = st.sidebar.selectbox("Select Y variable", data.columns)
                    show_regression_line = False
    
                    z_var_options = ["None"] + list(data.columns)
                    z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable)", z_var_options)
            

                    tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap", 
                                                                    "3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
                    with tab1:
                        st.write("Lineplot")
                        plot_chart(data, "line", x_var, y_var)

                    with tab2:
                        st.write("Barplot")
                        plot_chart(data, "bar", x_var, y_var)

                    with tab3:
                        st.write("Scatterplot")
                        show_regression_line = st.checkbox("Show regression line for scatterplot")
                        plot_chart(data, "scatter", x_var, y_var, show_regression_line=show_regression_line)

                    with tab4:
                        st.write("Heatmap")
                        plot_chart(data, "heatmap", x_var, y_var) 

                    with tab5:
                        st.write("3D Scatterplot")
                        if z_var != "None":
                            plot_chart(data, "scatter_3d", x_var, y_var, z_var)    

                    with tab6:
                        st.write("3D Lineplot")
                        if z_var != "None":
                            plot_chart(data, "line_3d", x_var, y_var, z_var)

                    with tab7:
                        st.write("3D Surfaceplot")
                        if z_var != "None":
                            plot_chart(data, "surface_3d", x_var, y_var, z_var)

                    #with tab8:
                        #st.write("Radar chart for 'Last 60 days'")
                        #radar_columns = ['Video views', 'Profile views', 'Likes', 'Comments', 'Shares']
                        #plot_radar_chart(data, radar_columns)                       
                        # Add more conditions for other specific file names if needed
                
                elif selected_feature == "Total views":
                    data = data.sort_values(by='Total views', ascending=True)
                    fig = px.bar(data, x='Total views', y='Cleaned_title', title='Views of trending videos for the week',
                            color_discrete_sequence=px.colors.qualitative.Alphabet, hover_data={'Total views': ':.2f'})
                    st.plotly_chart(fig)
                elif selected_feature == "Total shares":
                    data = data.sort_values(by='Total shares\xa0', ascending=True)
                    fig = px.bar(data, x='Total shares\xa0', y='Cleaned_title', title='Shares of trending videos for the week',
                            color_discrete_sequence=px.colors.qualitative.Set1, hover_data={'Total shares\xa0': ':.2f'})
                    st.plotly_chart(fig)
                elif selected_feature == "Total likes":
                    data = data.sort_values(by='Total likes', ascending=True)
                    fig = px.bar(data, x='Total likes', y='Cleaned_title', title='Likes of trending videos for the week',
                            color_discrete_sequence=px.colors.qualitative.Antique, hover_data={'Total likes': ':.2f'})
                    st.plotly_chart(fig)
                elif selected_feature == "Total comments":
                    data = data.sort_values(by='Total comments', ascending=True)
                    fig = px.bar(data, x='Total comments', y='Cleaned_title', title='Comments of trending videos for the week',
                            color_discrete_sequence=px.colors.qualitative.Vivid, hover_data={'Total comments': ':.2f'})
                    st.plotly_chart(fig)
                elif selected_feature == "Number of Hashtags":
                    # Count the occurrences of each hashtag
                    hashtag_counts = Counter(hashtag for hashtags in data['Hashtags'] for hashtag in hashtags)

                    # Get the top N most common hashtags
                    N = 10
                    top_hashtags = hashtag_counts.most_common(N)

                    # Display the top hashtags
                    print(f"Top {N} hashtags:")
                    for hashtag, count in top_hashtags:
                        print(f"{hashtag}: {count}")

                    # Visualize the results with a Plotly bar chart
                    fig = go.Figure(go.Bar(
                        x=[t[0] for t in top_hashtags],
                        y=[t[1] for t in top_hashtags],
                        text=[t[1] for t in top_hashtags],
                        textposition='auto',
                        marker_color='rgba(58, 71, 80, 0.6)',
                        opacity=0.8
                    ))

                    fig.update_layout(
                        title=f'Top {N} Hashtags',
                        xaxis_title='Hashtags',
                        yaxis_title='Count',
                        xaxis_tickangle=-45
                    )

                    st.plotly_chart(fig)

                    tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
                    with tab1:
                        fig = px.scatter(data, x='Hashtag_count', y='Total views', hover_data=['Cleaned_title'])
                        fig.update_layout(
                            title='Hashtag Count vs. Views',
                            xaxis_title='Hashtag Count',
                            yaxis_title='Views'
                        )
                        st.plotly_chart(fig)
                    with tab2:
                        fig = px.scatter(data, x='Hashtag_count', y='Total shares\xa0', hover_data=['Cleaned_title'])
                        fig.update_layout(
                            title='Hashtag Count vs. Shares',
                            xaxis_title='Hashtag Count',
                            yaxis_title='Shares'
                        )
                        st.plotly_chart(fig)
                    with tab3:
                        fig = px.scatter(data, x='Hashtag_count', y='Total likes', hover_data=['Cleaned_title'])
                        fig.update_layout(
                            title='Hashtag Count vs. Likes',
                            xaxis_title='Hashtag Count',
                            yaxis_title='Likes'
                        )
                        st.plotly_chart(fig)
                    with tab4:
                        fig = px.scatter(data, x='Hashtag_count', y='Total comments', hover_data=['Cleaned_title'])
                        fig.update_layout(
                            title='Hashtag Count vs. Comments',
                            xaxis_title='Hashtag Count',
                            yaxis_title='Comments'
                        )
                        st.plotly_chart(fig)
                elif selected_feature == "Hashtag Performance":
                    # Tokenize hashtags and create a list of unique hashtags
                    tokenized_hashtags = data["Hashtags"].tolist()
                    unique_hashtags = list(set([tag for tags in tokenized_hashtags for tag in tags]))
                    # Train a word2vec model
                    model = Word2Vec(tokenized_hashtags, size=50, window=5, min_count=1, workers=4)

                    # Create a hashtag vector dictionary
                    hashtag_vectors = {tag: model.wv[tag] for tag in unique_hashtags}
                    st.subheader("Explaining the concept of hashtag performance scores and cosine similarity scores")
                    st.markdown(
                        """
                    So how are the **performance scores** calculated for each feature? 

                    In each line, the code goes through each unique hashtag and selects all videos that use that hashtag. It then calculates the mean of the respective performance metric (views, shares, likes, or comments) for those videos.

                    This gives an average performance score for each hashtag, which can be used as an indication of how well videos with that hashtag tend to perform on average. However, this is a simplistic metric and there may be other factors influencing the performance of a video. It's also worth noting that the mean is sensitive to extreme values, so a few very popular or unpopular videos could skew the average performance for a given hashtag.
                    """)

                    st.markdown("""
                    How about **cosine similarity**?

                    Cosine similarity is a metric used to measure how similar two vectors are, irrespective of their size. In the context of Natural Language Processing (NLP), and in this case, it's used to measure the semantic similarity between two hashtags based on their embeddings (vectors) generated by the Word2Vec model.

                    In simple terms, it measures the cosine of the angle between two vectors. If the vectors are identical, the angle is 0, so the cosine is 1, indicating perfect similarity. If the vectors are orthogonal (i.e., the angle between them is 90 degrees), they're considered not similar, and the cosine similarity is 0. If the vectors point in opposite directions (i.e., the angle is 180 degrees), the cosine similarity is -1, indicating that they're diametrically dissimilar.
                    """)
                    # Calculate the average performance of each hashtag - views
                    hashtag_performance_views = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total views"].mean() for tag in unique_hashtags}

                    # Calculate the average performance of each hashtag - shares
                    hashtag_performance_shares = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total shares\xa0"].mean() for tag in unique_hashtags}

                    # Calculate the average performance of each hashtag - likes
                    hashtag_performance_likes = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total likes"].mean() for tag in unique_hashtags}

                    # Calculate the average performance of each hashtag - comments
                    hashtag_performance_comments = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total comments"].mean() for tag in unique_hashtags}

                    # Calculate the similarity between hashtags
                    similarity_matrix = cosine_similarity(list(hashtag_vectors.values()))

                    # Convert the similarity matrix into a DataFrame
                    similarity_df = pd.DataFrame(similarity_matrix, index=unique_hashtags, columns=unique_hashtags)

                    # Convert the performance dictionaries into DataFrames
                    perf_views_df = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "views"])
                    perf_shares_df = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "shares"])
                    perf_likes_df = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "likes"])
                    perf_comments_df = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "comments"])

                    # Merge the performance DataFrames into a single DataFrame
                    perf_df = pd.merge(perf_views_df, perf_shares_df, on="hashtag")
                    perf_df = pd.merge(perf_df, perf_likes_df, on="hashtag")
                    perf_df = pd.merge(perf_df, perf_comments_df, on="hashtag")

                    # Convert the similarity matrix into a 1D series
                    similarity_series = similarity_df.unstack()

                    # Rename the series index
                    similarity_series.index.rename(["hashtag1", "hashtag2"], inplace=True)

                    # Convert the series into a DataFrame
                    similarity_df = similarity_series.to_frame("similarity").reset_index()

                    # Merge the similarity DataFrame with the performance DataFrame
                    merged_df = pd.merge(similarity_df, perf_df, left_on="hashtag1", right_on="hashtag")

                    # Calculate the correlation between hashtag similarity and performance
                    correlation = merged_df[["similarity", "views", "shares", "likes", "comments"]].corr()
                    st.subheader("Correlation matrix between hashtag cosine similarity values and performance values")
                    #st.write(correlation)
                    # Create a heatmap
                    plt.figure(figsize=(10, 8))
                    sns.heatmap(correlation, annot=True, fmt=".2f", cmap='coolwarm', cbar=True)

                    # Show the plot in Streamlit
                    st.pyplot(plt)
                    # Rename the 'value' columns to make them unique
                    df1 = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "value"])
                    df2 = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "value"])
                    df3 = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "value"])
                    df4 = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "value"])

                    df1.rename(columns={'value': 'value_views'}, inplace=True)
                    df2.rename(columns={'value': 'value_shares'}, inplace=True)
                    df3.rename(columns={'value': 'value_likes'}, inplace=True)
                    df4.rename(columns={'value': 'value_comments'}, inplace=True)

                    # Merge the DataFrames on the 'hashtag' column
                    merged_df = df1.merge(df2, on='hashtag').merge(df3, on='hashtag').merge(df4, on='hashtag')

                    st.subheader("Hashtag Performance Scores based on Views, Shares, Likes and Comments - Calculated using average of all videos' metrics containing a particular hashtag")
                    st.write(merged_df)

                    # Create a pair plot with regression lines
                    st.write("**Pair Plots**")
                    sns.pairplot(merged_df, kind="reg", diag_kind="kde")
                    st.pyplot(plt)

                    # Split the data into train and test sets
                    X = merged_df[["value_shares", "value_likes", "value_comments"]]
                    y = merged_df["value_views"]
                    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
                    
                    #st.write("**3D Scatterplot of Predictors with Regression Plane**")
                    # Create a 3D scatterplot with a regression plane
                    #fig = plt.figure()
                    #ax = fig.add_subplot(111, projection="3d")

                    #ax.scatter(merged_df["value_shares"], merged_df["value_likes"], merged_df["value_comments"], c="blue", marker="o")

                    # Create a meshgrid for the regression plane
                    #x_range = np.linspace(merged_df["value_shares"].min(), merged_df["value_shares"].max(), num=10)
                    #y_range = np.linspace(merged_df["value_likes"].min(), merged_df["value_likes"].max(), num=10)
                    
                    #x_grid, y_grid = np.meshgrid(x_range, y_range)

                    # Calculate the regression plane
                    #X_grid = np.column_stack((x_grid.ravel(), y_grid.ravel()))
                    #plane = np.zeros(X_grid.shape[0])

                    #for i, (x, y) in enumerate(X_grid):
                        #plane[i] = model.predict(np.column_stack((x, y, merged_df["value_comments"].mean())).reshape(1, -1))

                    #z_grid = plane.reshape(x_grid.shape)

                    #ax.set_xlabel("value_shares")
                    #ax.set_ylabel("value_likes")
                    #ax.set_zlabel("value_comments")

                    #plt.show()
                    #st.pyplot(plt)

                    # Fit the models on the training data
                    lr_model = LinearRegression()
                    lr_model.fit(X_train, y_train)

                    rf_model = RandomForestRegressor()
                    rf_model.fit(X_train, y_train)

                    xgb_model = XGBRegressor()
                    xgb_model.fit(X_train, y_train)

                    # Make predictions on the testing data using the trained models
                    lr_pred = lr_model.predict(X_test)
                    rf_pred = rf_model.predict(X_test)
                    xgb_pred = xgb_model.predict(X_test)

                    # Calculate the evaluation metrics for each model
                    st.write("**Training regression models to predict value_views using value_shares, value_likes and value_comments**")
                    models = ["Linear Regression", "Random Forest", "XGBoost"]
                    predictions = [lr_pred, rf_pred, xgb_pred]

                    # Initialize a list to hold the model metrics
                    model_metrics = []

                    for model, pred in zip(models, predictions):
                        mse = mean_squared_error(y_test, pred)
                        mae = mean_absolute_error(y_test, pred)
                        r2 = r2_score(y_test, pred)
    
                        # Append a dictionary of the metrics to the list
                        model_metrics.append({"Model": model, "Mean Squared Error": mse, "Mean Absolute Error": mae, "R^2 Score": r2})
                    
                    # Convert the list of dictionaries into a DataFrame
                    metrics_df = pd.DataFrame(model_metrics)

                    # Display the DataFrame in Streamlit
                    st.write(metrics_df)


                    x_var = st.sidebar.selectbox("Select X variable", merged_df.columns)
                    y_var = st.sidebar.selectbox("Select Y variable", merged_df.columns)
                    show_regression_line = False
    
                    z_var_options = ["None"] + list(merged_df.columns)
                    z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable)", z_var_options)
            
            # Allow user to select time frequency for resampling
            #time_frequency = st.sidebar.selectbox("Select time frequency", ["Day", "Week", "Month"])

            #if time_frequency == "Week":
                #data_resampled = data.resample('W').sum()
            #elif time_frequency == "Month":
                #data_resampled = data.resample('M').sum()
            #else:
                #data_resampled = data
                    st.subheader("Various plots to represent performance scores for views, shares, likes and comments")
                    tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap", 
                                                            "3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
                    with tab1:
                        st.write("Lineplot")
                        plot_chart(merged_df, "line", x_var, y_var)

                    with tab2:
                        st.write("Barplot")
                        plot_chart(merged_df, "bar", x_var, y_var)

                    with tab3:
                        st.write("Scatterplot")
                        show_regression_line = st.checkbox("Show regression line")
                        plot_chart(merged_df, "scatter", x_var, y_var, show_regression_line=show_regression_line)

                    with tab4:
                        st.write("Heatmap")
                        plot_chart(merged_df, "heatmap", x_var, y_var) 

                    with tab5:
                        st.write("3D Scatterplot")
                        if z_var != "None":
                            plot_chart(merged_df, "scatter_3d", x_var, y_var, z_var)    

                    with tab6:
                        st.write("3D Lineplot")
                        if z_var != "None":
                            plot_chart(merged_df, "line_3d", x_var, y_var, z_var)

                    with tab7:
                        st.write("3D Surfaceplot")
                        if z_var != "None":
                            plot_chart(merged_df, "surface_3d", x_var, y_var, z_var)

                    #with tab8:
                        #st.write("Radar chart for 'Last 60 days'")
                        #radar_columns = ['value_views', 'value_shares', 'value_likes', 'value_comments']
                        #plot_radar_chart(data, radar_columns)

                    tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
                    with tab1:
                        st.subheader("Hashtag Performance - Views:")
                        #for tag, perf in hashtag_performance.items():
                            #st.write(f"{tag}: {perf}")
                        #AgGrid(hashtag_performance_views)
                        #df1 = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "value"])
                        # Sort the DataFrame by the 'value' column in descending order
                        sorted_df1 = df1.sort_values(by="value_views", ascending=False)
                        st.write(sorted_df1)
                        # Highlight specific bars (use 'rgba' values for transparency)
                        highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
                        sorted_df1['color'] = sorted_df1['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'red')
                        fig = px.bar(sorted_df1, x='hashtag', y='value_views', title='Hashtag performance for the week',
                        color='color', color_discrete_map='identity', hover_data={'value_views': ':.2f'})

                        fig.update_layout(title='Hashtag performance for the week - Views', xaxis_title='Hashtag', yaxis_title='Value')
                        st.plotly_chart(fig)
                        
                    with tab2:
                        st.subheader("Hashtag Performance - Shares:")
                        #for tag, perf in hashtag_performance.items():
                            #st.write(f"{tag}: {perf}")
                        #AgGrid(hashtag_performance_views)
                        #df2 = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "value"])
                        # Sort the DataFrame by the 'value' column in descending order
                        sorted_df2 = df2.sort_values(by="value_shares", ascending=False)
                        st.write(sorted_df2)
                        # Highlight specific bars (use 'rgba' values for transparency)
                        highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
                        sorted_df2['color'] = sorted_df2['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'blue')
                        fig = px.bar(sorted_df2, x='hashtag', y='value_shares', title='Hashtag performance for the week',
                        color='color', color_discrete_map='identity', hover_data={'value_shares': ':.2f'})

                        fig.update_layout(title='Hashtag performance for the week - Shares', xaxis_title='Hashtag', yaxis_title='Value')
                        st.plotly_chart(fig)
                    with tab3:
                        st.subheader("Hashtag Performance - Likes:")
                        #for tag, perf in hashtag_performance.items():
                            #st.write(f"{tag}: {perf}")
                        #AgGrid(hashtag_performance_views)
                        #df3 = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "value"])
                        # Sort the DataFrame by the 'value' column in descending order
                        sorted_df3 = df3.sort_values(by="value_likes", ascending=False)
                        st.write(sorted_df3)
                        # Highlight specific bars (use 'rgba' values for transparency)
                        highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
                        sorted_df3['color'] = sorted_df3['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'green')
                        fig = px.bar(sorted_df3, x='hashtag', y='value_likes', title='Hashtag performance for the week',
                        color='color', color_discrete_map='identity', hover_data={'value_likes': ':.2f'})

                        fig.update_layout(title='Hashtag performance for the week - Likes', xaxis_title='Hashtag', yaxis_title='Value')
                        st.plotly_chart(fig)
                    with tab4:
                        st.subheader("Hashtag Performance - Comments:")
                        #for tag, perf in hashtag_performance.items():
                            #st.write(f"{tag}: {perf}")
                        #AgGrid(hashtag_performance_views)
                        #df4 = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "value"])
                        # Sort the DataFrame by the 'value' column in descending order
                        sorted_df4 = df4.sort_values(by="value_comments", ascending=False)
                        st.write(sorted_df4)
                        # Highlight specific bars (use 'rgba' values for transparency)
                        highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
                        sorted_df4['color'] = sorted_df4['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'orange')
                        fig = px.bar(sorted_df4, x='hashtag', y='value_comments', title='Hashtag performance for the week',
                        color='color', color_discrete_map='identity', hover_data={'value_comments': ':.2f'})

                        fig.update_layout(title='Hashtag performance for the week - Comments', xaxis_title='Hashtag', yaxis_title='Value')
                        st.plotly_chart(fig)

                   
                    # Calculate the similarity between hashtags
                    #similarity_matrix = cosine_similarity(list(hashtag_vectors.values()))
                    #st.write(similarity_matrix)
                    # Convert the similarity matrix into a DataFrame
                    #similarity_df = pd.DataFrame(similarity_matrix, index=unique_hashtags, columns=unique_hashtags)
                    #st.write(similarity_df)
                    # Calculate the correlation between hashtag similarity and performance
                    #correlations = np.corrcoef(similarity_df, data["Total views"], rowvar=False)
                    #st.write(correlations)

        #with tab2:
            #if 'video_view_within_days' not in data.columns: #Video Posts
                #pass