Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,9 +43,8 @@ def flip_coin():
|
|
| 43 |
is_heads = random.random() < coin['winrate']
|
| 44 |
if is_heads:
|
| 45 |
game_state['balance'] += coin['value']
|
| 46 |
-
# Apply coin bonus
|
| 47 |
-
|
| 48 |
-
apply_bonus(coin['bonus'])
|
| 49 |
game_state['flips_left'] -= 1
|
| 50 |
|
| 51 |
return jsonify({
|
|
@@ -61,7 +60,7 @@ def buy_coin():
|
|
| 61 |
if index < len(coins) and game_state['balance'] >= coins[index]['price'] and index != game_state['current_coin']:
|
| 62 |
game_state['balance'] -= coins[index]['price']
|
| 63 |
game_state['current_coin'] = index
|
| 64 |
-
return jsonify({'success': True, 'balance': game_state['balance']})
|
| 65 |
return jsonify({'success': False})
|
| 66 |
|
| 67 |
@app.route('/generate_coin', methods=['POST'])
|
|
@@ -72,10 +71,10 @@ def generate_coin():
|
|
| 72 |
prompt = """Generate a new coin for a game in JSON format with the following properties:
|
| 73 |
name (a creative name for the coin),
|
| 74 |
color (as a hex code),
|
| 75 |
-
price (
|
| 76 |
-
value (
|
| 77 |
winrate (between 0.5 and 0.8),
|
| 78 |
-
bonus (a
|
| 79 |
|
| 80 |
Example:
|
| 81 |
{
|
|
@@ -84,16 +83,16 @@ def generate_coin():
|
|
| 84 |
"price": 15,
|
| 85 |
"value": 0.2,
|
| 86 |
"winrate": 0.65,
|
| 87 |
-
"bonus":
|
| 88 |
}
|
| 89 |
"""
|
| 90 |
|
| 91 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 92 |
-
with torch.no_grad():
|
| 93 |
-
outputs = model.generate(**inputs, max_length=300)
|
| 94 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 95 |
-
|
| 96 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
new_coin = json.loads(response)
|
| 98 |
if validate_coin(new_coin):
|
| 99 |
coins.append(new_coin)
|
|
@@ -103,8 +102,9 @@ def generate_coin():
|
|
| 103 |
return jsonify({'success': True, 'coin': new_coin, 'balance': game_state['balance']})
|
| 104 |
else:
|
| 105 |
return jsonify({'success': False, 'error': 'Invalid coin format'})
|
| 106 |
-
except
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
@app.route('/game_over', methods=['POST'])
|
| 110 |
def game_over():
|
|
@@ -129,18 +129,10 @@ def validate_coin(coin):
|
|
| 129 |
return all(key in coin for key in required_keys) and \
|
| 130 |
isinstance(coin['name'], str) and \
|
| 131 |
isinstance(coin['color'], str) and coin['color'].startswith('#') and \
|
| 132 |
-
isinstance(coin['price'], (int, float)) and coin['price']
|
| 133 |
-
isinstance(coin['value'], (int, float)) and coin['value']
|
| 134 |
isinstance(coin['winrate'], (int, float)) and 0.5 <= coin['winrate'] <= 0.8 and \
|
| 135 |
-
isinstance(coin['bonus'],
|
| 136 |
-
|
| 137 |
-
def apply_bonus(bonus):
|
| 138 |
-
if bonus.startswith('+'):
|
| 139 |
-
value, attribute = bonus.split()[0][1:], bonus.split()[1]
|
| 140 |
-
if attribute == 'flips_left':
|
| 141 |
-
game_state['flips_left'] += int(value)
|
| 142 |
-
elif attribute == 'balance':
|
| 143 |
-
game_state['balance'] += float(value)
|
| 144 |
|
| 145 |
if __name__ == '__main__':
|
| 146 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 43 |
is_heads = random.random() < coin['winrate']
|
| 44 |
if is_heads:
|
| 45 |
game_state['balance'] += coin['value']
|
| 46 |
+
# Apply coin bonus
|
| 47 |
+
game_state['balance'] += coin['bonus']
|
|
|
|
| 48 |
game_state['flips_left'] -= 1
|
| 49 |
|
| 50 |
return jsonify({
|
|
|
|
| 60 |
if index < len(coins) and game_state['balance'] >= coins[index]['price'] and index != game_state['current_coin']:
|
| 61 |
game_state['balance'] -= coins[index]['price']
|
| 62 |
game_state['current_coin'] = index
|
| 63 |
+
return jsonify({'success': True, 'balance': game_state['balance'], 'current_coin': index})
|
| 64 |
return jsonify({'success': False})
|
| 65 |
|
| 66 |
@app.route('/generate_coin', methods=['POST'])
|
|
|
|
| 71 |
prompt = """Generate a new coin for a game in JSON format with the following properties:
|
| 72 |
name (a creative name for the coin),
|
| 73 |
color (as a hex code),
|
| 74 |
+
price (between 10 and 100),
|
| 75 |
+
value (between 0.1 and 1.0),
|
| 76 |
winrate (between 0.5 and 0.8),
|
| 77 |
+
bonus (a flat $ value between 0.01 and 0.1)
|
| 78 |
|
| 79 |
Example:
|
| 80 |
{
|
|
|
|
| 83 |
"price": 15,
|
| 84 |
"value": 0.2,
|
| 85 |
"winrate": 0.65,
|
| 86 |
+
"bonus": 0.05
|
| 87 |
}
|
| 88 |
"""
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 92 |
+
with torch.no_grad():
|
| 93 |
+
outputs = model.generate(**inputs, max_length=300)
|
| 94 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 95 |
+
|
| 96 |
new_coin = json.loads(response)
|
| 97 |
if validate_coin(new_coin):
|
| 98 |
coins.append(new_coin)
|
|
|
|
| 102 |
return jsonify({'success': True, 'coin': new_coin, 'balance': game_state['balance']})
|
| 103 |
else:
|
| 104 |
return jsonify({'success': False, 'error': 'Invalid coin format'})
|
| 105 |
+
except Exception as e:
|
| 106 |
+
# Refund the player if coin generation fails
|
| 107 |
+
return jsonify({'success': False, 'error': str(e), 'balance': game_state['balance']})
|
| 108 |
|
| 109 |
@app.route('/game_over', methods=['POST'])
|
| 110 |
def game_over():
|
|
|
|
| 129 |
return all(key in coin for key in required_keys) and \
|
| 130 |
isinstance(coin['name'], str) and \
|
| 131 |
isinstance(coin['color'], str) and coin['color'].startswith('#') and \
|
| 132 |
+
isinstance(coin['price'], (int, float)) and 10 <= coin['price'] <= 100 and \
|
| 133 |
+
isinstance(coin['value'], (int, float)) and 0.1 <= coin['value'] <= 1.0 and \
|
| 134 |
isinstance(coin['winrate'], (int, float)) and 0.5 <= coin['winrate'] <= 0.8 and \
|
| 135 |
+
isinstance(coin['bonus'], (int, float)) and 0.01 <= coin['bonus'] <= 0.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if __name__ == '__main__':
|
| 138 |
app.run(host='0.0.0.0', port=7860)
|