Download inference.py from NeerjaK/wordle: direct link, hf CLI and curl.
- Browser
- Download file 3.39 kB
-
https://huggingface.co/spaces/NeerjaK/wordle/resolve/main/inference.py
- Command line
-
hf download hf://spaces/NeerjaK/wordle/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/NeerjaK/wordle/resolve/main/inference.py
3.39 kB
| """ | |
| agent.py | |
| Wordle Baseline Agent using Gemini API. | |
| Plays a full Wordle game using Gemini as the guesser, connecting to the | |
| local Wordle environment server at http://127.0.0.1:8000. | |
| Usage: | |
| pip install google-generativeai | |
| set GEMINI_API_KEY=your_key_here | |
| python agent.py | |
| """ | |
| import os | |
| import re | |
| import google.generativeai as genai | |
| from wordle import WordleEnv | |
| from wordle.models import WordleAction | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
| WORDLE_SERVER_URL = "http://127.0.0.1:8000" | |
| GEMINI_MODEL = "gemini-2.5-flash" | |
| MAX_GUESSES = 6 | |
| genai.configure(api_key=GEMINI_API_KEY) | |
| model = genai.GenerativeModel(GEMINI_MODEL) | |
| SYSTEM_PROMPT = """You are an expert Wordle player. You must guess a secret 5-letter English word. | |
| After each guess you receive feedback per letter: | |
| G = Correct letter, correct position (Green) | |
| Y = Correct letter, wrong position (Yellow) | |
| X = Letter not in the word (Gray) | |
| Respond with ONLY the 5-letter word guess. No explanation, no punctuation.""" | |
| def build_prompt(feedback_history: list[dict]) -> str: | |
| """Build the prompt from guess history.""" | |
| if not feedback_history: | |
| return "This is the start of a new game. Make your first guess (a common 5-letter English word)." | |
| lines = ["Here is the Wordle game so far:"] | |
| for i, entry in enumerate(feedback_history): | |
| feedback_str = " ".join(entry["feedback"]) | |
| lines.append(f" Guess {i+1}: {entry['guess']} → [{feedback_str}]") | |
| lines.append(f"\nYou have {MAX_GUESSES - len(feedback_history)} guess(es) left. What is your next guess?") | |
| return "\n".join(lines) | |
| def parse_guess(text: str) -> str: | |
| """Extract a clean 5-letter word from Gemini's response.""" | |
| text = text.strip().upper() | |
| match = re.search(r"\b([A-Z]{5})\b", text) | |
| if match: | |
| return match.group(1) | |
| alpha = re.sub(r"[^A-Z]", "", text) | |
| return alpha[:5] if len(alpha) >= 5 else "CRANE" | |
| def play_game(): | |
| """Play a single Wordle game using Gemini as the agent.""" | |
| feedback_history = [] | |
| total_reward = 0.0 | |
| with WordleEnv(base_url=WORDLE_SERVER_URL).sync() as env: | |
| obs = env.reset() | |
| print("=" * 40) | |
| print(f"🟩 New game started: {obs.observation.message}") | |
| print("=" * 40) | |
| for turn in range(MAX_GUESSES): | |
| prompt = f"{SYSTEM_PROMPT}\n\n{build_prompt(feedback_history)}" | |
| response = model.generate_content(prompt) | |
| guess = parse_guess(response.text) | |
| result = env.step(WordleAction(guess=guess)) | |
| obs = result.observation | |
| total_reward += result.reward | |
| feedback_str = " | ".join(obs.feedback) | |
| print(f"Turn {turn+1}: Guess={guess} Feedback=[{feedback_str}] Reward={result.reward:+.1f}") | |
| feedback_history.append({"guess": guess, "feedback": obs.feedback}) | |
| if result.done: | |
| print("=" * 40) | |
| if obs.is_win: | |
| print(f"✅ Gemini WON in {turn+1} guess(es)! Total reward: {total_reward:.1f}") | |
| else: | |
| print(f"❌ Gemini LOST. {obs.message} Total reward: {total_reward:.1f}") | |
| print("=" * 40) | |
| return | |
| print(f"\n❌ Ran out of guesses. Total reward: {total_reward:.1f}") | |
| if __name__ == "__main__": | |
| play_game() | |