Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- dynamic_pricing_bandit_app.py +69 -0
- requirements.txt +4 -0
dynamic_pricing_bandit_app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# dynamic_pricing_bandit_app.py
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
PRICE_POINTS = [5000, 7500, 10000, 12500, 15000]
|
| 9 |
+
SEGMENTS = ["Retail", "HNI", "Corporate"]
|
| 10 |
+
DEAL_TYPES = ["M&A Advisory", "Debt Issuance", "Equity Offering", "Restructuring"]
|
| 11 |
+
REGIONS = ["North America", "Europe", "Asia Pacific", "Latin America", "Middle East"]
|
| 12 |
+
INDUSTRIES = ["Technology", "Healthcare", "Financial Services", "Energy", "Consumer Goods", "Industrial"]
|
| 13 |
+
|
| 14 |
+
class ThompsonBandit:
|
| 15 |
+
def __init__(self, n_arms):
|
| 16 |
+
self.successes = np.ones(n_arms)
|
| 17 |
+
self.failures = np.ones(n_arms)
|
| 18 |
+
|
| 19 |
+
def select_arm(self):
|
| 20 |
+
return np.argmax(np.random.beta(self.successes, self.failures))
|
| 21 |
+
|
| 22 |
+
def update(self, arm, reward):
|
| 23 |
+
if reward:
|
| 24 |
+
self.successes[arm] += 1
|
| 25 |
+
else:
|
| 26 |
+
self.failures[arm] += 1
|
| 27 |
+
|
| 28 |
+
# Load HF dataset
|
| 29 |
+
dataset = load_dataset("banking77", split="train[:500]")
|
| 30 |
+
df = pd.DataFrame(dataset)
|
| 31 |
+
df["segment"] = np.random.choice(SEGMENTS, len(df))
|
| 32 |
+
df["deal_type"] = np.random.choice(DEAL_TYPES, len(df))
|
| 33 |
+
df["deal_size"] = np.random.lognormal(mean=16, sigma=1.0, size=len(df)).astype(int)
|
| 34 |
+
df["region"] = np.random.choice(REGIONS, len(df))
|
| 35 |
+
df["industry"] = np.random.choice(INDUSTRIES, len(df))
|
| 36 |
+
|
| 37 |
+
bandit = ThompsonBandit(len(PRICE_POINTS))
|
| 38 |
+
|
| 39 |
+
def recommend_price(segment, deal_type, deal_size_str, region, industry):
|
| 40 |
+
try:
|
| 41 |
+
deal_size = float(deal_size_str.replace("$", "").replace(",", ""))
|
| 42 |
+
arm = bandit.select_arm()
|
| 43 |
+
price = PRICE_POINTS[arm]
|
| 44 |
+
acceptance_prob = max(0.1, 1 - (price / PRICE_POINTS[-1]) * 0.8)
|
| 45 |
+
accepted = np.random.binomial(1, acceptance_prob)
|
| 46 |
+
bandit.update(arm, accepted)
|
| 47 |
+
return f"Recommended Price: ${price:,}\nClient would {'accept' if accepted else 'decline'} this price."
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return str(e)
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as app:
|
| 52 |
+
gr.Markdown("# Dynamic Pricing Bandit App")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
segment_input = gr.Dropdown(choices=SEGMENTS, label="Client Segment")
|
| 56 |
+
deal_type_input = gr.Dropdown(choices=DEAL_TYPES, label="Deal Type")
|
| 57 |
+
deal_size_input = gr.Textbox(label="Deal Size (USD)", value="$50000000")
|
| 58 |
+
region_input = gr.Dropdown(choices=REGIONS, label="Region")
|
| 59 |
+
industry_input = gr.Dropdown(choices=INDUSTRIES, label="Industry")
|
| 60 |
+
btn = gr.Button("Get Recommendation")
|
| 61 |
+
with gr.Column():
|
| 62 |
+
result = gr.Markdown()
|
| 63 |
+
|
| 64 |
+
btn.click(fn=recommend_price,
|
| 65 |
+
inputs=[segment_input, deal_type_input, deal_size_input, region_input, industry_input],
|
| 66 |
+
outputs=result)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
datasets
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|