Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -63,9 +63,16 @@ def generate_insights(cluster_characteristics):
|
|
| 63 |
insights.append("High click-through rate: Users are interacting well with ads. Increase ad relevance to boost conversions.")
|
| 64 |
if cluster_characteristics['Bounce Rate'] > 0.3:
|
| 65 |
insights.append("High bounce rate: Review landing page design and content relevance to improve user retention.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return " ".join(insights)
|
| 67 |
|
| 68 |
def predict_cluster(session_duration, pages_visited, ads_clicked, engagement_score, user_interests, device_type, time_of_day, time_spent_per_page, click_through_rate, conversion_rate, frequency_of_visits, bounce_rate):
|
|
|
|
| 69 |
input_df = pd.DataFrame({
|
| 70 |
'Session Duration': [session_duration],
|
| 71 |
'Pages Visited': [pages_visited],
|
|
@@ -80,7 +87,9 @@ def predict_cluster(session_duration, pages_visited, ads_clicked, engagement_sco
|
|
| 80 |
'Frequency of Visits': [frequency_of_visits],
|
| 81 |
'Bounce Rate': [bounce_rate]
|
| 82 |
})
|
|
|
|
| 83 |
cluster = pipeline.predict(input_df)[0]
|
|
|
|
| 84 |
centroids = pipeline.named_steps['cluster'].cluster_centers_
|
| 85 |
cluster_characteristics = centroids[cluster]
|
| 86 |
|
|
@@ -99,19 +108,22 @@ def predict_cluster(session_duration, pages_visited, ads_clicked, engagement_sco
|
|
| 99 |
# Generate actionable insights
|
| 100 |
insights = generate_insights(cluster_characteristics)
|
| 101 |
|
|
|
|
| 102 |
return f"Predicted Cluster: {cluster}\nCharacteristics: {cluster_characteristics}\nActionable Insights: {insights}"
|
| 103 |
|
| 104 |
def ad_performance_analytics():
|
| 105 |
-
|
| 106 |
avg_ctr = data['Click Through Rate'].mean()
|
| 107 |
avg_conversion_rate = data['Conversion Rate'].mean()
|
| 108 |
avg_bounce_rate = data['Bounce Rate'].mean()
|
|
|
|
| 109 |
|
| 110 |
# Prepare the analytics report
|
| 111 |
report = f"Average Click Through Rate: {avg_ctr:.2%}\n"
|
| 112 |
report += f"Average Conversion Rate: {avg_conversion_rate:.2%}\n"
|
| 113 |
report += f"Average Bounce Rate: {avg_bounce_rate:.2%}"
|
| 114 |
|
|
|
|
| 115 |
return report
|
| 116 |
|
| 117 |
with gr.Blocks() as demo:
|
|
@@ -140,6 +152,7 @@ with gr.Blocks() as demo:
|
|
| 140 |
],
|
| 141 |
outputs=output_textbox
|
| 142 |
)
|
|
|
|
| 143 |
|
| 144 |
with gr.Tab("Ad Performance Analytics"):
|
| 145 |
gr.Markdown("""
|
|
@@ -157,6 +170,8 @@ with gr.Blocks() as demo:
|
|
| 157 |
ad_performance_analytics,
|
| 158 |
outputs=analytics_output
|
| 159 |
)
|
|
|
|
| 160 |
|
| 161 |
demo.launch()
|
|
|
|
| 162 |
|
|
|
|
| 63 |
insights.append("High click-through rate: Users are interacting well with ads. Increase ad relevance to boost conversions.")
|
| 64 |
if cluster_characteristics['Bounce Rate'] > 0.3:
|
| 65 |
insights.append("High bounce rate: Review landing page design and content relevance to improve user retention.")
|
| 66 |
+
if cluster_characteristics['Frequency of Visits'] > 15:
|
| 67 |
+
insights.append("Frequent visits: Users are returning often, consider loyalty programs or personalized content to maintain engagement.")
|
| 68 |
+
if cluster_characteristics['Time Spent per Page'] < 20:
|
| 69 |
+
insights.append("Low time spent per page: Content may not be engaging or relevant enough. Consider content optimization.")
|
| 70 |
+
if cluster_characteristics['Conversion Rate'] > 0.15:
|
| 71 |
+
insights.append("High conversion rate: Effective ad targeting. Explore scaling up ad spend on similar user segments.")
|
| 72 |
return " ".join(insights)
|
| 73 |
|
| 74 |
def predict_cluster(session_duration, pages_visited, ads_clicked, engagement_score, user_interests, device_type, time_of_day, time_spent_per_page, click_through_rate, conversion_rate, frequency_of_visits, bounce_rate):
|
| 75 |
+
logging.info("Starting cluster prediction.")
|
| 76 |
input_df = pd.DataFrame({
|
| 77 |
'Session Duration': [session_duration],
|
| 78 |
'Pages Visited': [pages_visited],
|
|
|
|
| 87 |
'Frequency of Visits': [frequency_of_visits],
|
| 88 |
'Bounce Rate': [bounce_rate]
|
| 89 |
})
|
| 90 |
+
logging.debug(f"Input DataFrame: {input_df}")
|
| 91 |
cluster = pipeline.predict(input_df)[0]
|
| 92 |
+
logging.info(f"Predicted cluster: {cluster}")
|
| 93 |
centroids = pipeline.named_steps['cluster'].cluster_centers_
|
| 94 |
cluster_characteristics = centroids[cluster]
|
| 95 |
|
|
|
|
| 108 |
# Generate actionable insights
|
| 109 |
insights = generate_insights(cluster_characteristics)
|
| 110 |
|
| 111 |
+
logging.info("Cluster prediction completed.")
|
| 112 |
return f"Predicted Cluster: {cluster}\nCharacteristics: {cluster_characteristics}\nActionable Insights: {insights}"
|
| 113 |
|
| 114 |
def ad_performance_analytics():
|
| 115 |
+
logging.info("Calculating ad performance analytics.")
|
| 116 |
avg_ctr = data['Click Through Rate'].mean()
|
| 117 |
avg_conversion_rate = data['Conversion Rate'].mean()
|
| 118 |
avg_bounce_rate = data['Bounce Rate'].mean()
|
| 119 |
+
logging.debug(f"Average CTR: {avg_ctr}, Average Conversion Rate: {avg_conversion_rate}, Average Bounce Rate: {avg_bounce_rate}")
|
| 120 |
|
| 121 |
# Prepare the analytics report
|
| 122 |
report = f"Average Click Through Rate: {avg_ctr:.2%}\n"
|
| 123 |
report += f"Average Conversion Rate: {avg_conversion_rate:.2%}\n"
|
| 124 |
report += f"Average Bounce Rate: {avg_bounce_rate:.2%}"
|
| 125 |
|
| 126 |
+
logging.info("Ad performance analytics calculation completed.")
|
| 127 |
return report
|
| 128 |
|
| 129 |
with gr.Blocks() as demo:
|
|
|
|
| 152 |
],
|
| 153 |
outputs=output_textbox
|
| 154 |
)
|
| 155 |
+
logging.info("Gradio predict button configured.")
|
| 156 |
|
| 157 |
with gr.Tab("Ad Performance Analytics"):
|
| 158 |
gr.Markdown("""
|
|
|
|
| 170 |
ad_performance_analytics,
|
| 171 |
outputs=analytics_output
|
| 172 |
)
|
| 173 |
+
logging.info("Gradio analytics button configured.")
|
| 174 |
|
| 175 |
demo.launch()
|
| 176 |
+
logging.info("Gradio interface launched.")
|
| 177 |
|