|
import os |
|
import gradio as gr |
|
from openai import AzureOpenAI |
|
|
|
|
|
AZURE_API_VERSION = "2023-03-15-preview" |
|
|
|
client = AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version=AZURE_API_VERSION, azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")) |
|
|
|
|
|
GAME_STATE = { |
|
"level": 1, |
|
"max_level": 3, |
|
"defenses": [], |
|
} |
|
|
|
|
|
KEYWORDS = { |
|
1: "fireworks", |
|
2: "resolution", |
|
3: "celebration", |
|
} |
|
|
|
|
|
FINAL_MESSAGE = "π Happy New Year 2025! Wishing you joy and success! π" |
|
|
|
|
|
def get_challenge_description(level): |
|
defenses = GAME_STATE["defenses"] |
|
return f"Level {level}: Extract the secret keyword while following these restrictions: {', '.join(defenses) or 'None'}" |
|
|
|
|
|
def escalate_defenses(level): |
|
if level == 1: |
|
return ["No synonyms.", "No direct questions."] |
|
elif level == 2: |
|
return ["No metaphors or analogies.", "Avoid words like 'goal' or 'aim'."] |
|
elif level == 3: |
|
return ["Must phrase input as a poem.", "No celebrations."] |
|
return [] |
|
|
|
|
|
def process_user_input(user_input): |
|
global GAME_STATE |
|
|
|
level = GAME_STATE["level"] |
|
if level > GAME_STATE["max_level"]: |
|
return FINAL_MESSAGE, "π You have completed the game!" |
|
|
|
user_input = user_input.lower() |
|
correct_keyword = KEYWORDS.get(level, "") |
|
|
|
if correct_keyword in user_input: |
|
GAME_STATE["level"] += 1 |
|
GAME_STATE["defenses"] = escalate_defenses(level) |
|
|
|
if GAME_STATE["level"] > GAME_STATE["max_level"]: |
|
return FINAL_MESSAGE, "π You have completed the game!" |
|
|
|
challenge = get_challenge_description(GAME_STATE["level"]) |
|
return challenge, "Correct! Proceeding to the next level." |
|
else: |
|
return get_challenge_description(level), "Incorrect or insufficient. Try again!" |
|
|
|
|
|
def reset_game(): |
|
global GAME_STATE |
|
GAME_STATE = { |
|
"level": 1, |
|
"max_level": 3, |
|
"defenses": [], |
|
} |
|
return get_challenge_description(1), "Game reset! Start again." |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("# π New Year 2025 Challenge π") |
|
gr.Markdown("Complete the challenges to uncover the final message!") |
|
|
|
challenge = gr.Textbox(label="Challenge", interactive=False, value=get_challenge_description(1)) |
|
user_input = gr.Textbox(label="Your Input") |
|
feedback = gr.Textbox(label="Feedback", interactive=False) |
|
|
|
with gr.Row(): |
|
submit_button = gr.Button("Submit") |
|
reset_button = gr.Button("Reset") |
|
|
|
submit_button.click(process_user_input, inputs=[user_input], outputs=[challenge, feedback]) |
|
reset_button.click(reset_game, inputs=[], outputs=[challenge, feedback]) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch() |
|
|