File size: 2,866 Bytes
933256c
 
 
 
 
33bff5d
 
 
 
933256c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from flask import Flask, render_template, jsonify, request
import json
import random
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import os

# Set the TRANSFORMERS_CACHE environment variable
os.environ['TRANSFORMERS_CACHE'] = '/app/cache'

app = Flask(__name__)

# Load initial coins
with open('coins.json', 'r') as f:
    coins = json.load(f)

# Initialize game state
game_state = {
    'balance': 0,
    'flips_left': 1000,
    'current_coin': 0
}

# Load AI model
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")

@app.route('/')
def index():
    return render_template('index.html', coins=coins, game_state=game_state)

@app.route('/flip', methods=['POST'])
def flip_coin():
    if game_state['flips_left'] > 0:
        coin = coins[game_state['current_coin']]
        is_heads = random.random() < coin['winrate']
        if is_heads:
            game_state['balance'] += coin['value']
        game_state['flips_left'] -= 1
        return jsonify({
            'result': 'H' if is_heads else 'T',
            'balance': game_state['balance'],
            'flips_left': game_state['flips_left']
        })
    return jsonify({'error': 'No flips left'})

@app.route('/buy', methods=['POST'])
def buy_coin():
    index = int(request.json['index'])
    if index < len(coins) and game_state['balance'] >= coins[index]['price']:
        game_state['balance'] -= coins[index]['price']
        game_state['current_coin'] = index
        return jsonify({'success': True, 'balance': game_state['balance']})
    return jsonify({'success': False})

@app.route('/generate_coin', methods=['POST'])
def generate_coin():
    prompt = "Generate a new coin for a game in JSON format with the following properties: color (as a hex code), price, value, and winrate. The price should be higher than 10, the value should be higher than 0.1, and the winrate should be between 0.5 and 0.8."

    inputs = tokenizer(prompt, return_tensors="pt")
    with torch.no_grad():
        outputs = model.generate(**inputs, max_length=200)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    try:
        new_coin = json.loads(response)
        if all(key in new_coin for key in ['color', 'price', 'value', 'winrate']):
            coins.append(new_coin)
            return jsonify({'success': True, 'coin': new_coin})
        else:
            print("Error: Generated coin does not have all required properties")
            return jsonify({'success': False, 'error': 'Invalid coin format'})
    except json.JSONDecodeError:
        print("Error: Could not parse generated coin as JSON")
        return jsonify({'success': False, 'error': 'Invalid JSON format'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)