Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
import google.generativeai as genai | |
import os | |
GOOGLE_API_KEY = os.getenv("Gemini") | |
genai.configure(api_key=GOOGLE_API_KEY) | |
model = genai.GenerativeModel('gemini-pro') | |
# Define the moves | |
moves = ["Rock", "Paper", "Scissors"] | |
# Function to determine the winner | |
def determine_winner(player_move, computer_move): | |
if player_move == computer_move: | |
return "It's a tie!" | |
elif (player_move == "Rock" and computer_move == "Scissors") or \ | |
(player_move == "Paper" and computer_move == "Rock") or \ | |
(player_move == "Scissors" and computer_move == "Paper"): | |
st.session_state.playerWin+=1 | |
return "You win!" | |
else: | |
st.session_state.computerWin+=1 | |
return "You lose!" | |
# Streamlit app | |
st.markdown(""" | |
<style> | |
button { | |
height: 4rem; | |
width: 4rem; | |
font-size: 40px !important; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
st.title("Rock, Paper, Scissors") | |
st.write("Choose your move:") | |
# Create three horizontal buttons | |
col1, col2, col3 = st.columns(3) | |
moveEmojis = {"Rock": "✊", "Paper":"✋", "Scissors": "✌️"} | |
with col1: | |
if st.button("Rock ✊"): | |
player_move = "Rock" | |
with col2: | |
if st.button("Paper ✋"): | |
player_move = "Paper" | |
with col3: | |
if st.button("Scissors✌️"): | |
player_move = "Scissors" | |
# Ensure player_move is defined | |
if 'count' not in st.session_state: | |
st.session_state.count = 0 | |
if 'playerWin' not in st.session_state: | |
st.session_state.playerWin = 0 | |
if 'computerWin' not in st.session_state: | |
st.session_state.computerWin = 0 | |
if 'player_move' not in st.session_state: | |
st.session_state.player_move = None | |
# Assign player move to session state if a move is made | |
if 'player_move' in locals(): | |
st.session_state.player_move = player_move | |
if "chat" not in st.session_state: | |
st.session_state.chat = model.start_chat(history = [ | |
{ | |
"role": 'user', | |
"parts": 'You are playing a random rock paper scissors game. For rock will say "Rock", for paper will say "Paper" and for scissors will say "Scissors". Pick a random word out of three and reply in single word once I do my move.' | |
}, | |
{ | |
"role": 'model', | |
"parts": 'Alright, let us play a random Rock-Paper-Scissors game. Make your first move!' | |
} | |
]) | |
# If a move is selected, randomly choose a move for the computer and determine the result | |
if st.session_state.player_move: | |
st.session_state.count = st.session_state.count+1 | |
player_move = st.session_state.player_move | |
response = st.session_state.chat.send_message(player_move) | |
computer_move = response.text | |
# Display player and computer moves | |
col1, col2 = st.columns(2) | |
with col1: | |
st.write("Your move:") | |
st.subheader("{}: {}".format(player_move, moveEmojis[player_move])) | |
with col2: | |
st.write("Computer's move:") | |
st.subheader("{}: {}".format(computer_move, moveEmojis[computer_move])) | |
# Display result | |
result = determine_winner(player_move, computer_move) | |
st.write("Result:") | |
st.subheader(result) | |
if st.session_state.count == 3: | |
st.session_state.count = 0 | |
if st.session_state.computerWin > st.session_state.playerWin: | |
st.write("Best of Three result: Sorry You lose!") | |
else: | |
st.write("Best of Three result: Yayy you win !") | |
st.session_state.computerWin = 0 | |
st.session_state.playerWin = 0 | |