AlexMerigot's picture
Update app.py
02916c9 verified
import gradio as gr
# Load models
text_generator = gr.load("models/meta-llama/Meta-Llama-3-8B-Instruct")
stable_diffusion = gr.load("models/stabilityai/stable-diffusion-xl-base-1.0")
# Global variables for the game
word_to_guess = ""
ai_score = 0
user_score = 0
ai_turn = True # True if it's AI's turn to guess, False if it's user's turn to ask
# Function to start a new game
def start_game(user_word):
global word_to_guess, ai_score, user_score, ai_turn
word_to_guess = user_word
ai_score = 0
user_score = 0
ai_turn = True
game_state = f"The game has started! The word has been set. It's the AI's turn to ask questions."
return game_state, ai_score, user_score
# Function for AI to ask questions
def ai_ask_question(game_state, user_answer=""):
global ai_score, ai_turn, word_to_guess
if ai_turn:
# AI asks a question
ai_prompt = f"The word to guess is a common noun. Is it '{word_to_guess}'?"
ai_question = text_generator(ai_prompt)
ai_score += 1
ai_turn = False
game_state += f"\nAI: {ai_question}\n"
else:
# User responds
ai_turn = True
if user_answer.lower() == "yes" and word_to_guess.lower() == word_to_guess:
game_state += f"User guessed the word '{word_to_guess}' correctly! User score: {user_score}. Now it's the user's turn to guess a word."
else:
game_state += f"\nUser: {user_answer.capitalize()}\n"
return game_state, ai_score, user_score
# Function for user to ask questions
def user_ask_question(user_question, game_state):
global user_score, ai_turn, word_to_guess
if not ai_turn:
return "It's not your turn yet.", ai_score, user_score
# AI responds to user's question
user_score += 1
ai_prompt = f"The user asked: '{user_question}'. Respond with 'yes' or 'no'. The word to guess is '{word_to_guess}'."
ai_answer = text_generator(ai_prompt)
if ai_answer.lower() == "yes" and word_to_guess.lower() == word_to_guess:
game_state += f"User guessed the word '{word_to_guess}' correctly! User score: {user_score}. Game over."
else:
ai_turn = True
game_state += f"\nUser: {user_question}\nAI: {ai_answer.capitalize()}\n"
return game_state, ai_score, user_score
# Gradio interface
interface = gr.Interface(
fn=lambda user_word, user_answer, user_question, game_state: (
start_game(user_word) if user_word else (
ai_ask_question(game_state, user_answer) if user_answer else user_ask_question(user_question, game_state)
)
),
inputs=[
gr.Textbox(lines=1, placeholder="Set a word for the AI to guess (leave empty if continuing)...", label="Set Word"),
gr.Textbox(lines=1, placeholder="Answer the AI's question with yes or no (leave empty if not applicable)...", label="Answer AI"),
gr.Textbox(lines=1, placeholder="Ask a question to guess the AI's word (leave empty if not applicable)...", label="User Question"),
gr.Textbox(lines=10, placeholder="Current game state...", label="Game State", value="The game begins here.", interactive=False)
],
outputs=[
gr.Textbox(label="Updated Game State"),
gr.Number(label="AI Score"),
gr.Number(label="User Score")
],
title="Word Guessing Game",
description="Set a word for the AI to guess. The AI will ask yes/no questions. Answer them and try to have the AI guess the word. Then the roles will switch."
)
# Launch Gradio app
if __name__ == "__main__":
interface.launch()