-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
109 lines (92 loc) · 3.7 KB
/
app.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
import pickle
import random
import os
import tensorflow as tf
import gdown
# Configure the app
st.set_page_config(
page_title="Guess the Next Word!",
page_icon="🎮",
layout="wide",
initial_sidebar_state="expanded",
)
# Google Drive file ID
file_id = '1VmxkGEFA6XQV3guHviwgHlb55EpHa6aG'
url = f'https://drive.google.com/uc?export=download&id={file_id}'
# Download model to the current working directory
output_path = 'next_words.keras'
gdown.download(url, output_path, quiet=False)
# Load the Keras model
# Check if the model is loaded
print("Model loaded successfully!")
try:
model = load_model(output_path)
tokenizer = pickle.load(open('token.pkl', 'rb'))
except Exception as e:
st.error("Error loading model or tokenizer. Please check your files.")
st.stop()
# Prediction function
def predict_word(text, context_length):
text_input = " ".join(text.split()[-context_length:])
sequence = tokenizer.texts_to_sequences([text_input])
sequence = np.array(sequence)
preds = model.predict(sequence)
predicted_index = np.argmax(preds, axis=-1)
predicted_word = None
for word, index in tokenizer.word_index.items():
if index == predicted_index:
predicted_word = word
break
return predicted_word
# Sidebar for navigation
st.sidebar.title("🎮 Guess the Next Word!")
st.sidebar.markdown("**Challenge yourself and guess the AI's next word prediction!**")
# App title
st.title("🎮 Guess the Next Word!")
st.markdown("### Enter a sentence, and let's see if you can guess what the AI predicts next!")
# Game setup
if "game_started" not in st.session_state:
st.session_state["game_started"] = False
st.session_state["score"] = 0
st.session_state["round"] = 1
# Start the game
if not st.session_state["game_started"]:
st.markdown("Click the button below to start the game!")
if st.button("Start Game 🎯"):
st.session_state["game_started"] = True
st.session_state["score"] = 0
st.session_state["round"] = 1
# Game logic
if st.session_state["game_started"]:
st.sidebar.write(f"**Round:** {st.session_state['round']} | **Score:** {st.session_state['score']}")
# User input
user_input = st.text_input("Type a sentence:", placeholder="Type your sentence here...")
# Predict the word
if user_input and st.button("Predict 🔮"):
with st.spinner("AI is predicting..."):
predicted_word = predict_word(user_input,4)
st.session_state["predicted_word"] = predicted_word
st.markdown("### 📝 Make your guess!")
# Guess the word
if "predicted_word" in st.session_state:
guess = st.text_input("What's your guess?")
if guess:
if guess.lower() == st.session_state["predicted_word"]:
st.success(f"🎉 Correct! The predicted word is **{st.session_state['predicted_word']}**.")
st.session_state["score"] += 1
else:
st.error(f"❌ Oops! The correct word was **{st.session_state['predicted_word']}**.")
st.session_state["round"] += 1
st.button("Next Round 🔄", on_click=lambda: st.session_state.pop("predicted_word", None))
# Scoreboard and restart option
st.sidebar.markdown("### 🎲 Game Controls")
if st.sidebar.button("Restart Game"):
st.session_state["game_started"] = False
st.session_state["score"] = 0
st.session_state["round"] = 1
# Footer
st.sidebar.markdown("---")
st.sidebar.markdown("### Built with 💻 by AI Enthusiasts")