Team Ai
Apppublic

itzfrontman/pytorch-hackathon

sourceHugging Facemitupdated 6mo agoView on Hugging Face
1likes
app.py55 linesDownload Raw Back to root
1import gradio as gr
2from env import EcommerceEnv
3from models import Action
4import random
5
6def simulate():
7    env = EcommerceEnv()
8    obs = env.reset()
9
10    log = ""
11    total_reward = 0
12    steps = 0
13    clicks = 0
14    purchases = 0
15
16    done = False
17
18    while not done:
19
20        action = Action(recommended_product=random.randint(1, 3))
21
22        obs, reward, done, _ = env.step(action)
23
24        steps += 1
25        total_reward += reward.score
26
27        if reward.score == 1.0:
28            purchases += 1
29        elif reward.score > 0:
30            clicks += 1
31
32        log += f"Step {steps} โ†’ Recommended: {action.recommended_product} | Reward: {reward.score}\n"
33
34    # Metrics
35    ctr = clicks / steps if steps else 0
36    conversion = purchases / steps if steps else 0
37
38    log += "\n--- SESSION SUMMARY ---\n"
39    log += f"Total Steps: {steps}\n"
40    log += f"Total Reward: {round(total_reward,2)}\n"
41    log += f"Clicks: {clicks}\n"
42    log += f"Purchases: {purchases}\n"
43    log += f"CTR: {round(ctr,2)}\n"
44    log += f"Conversion Rate: {round(conversion,2)}\n"
45
46    return log
47
48
49gr.Interface(
50    fn=simulate,
51    inputs=[],
52    outputs="text",
53    title="๐Ÿ›’ AI E-commerce Recommendation Simulator",
54    description="Simulates how an AI agent recommends products and optimizes user engagement & conversions."
55).launch(share=True)