Download app.py from Lifesightio/Incrementality-Calculator: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/spaces/Lifesightio/Incrementality-Calculator/resolve/main/app.py
- Command line
-
hf download hf://spaces/Lifesightio/Incrementality-Calculator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Lifesightio/Incrementality-Calculator/resolve/main/app.py
3.86 kB
| import gradio as gr | |
| import spaces | |
| def zero_gpu_placeholder(): | |
| pass | |
| def calculate_incrementality( | |
| test_users, | |
| test_conversions, | |
| control_users, | |
| control_conversions, | |
| revenue_per_conversion, | |
| ad_spend | |
| ): | |
| if test_users <= 0 or control_users <= 0: | |
| return "Error: Group sizes must be greater than zero." | |
| if test_conversions < 0 or control_conversions < 0: | |
| return "Error: Conversions cannot be negative." | |
| if test_conversions > test_users or control_conversions > control_users: | |
| return "Error: Conversions cannot exceed group size." | |
| if revenue_per_conversion < 0 or ad_spend <= 0: | |
| return "Error: Revenue must be nonnegative and spend must be positive." | |
| test_rate = test_conversions / test_users | |
| control_rate = control_conversions / control_users | |
| expected_conversions = control_rate * test_users | |
| incremental_conversions = ( | |
| test_conversions - expected_conversions | |
| ) | |
| incremental_revenue = ( | |
| incremental_conversions * revenue_per_conversion | |
| ) | |
| lift = ( | |
| (test_rate - control_rate) / control_rate * 100 | |
| if control_rate > 0 else None | |
| ) | |
| iroas = incremental_revenue / ad_spend | |
| lift_text = ( | |
| f"{lift:.2f}%" | |
| if lift is not None | |
| else "Undefined (zero control conversion rate)" | |
| ) | |
| return f""" | |
| # Campaign Incrementality Results | |
| ### Incremental Conversions | |
| {incremental_conversions:,.1f} | |
| ### Conversion Lift | |
| {lift_text} | |
| ### Incremental Revenue | |
| ${incremental_revenue:,.2f} | |
| ### Incremental ROAS (iROAS) | |
| {iroas:.2f}x | |
| --- | |
| **Interpretation** | |
| The calculator estimates the additional conversions | |
| and revenue associated with the test treatment. | |
| These are illustrative estimates. Causal conclusions | |
| require a properly randomized or otherwise valid | |
| experiment design. | |
| """ | |
| with gr.Blocks(title="Lifesight Incrementality Calculator") as demo: | |
| gr.Markdown(""" | |
| # Lifesight Incrementality Calculator | |
| ### Measure what your marketing actually drives. | |
| Estimate incremental conversions, revenue lift, | |
| and incremental ROAS using test and control groups. | |
| **Powered by Lifesight** | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### Test Group") | |
| test_users = gr.Number( | |
| label="Test Group Users", | |
| value=10000 | |
| ) | |
| test_conversions = gr.Number( | |
| label="Test Group Conversions", | |
| value=500 | |
| ) | |
| with gr.Column(): | |
| gr.Markdown("### Control Group") | |
| control_users = gr.Number( | |
| label="Control Group Users", | |
| value=10000 | |
| ) | |
| control_conversions = gr.Number( | |
| label="Control Group Conversions", | |
| value=400 | |
| ) | |
| revenue_per_conversion = gr.Number( | |
| label="Revenue Per Conversion ($)", | |
| value=100 | |
| ) | |
| ad_spend = gr.Number( | |
| label="Campaign Ad Spend ($)", | |
| value=3000 | |
| ) | |
| calculate_button = gr.Button( | |
| "Calculate Incrementality", | |
| variant="primary" | |
| ) | |
| output = gr.Markdown() | |
| calculate_button.click( | |
| fn=calculate_incrementality, | |
| inputs=[ | |
| test_users, | |
| test_conversions, | |
| control_users, | |
| control_conversions, | |
| revenue_per_conversion, | |
| ad_spend | |
| ], | |
| outputs=output | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| Learn more about Unified Marketing Measurement | |
| at [Lifesight](https://lifesight.io). | |
| **Disclaimer:** Educational calculator. | |
| Not a substitute for experimental validation. | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| ssr_mode=False | |
| ) | |