Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from components import GamePlay, Player, Dealer, Deck
|
4 |
+
|
5 |
+
# PersistDataset -----
|
6 |
+
import os
|
7 |
+
import csv
|
8 |
+
|
9 |
+
import huggingface_hub
|
10 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
11 |
+
from datetime import datetime
|
12 |
+
|
13 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Carddata.csv"
|
14 |
+
DATASET_REPO_ID = "awacke1/Carddata.csv"
|
15 |
+
DATA_FILENAME = "Carddata.csv"
|
16 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
17 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
18 |
+
# overriding/appending to the gradio template
|
19 |
+
|
20 |
+
def generate_html() -> str:
|
21 |
+
with open(DATA_FILE) as csvfile:
|
22 |
+
reader = csv.DictReader(csvfile)
|
23 |
+
rows = []
|
24 |
+
for row in reader:
|
25 |
+
rows.append(row)
|
26 |
+
rows.reverse()
|
27 |
+
if len(rows) == 0:
|
28 |
+
return "no messages yet"
|
29 |
+
else:
|
30 |
+
html = "<div class='chatbot'>"
|
31 |
+
for row in rows:
|
32 |
+
html += "<div>"
|
33 |
+
html += f"<span>{row['name']}</span>"
|
34 |
+
html += f"<span class='message'>{row['message']}</span>"
|
35 |
+
html += "</div>"
|
36 |
+
html += "</div>"
|
37 |
+
return html
|
38 |
+
|
39 |
+
def store_message(name: str, message: str):
|
40 |
+
if name and message:
|
41 |
+
with open(DATA_FILE, "a") as csvfile:
|
42 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
43 |
+
writer.writerow(
|
44 |
+
{"name": name, "message": message, "time": str(datetime.now())}
|
45 |
+
)
|
46 |
+
commit_url = repo.push_to_hub()
|
47 |
+
return generate_html()
|
48 |
+
|
49 |
+
# Game settings
|
50 |
+
number_of_decks = 6
|
51 |
+
blackjack_multiplier = 1.5
|
52 |
+
|
53 |
+
# Initialize player, dealer, deck and game play. Cache these variables
|
54 |
+
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
|
55 |
+
def start_game():
|
56 |
+
game_deck = Deck(number_of_decks)
|
57 |
+
dealer = Dealer()
|
58 |
+
player = Player()
|
59 |
+
game_play = GamePlay(player, dealer, game_deck, blackjack_multiplier)
|
60 |
+
return game_deck, dealer, player, game_play
|
61 |
+
|
62 |
+
game_deck, dealer, player, game_play = start_game()
|
63 |
+
|
64 |
+
st.title('🃏Blackjack Simulator AI♠2️⃣1️⃣')
|
65 |
+
|
66 |
+
if st.button('New hand?'):
|
67 |
+
game_play.deal_in()
|
68 |
+
|
69 |
+
player_stats = st.empty()
|
70 |
+
player_images = st.empty()
|
71 |
+
player_hit_option = st.empty()
|
72 |
+
player_double_down_option = st.empty()
|
73 |
+
player_stand_option = st.empty()
|
74 |
+
dealer_stats = st.empty()
|
75 |
+
dealer_images = st.empty()
|
76 |
+
result = st.empty()
|
77 |
+
|
78 |
+
|
79 |
+
if 'Hit' in player.possible_actions:
|
80 |
+
if player_hit_option.button('Hit'):
|
81 |
+
player.player_hit(game_deck, game_play)
|
82 |
+
if 'Hit' not in player.possible_actions:
|
83 |
+
player_hit_option.empty()
|
84 |
+
if 'Double Down' in player.possible_actions:
|
85 |
+
if player_double_down_option.button('Double Down'):
|
86 |
+
player.double_down(game_deck, game_play)
|
87 |
+
player_double_down_option.empty()
|
88 |
+
player_hit_option.empty()
|
89 |
+
player_stand_option.empty()
|
90 |
+
if 'Stand' in player.possible_actions:
|
91 |
+
if player_stand_option.button('Stand'):
|
92 |
+
player.stand(game_play)
|
93 |
+
player_hit_option.empty()
|
94 |
+
player_double_down_option.empty()
|
95 |
+
player_stand_option.empty()
|
96 |
+
|
97 |
+
|
98 |
+
game_play.update()
|
99 |
+
player_stats.write(player)
|
100 |
+
player_images.image([Image.open(card.image_location)
|
101 |
+
for card in player.cards], width=100)
|
102 |
+
dealer_stats.write(dealer)
|
103 |
+
dealer_images.image([Image.open(card.image_location)
|
104 |
+
for card in dealer.cards], width=100)
|
105 |
+
|
106 |
+
result.write(game_play)
|