RPS / app.py
Hari022's picture
Update app.py
bec7cb3 verified
import streamlit as st
from sklearn.linear_model import LogisticRegression
import numpy as np
import random
# Mappings
move_map = {"R": 0, "P": 1, "S": 2}
reverse_map = {0: "R", 1: "P", 2: "S"}
emoji_map = {"R": "๐Ÿชจ Rock", "P": "๐Ÿ“„ Paper", "S": "โœ‚๏ธ Scissors"}
counter_map = {"R": "P", "P": "S", "S": "R"}
# Train model inline
def generate_training_data():
pattern = ["R", "P", "S", "R", "P", "R", "S", "S", "P", "R"]
X, y = [], []
for i in range(len(pattern) - 3):
X.append([move_map[pattern[i]], move_map[pattern[i+1]], move_map[pattern[i+2]]])
y.append(move_map[pattern[i+3]])
return np.array(X), np.array(y)
def train_model():
X, y = generate_training_data()
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)
model.fit(X, y)
return model
model = train_model()
def predict_ai_move(history):
if len(history) < 3:
return random.choice(["R", "P", "S"])
recent = [move_map[m] for m in history[-3:]]
pred = model.predict([recent])[0]
predicted = reverse_map[pred]
return counter_map[predicted]
# Session state
if "user_history" not in st.session_state:
st.session_state.user_history = []
st.session_state.user_score = 0
st.session_state.ai_score = 0
st.session_state.result = ""
st.session_state.last_user = ""
st.session_state.last_ai = ""
# Streamlit UI
st.set_page_config(page_title="ML RPS", layout="centered")
st.markdown("<h1 style='text-align:center;'>๐ŸŽฎ Rock-Paper-Scissors: ML Edition</h1>", unsafe_allow_html=True)
st.markdown("<h4 style='text-align:center;'>๐Ÿง  Logistic Regression AI Learns You</h4>", unsafe_allow_html=True)
st.markdown("---")
st.markdown("### ๐Ÿ‘‰ Your Move:")
col1, col2, col3 = st.columns(3)
clicked = None
with col1:
if st.button("๐Ÿชจ Rock"):
clicked = "R"
with col2:
if st.button("๐Ÿ“„ Paper"):
clicked = "P"
with col3:
if st.button("โœ‚๏ธ Scissors"):
clicked = "S"
# Game logic
if clicked:
user = clicked
ai = predict_ai_move(st.session_state.user_history)
st.session_state.user_history.append(user)
if user == ai:
result = "๐Ÿค It's a Draw!"
color = "orange"
elif counter_map[user] == ai:
st.session_state.ai_score += 1
result = "๐Ÿ˜Ž AI Wins!"
color = "red"
else:
st.session_state.user_score += 1
result = "๐ŸŽ‰ You Win!"
color = "green"
st.balloons()
st.session_state.result = result
st.session_state.last_user = emoji_map[user]
st.session_state.last_ai = emoji_map[ai]
# Results
st.markdown("---")
if st.session_state.result:
st.markdown(f"<div style='text-align:center; font-size:20px;'>๐Ÿง‘ You: {st.session_state.last_user} &nbsp;&nbsp;&nbsp; ๐Ÿค– AI: {st.session_state.last_ai}</div>", unsafe_allow_html=True)
st.markdown(f"<div style='text-align:center; font-size:26px; font-weight:bold; color:{color}; margin-top:10px;'>{st.session_state.result}</div>", unsafe_allow_html=True)
# Scoreboard
st.markdown("---")
st.markdown("### ๐Ÿงฎ Scoreboard")
col1, col2 = st.columns(2)
col1.metric("You", st.session_state.user_score)
col2.metric("AI", st.session_state.ai_score)
st.markdown("<center><small>๐Ÿš€ Powered by real-time machine learning</small></center>", unsafe_allow_html=True)