Update app.py
Browse files
app.py
CHANGED
@@ -8,12 +8,14 @@ from typing_extensions import TypedDict
|
|
8 |
from langgraph.graph import StateGraph, START, END
|
9 |
|
10 |
# Define the game state
|
11 |
-
global_score = {"total": 0}
|
|
|
12 |
|
13 |
class State(TypedDict):
|
14 |
bet: int
|
15 |
result: str
|
16 |
score: int
|
|
|
17 |
|
18 |
# Nodes for LangGraph
|
19 |
def input_node(state):
|
@@ -21,19 +23,19 @@ def input_node(state):
|
|
21 |
|
22 |
def success_node(state):
|
23 |
global_score["total"] += 10
|
24 |
-
state["result"] = f"You win! +$10"
|
25 |
state["score"] = global_score["total"]
|
26 |
return state
|
27 |
|
28 |
def failure_node(state):
|
29 |
global_score["total"] -= 5
|
30 |
-
state["result"] = f"You lose! -$5"
|
31 |
state["score"] = global_score["total"]
|
32 |
return state
|
33 |
|
34 |
def determine_outcome(state) -> Literal["node_2", "node_3"]:
|
35 |
roll = random.randint(1, 6) + random.randint(1, 6)
|
36 |
-
|
37 |
return "node_2" if roll <= state["bet"] else "node_3"
|
38 |
|
39 |
# Build LangGraph
|
@@ -52,19 +54,25 @@ graph = builder.compile()
|
|
52 |
# Gradio UI function
|
53 |
def play_game(bet: int):
|
54 |
if bet < 1 or bet > 12:
|
55 |
-
return "Please enter a value between 1 and 12.", global_score["total"]
|
56 |
-
result = graph.invoke({"bet": bet, "result": "", "score": global_score["total"]})
|
57 |
-
|
|
|
|
|
58 |
|
59 |
# Gradio Interface
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown("## π² Dice Bet Game with LangGraph")
|
62 |
with gr.Row():
|
63 |
bet_input = gr.Number(label="Your Bet (1-12)", value=7, precision=0)
|
|
|
|
|
64 |
result_output = gr.Textbox(label="Result")
|
65 |
score_output = gr.Number(label="Total Winnings", value=0, precision=0, interactive=False)
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
demo.launch()
|
|
|
8 |
from langgraph.graph import StateGraph, START, END
|
9 |
|
10 |
# Define the game state
|
11 |
+
global_score = {"total": 0}
|
12 |
+
game_history = [] # Track game rounds
|
13 |
|
14 |
class State(TypedDict):
|
15 |
bet: int
|
16 |
result: str
|
17 |
score: int
|
18 |
+
roll: int
|
19 |
|
20 |
# Nodes for LangGraph
|
21 |
def input_node(state):
|
|
|
23 |
|
24 |
def success_node(state):
|
25 |
global_score["total"] += 10
|
26 |
+
state["result"] = f"π You win! +$10 (Roll: {state['roll']})"
|
27 |
state["score"] = global_score["total"]
|
28 |
return state
|
29 |
|
30 |
def failure_node(state):
|
31 |
global_score["total"] -= 5
|
32 |
+
state["result"] = f"π You lose! -$5 (Roll: {state['roll']})"
|
33 |
state["score"] = global_score["total"]
|
34 |
return state
|
35 |
|
36 |
def determine_outcome(state) -> Literal["node_2", "node_3"]:
|
37 |
roll = random.randint(1, 6) + random.randint(1, 6)
|
38 |
+
state['roll'] = roll
|
39 |
return "node_2" if roll <= state["bet"] else "node_3"
|
40 |
|
41 |
# Build LangGraph
|
|
|
54 |
# Gradio UI function
|
55 |
def play_game(bet: int):
|
56 |
if bet < 1 or bet > 12:
|
57 |
+
return "Please enter a value between 1 and 12.", global_score["total"], "", []
|
58 |
+
result = graph.invoke({"bet": bet, "result": "", "score": global_score["total"], "roll": 0})
|
59 |
+
dice_img = f"https://raw.githubusercontent.com/mnaylor7/dice-roll-animation/main/dice-{result['roll']}.gif" if 2 <= result['roll'] <= 12 else ""
|
60 |
+
game_history.append({"Bet": bet, "Roll": result["roll"], "Result": result["result"], "Score": result["score"]})
|
61 |
+
return result["result"], result["score"], dice_img, game_history[::-1]
|
62 |
|
63 |
# Gradio Interface
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("## π² Dice Bet Game with LangGraph")
|
66 |
with gr.Row():
|
67 |
bet_input = gr.Number(label="Your Bet (1-12)", value=7, precision=0)
|
68 |
+
play_button = gr.Button("π² Roll Dice")
|
69 |
+
with gr.Row():
|
70 |
result_output = gr.Textbox(label="Result")
|
71 |
score_output = gr.Number(label="Total Winnings", value=0, precision=0, interactive=False)
|
72 |
+
dice_display = gr.Image(label="Dice Roll Animation", visible=True)
|
73 |
+
history_table = gr.Dataframe(headers=["Bet", "Roll", "Result", "Score"], label="Game History", interactive=False)
|
74 |
+
|
75 |
+
play_button.click(play_game, inputs=[bet_input], outputs=[result_output, score_output, dice_display, history_table])
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
demo.launch()
|