Spaces:
Sleeping
Sleeping
v2
Browse files
app.py
CHANGED
@@ -21,13 +21,20 @@ game_state = {
|
|
21 |
'current_coin': 0
|
22 |
}
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Load AI model
|
25 |
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
26 |
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
27 |
|
28 |
@app.route('/')
|
29 |
def index():
|
30 |
-
return render_template('index.html', coins=coins, game_state=game_state)
|
31 |
|
32 |
@app.route('/flip', methods=['POST'])
|
33 |
def flip_coin():
|
@@ -37,6 +44,11 @@ def flip_coin():
|
|
37 |
if is_heads:
|
38 |
game_state['balance'] += coin['value']
|
39 |
game_state['flips_left'] -= 1
|
|
|
|
|
|
|
|
|
|
|
40 |
return jsonify({
|
41 |
'result': 'H' if is_heads else 'T',
|
42 |
'balance': game_state['balance'],
|
@@ -47,7 +59,7 @@ def flip_coin():
|
|
47 |
@app.route('/buy', methods=['POST'])
|
48 |
def buy_coin():
|
49 |
index = int(request.json['index'])
|
50 |
-
if index < len(coins) and game_state['balance'] >= coins[index]['price']:
|
51 |
game_state['balance'] -= coins[index]['price']
|
52 |
game_state['current_coin'] = index
|
53 |
return jsonify({'success': True, 'balance': game_state['balance']})
|
@@ -55,24 +67,83 @@ def buy_coin():
|
|
55 |
|
56 |
@app.route('/generate_coin', methods=['POST'])
|
57 |
def generate_coin():
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
inputs = tokenizer(prompt, return_tensors="pt")
|
61 |
with torch.no_grad():
|
62 |
-
outputs = model.generate(**inputs, max_length=
|
63 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
|
65 |
try:
|
66 |
new_coin = json.loads(response)
|
67 |
-
if
|
68 |
coins.append(new_coin)
|
69 |
-
|
|
|
|
|
|
|
70 |
else:
|
71 |
-
print("Error: Generated coin does not have all required properties")
|
72 |
return jsonify({'success': False, 'error': 'Invalid coin format'})
|
73 |
except json.JSONDecodeError:
|
74 |
-
print("Error: Could not parse generated coin as JSON")
|
75 |
return jsonify({'success': False, 'error': 'Invalid JSON format'})
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
if __name__ == '__main__':
|
78 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
21 |
'current_coin': 0
|
22 |
}
|
23 |
|
24 |
+
# Load leaderboard
|
25 |
+
try:
|
26 |
+
with open('leaderboard.json', 'r') as f:
|
27 |
+
leaderboard = json.load(f)
|
28 |
+
except FileNotFoundError:
|
29 |
+
leaderboard = []
|
30 |
+
|
31 |
# Load AI model
|
32 |
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
33 |
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
34 |
|
35 |
@app.route('/')
|
36 |
def index():
|
37 |
+
return render_template('index.html', coins=coins, game_state=game_state, leaderboard=leaderboard)
|
38 |
|
39 |
@app.route('/flip', methods=['POST'])
|
40 |
def flip_coin():
|
|
|
44 |
if is_heads:
|
45 |
game_state['balance'] += coin['value']
|
46 |
game_state['flips_left'] -= 1
|
47 |
+
|
48 |
+
# Apply coin ability if it exists
|
49 |
+
if 'ability' in coin:
|
50 |
+
ability_effect(coin['ability'])
|
51 |
+
|
52 |
return jsonify({
|
53 |
'result': 'H' if is_heads else 'T',
|
54 |
'balance': game_state['balance'],
|
|
|
59 |
@app.route('/buy', methods=['POST'])
|
60 |
def buy_coin():
|
61 |
index = int(request.json['index'])
|
62 |
+
if index < len(coins) and game_state['balance'] >= coins[index]['price'] and index != game_state['current_coin']:
|
63 |
game_state['balance'] -= coins[index]['price']
|
64 |
game_state['current_coin'] = index
|
65 |
return jsonify({'success': True, 'balance': game_state['balance']})
|
|
|
67 |
|
68 |
@app.route('/generate_coin', methods=['POST'])
|
69 |
def generate_coin():
|
70 |
+
if game_state['balance'] < 4:
|
71 |
+
return jsonify({'success': False, 'error': 'Not enough balance to generate a coin'})
|
72 |
+
|
73 |
+
prompt = """Generate a new coin for a game in JSON format with the following properties:
|
74 |
+
name (a creative name for the coin),
|
75 |
+
color (as a hex code),
|
76 |
+
price (higher than 10),
|
77 |
+
value (higher than 0.1),
|
78 |
+
winrate (between 0.5 and 0.8),
|
79 |
+
ability (a unique effect that modifies game state, e.g., 'add 5 flips', 'double next flip value', etc.)
|
80 |
+
|
81 |
+
Example:
|
82 |
+
{
|
83 |
+
"name": "Lucky Clover",
|
84 |
+
"color": "#2ecc71",
|
85 |
+
"price": 15,
|
86 |
+
"value": 0.2,
|
87 |
+
"winrate": 0.65,
|
88 |
+
"ability": "add 3 flips on every 10th flip"
|
89 |
+
}
|
90 |
+
"""
|
91 |
|
92 |
inputs = tokenizer(prompt, return_tensors="pt")
|
93 |
with torch.no_grad():
|
94 |
+
outputs = model.generate(**inputs, max_length=300)
|
95 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
96 |
|
97 |
try:
|
98 |
new_coin = json.loads(response)
|
99 |
+
if validate_coin(new_coin):
|
100 |
coins.append(new_coin)
|
101 |
+
with open('coins.json', 'w') as f:
|
102 |
+
json.dump(coins, f)
|
103 |
+
game_state['balance'] -= 4
|
104 |
+
return jsonify({'success': True, 'coin': new_coin, 'balance': game_state['balance']})
|
105 |
else:
|
|
|
106 |
return jsonify({'success': False, 'error': 'Invalid coin format'})
|
107 |
except json.JSONDecodeError:
|
|
|
108 |
return jsonify({'success': False, 'error': 'Invalid JSON format'})
|
109 |
|
110 |
+
@app.route('/game_over', methods=['POST'])
|
111 |
+
def game_over():
|
112 |
+
initials = request.json['initials']
|
113 |
+
score = game_state['balance']
|
114 |
+
leaderboard.append({'initials': initials, 'score': score})
|
115 |
+
leaderboard.sort(key=lambda x: x['score'], reverse=True)
|
116 |
+
leaderboard = leaderboard[:10] # Keep only top 10
|
117 |
+
with open('leaderboard.json', 'w') as f:
|
118 |
+
json.dump(leaderboard, f)
|
119 |
+
return jsonify({'success': True})
|
120 |
+
|
121 |
+
@app.route('/reset_game', methods=['POST'])
|
122 |
+
def reset_game():
|
123 |
+
game_state['balance'] = 0
|
124 |
+
game_state['flips_left'] = 1000
|
125 |
+
game_state['current_coin'] = 0
|
126 |
+
return jsonify({'success': True})
|
127 |
+
|
128 |
+
def validate_coin(coin):
|
129 |
+
required_keys = ['name', 'color', 'price', 'value', 'winrate', 'ability']
|
130 |
+
return all(key in coin for key in required_keys) and \
|
131 |
+
isinstance(coin['name'], str) and \
|
132 |
+
isinstance(coin['color'], str) and coin['color'].startswith('#') and \
|
133 |
+
isinstance(coin['price'], (int, float)) and coin['price'] > 10 and \
|
134 |
+
isinstance(coin['value'], (int, float)) and coin['value'] > 0.1 and \
|
135 |
+
isinstance(coin['winrate'], (int, float)) and 0.5 <= coin['winrate'] <= 0.8 and \
|
136 |
+
isinstance(coin['ability'], str)
|
137 |
+
|
138 |
+
def ability_effect(ability):
|
139 |
+
# Implement ability effects here
|
140 |
+
if "add" in ability and "flips" in ability:
|
141 |
+
flips_to_add = int(ability.split()[1])
|
142 |
+
game_state['flips_left'] += flips_to_add
|
143 |
+
elif "double next flip value" in ability:
|
144 |
+
# This would need to be handled in the flip_coin function
|
145 |
+
pass
|
146 |
+
# Add more ability effects as needed
|
147 |
+
|
148 |
if __name__ == '__main__':
|
149 |
+
app.run(host='0.0.0.0', port=7860)
|