|
import streamlit as st |
|
from huggingface_hub import InferenceClient |
|
|
|
from wordgame import WordGame |
|
|
|
|
|
if "wordgame" not in st.session_state: |
|
st.session_state.wordgame = WordGame() |
|
|
|
st.title("The Word Game") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_name = "DrDomedag/HumanDialoguev2" |
|
|
|
|
|
|
|
client = InferenceClient(model_name, timeout=300) |
|
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [{"role": "system", "content": "You are a helpful assistant."}] |
|
|
|
|
|
user_input = st.text_input("Type your message here...", key="user_input") |
|
|
|
|
|
if st.button("Send"): |
|
if user_input.strip(): |
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
with st.spinner("The robot is thinking..."): |
|
response = client.text_generation(user_input) |
|
|
|
assistant_reply = response.strip() |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": assistant_reply}) |
|
|
|
print(assistant_reply) |
|
|
|
if not st.session_state.wordgame.check_input_for_word(user_input.strip().lower()): |
|
print("Target word not in user input.") |
|
check_result = st.session_state.wordgame.check_output_for_word(assistant_reply) |
|
if "Success!" in check_result: |
|
st.balloons() |
|
st.session_state.messages.append({"role": "game", "content": check_result}) |
|
else: |
|
print("Target word found in user input.") |
|
st.session_state.messages.append({"role": "game", "content": "You're not allowed to input the target word yourself, that's cheating! You just lost one point!"}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for message in reversed(st.session_state.messages): |
|
if message["role"] == "user": |
|
st.markdown(f"**You:** {message['content']}") |
|
elif message["role"] == "assistant": |
|
st.markdown(f"**Robot:** {message['content']}") |
|
elif message["role"] == "game": |
|
st.markdown(f"**Game:** {message['content']}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with st.sidebar: |
|
st.write(f"Current score: {st.session_state.wordgame.points}") |
|
st.write(f"Remaining attempts: {st.session_state.wordgame.attempts}") |
|
st.write(f"Target word: **{st.session_state.wordgame.target_word}**") |
|
|
|
st.info(f'The goal of the game is to make the model say the target word. You are not allowed to input the word yourself. Other than that, input any query and click "Send"! You get three points if you make it in one try, two points if you make it in two, and one point if it takes you three tries. If you do not win in three attempts, you will get zero points and a new target word.') |