Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from typing import TypedDict
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langgraph.graph import StateGraph, END
|
| 5 |
+
|
| 6 |
+
# ----------------------
|
| 7 |
+
# LangGraph Game Logic
|
| 8 |
+
# ----------------------
|
| 9 |
+
class GameState(TypedDict):
|
| 10 |
+
bet: int
|
| 11 |
+
result: str
|
| 12 |
+
roll: int
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def node_roll(state: GameState):
|
| 16 |
+
roll = random.randint(1, 6) + random.randint(1, 6)
|
| 17 |
+
state["roll"] = roll
|
| 18 |
+
return state
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def node_win(state: GameState):
|
| 22 |
+
state["result"] = "win"
|
| 23 |
+
return state
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def node_loss(state: GameState):
|
| 27 |
+
state["result"] = "loss"
|
| 28 |
+
return state
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def decide(state: GameState):
|
| 32 |
+
if state["roll"] <= state["bet"]:
|
| 33 |
+
return "win_node"
|
| 34 |
+
return "loss_node"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Build LangGraph
|
| 38 |
+
graph = StateGraph(GameState)
|
| 39 |
+
graph.add_node("roll_node", node_roll)
|
| 40 |
+
graph.add_node("win_node", node_win)
|
| 41 |
+
graph.add_node("loss_node", node_loss)
|
| 42 |
+
|
| 43 |
+
graph.set_entry_point("roll_node")
|
| 44 |
+
graph.add_conditional_edges("roll_node", decide)
|
| 45 |
+
graph.add_edge("win_node", END)
|
| 46 |
+
graph.add_edge("loss_node", END)
|
| 47 |
+
|
| 48 |
+
app = graph.compile()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ----------------------
|
| 52 |
+
# Gradio App State
|
| 53 |
+
# ----------------------
|
| 54 |
+
class Dashboard:
|
| 55 |
+
def __init__(self):
|
| 56 |
+
self.total = 0
|
| 57 |
+
self.history = []
|
| 58 |
+
|
| 59 |
+
def update(self, result):
|
| 60 |
+
if result == "win":
|
| 61 |
+
self.total += 10
|
| 62 |
+
else:
|
| 63 |
+
self.total -= 5
|
| 64 |
+
self.history.append(result)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
dashboard = Dashboard()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# ----------------------
|
| 71 |
+
# Game Function
|
| 72 |
+
# ----------------------
|
| 73 |
+
def play_game(bet):
|
| 74 |
+
if bet is None or bet < 2 or bet > 12:
|
| 75 |
+
return "Enter a valid bet (2-12)", dashboard.total, str(dashboard.history)
|
| 76 |
+
|
| 77 |
+
state = {"bet": int(bet)}
|
| 78 |
+
result_state = app.invoke(state)
|
| 79 |
+
|
| 80 |
+
roll = result_state["roll"]
|
| 81 |
+
result = result_state["result"]
|
| 82 |
+
|
| 83 |
+
dashboard.update(result)
|
| 84 |
+
|
| 85 |
+
message = f"π² Dice Roll: {roll} β You {result.upper()}"
|
| 86 |
+
return message, dashboard.total, str(dashboard.history)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def stop_game():
|
| 90 |
+
return "π Game stopped.", dashboard.total, str(dashboard.history)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# ----------------------
|
| 94 |
+
# Gradio UI (HF Spaces Ready)
|
| 95 |
+
# ----------------------
|
| 96 |
+
with gr.Blocks(title="Dice Betting Game") as demo:
|
| 97 |
+
gr.Markdown("""
|
| 98 |
+
# π² Dice Betting Game (LangGraph)
|
| 99 |
+
Place a bet between 2 and 12.\n
|
| 100 |
+
- Win β +$10
|
| 101 |
+
- Loss β -$5
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
bet_input = gr.Number(label="Enter your bet (2-12)")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
play_btn = gr.Button("π― Place Bet")
|
| 109 |
+
stop_btn = gr.Button("π Stop")
|
| 110 |
+
|
| 111 |
+
output = gr.Textbox(label="Game Output")
|
| 112 |
+
total_score = gr.Number(label="π° Total Winnings ($)")
|
| 113 |
+
history_box = gr.Textbox(label="π Game History")
|
| 114 |
+
|
| 115 |
+
play_btn.click(play_game, inputs=bet_input, outputs=[output, total_score, history_box])
|
| 116 |
+
stop_btn.click(stop_game, outputs=[output, total_score, history_box])
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Required for Hugging Face Spaces
|
| 120 |
+
app = demo
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# ----------------------
|
| 124 |
+
# requirements.txt (create separately in repo)
|
| 125 |
+
# ----------------------
|
| 126 |
+
# gradio
|
| 127 |
+
# langgraph
|