Spaces:
Sleeping
Sleeping
Quentin Gallouédec
commited on
Commit
·
ee53a4b
1
Parent(s):
0323e88
lib!!!
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class WordleEnv:
|
| 5 |
+
"""
|
| 6 |
+
Demonstration env. Not a full game; 4-letter variant for brevity.
|
| 7 |
+
|
| 8 |
+
Observations are emoji strings; actions are 4-letter lowercase words.
|
| 9 |
+
Reward is 1.0 on success, else 0.0. Terminal on success or after 6 guesses.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
dictionary = {"word", "wood", "ward", "sore", "bore", "bake", "bake", "bake", "earn"}
|
| 13 |
+
|
| 14 |
+
def __init__(self, max_guesses: int = 6) -> None:
|
| 15 |
+
self._max = max_guesses
|
| 16 |
+
|
| 17 |
+
def reset(self) -> str:
|
| 18 |
+
self._secret = random.choice(list(self.dictionary))
|
| 19 |
+
self._n = 0
|
| 20 |
+
self._obs = "⬜" * 4
|
| 21 |
+
return self._obs
|
| 22 |
+
|
| 23 |
+
def step(self, action: str) -> tuple[str, float, bool]:
|
| 24 |
+
guess: str = str(action)
|
| 25 |
+
guess = guess.strip().lower()
|
| 26 |
+
|
| 27 |
+
if len(guess) != 4 or not guess.isalpha():
|
| 28 |
+
raise ValueError("Action must be a 4-letter lowercase word.")
|
| 29 |
+
|
| 30 |
+
if self._n >= self._max:
|
| 31 |
+
raise RuntimeError("Episode is done. Call reset() to start a new episode.")
|
| 32 |
+
|
| 33 |
+
self._n += 1
|
| 34 |
+
secret = self._secret
|
| 35 |
+
feedback: list[str] = []
|
| 36 |
+
for i, ch in enumerate(guess):
|
| 37 |
+
if ch == secret[i]:
|
| 38 |
+
feedback.append("🟩")
|
| 39 |
+
elif ch in secret:
|
| 40 |
+
feedback.append("🟨")
|
| 41 |
+
else:
|
| 42 |
+
feedback.append("⬜")
|
| 43 |
+
self._obs = "".join(feedback)
|
| 44 |
+
done = guess == secret or self._n >= self._max
|
| 45 |
+
reward = 1.0 if guess == secret else 0.0
|
| 46 |
+
return self._obs, reward, done
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
from my_lib import get_demo
|
| 50 |
+
|
| 51 |
+
demo = get_demo(WordleEnv)
|
| 52 |
+
demo.launch(mcp_server=True)
|
my_lib.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
+
import uuid
|
| 3 |
+
from functools import wraps
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def bind_method_to_session(method, registry: dict):
|
| 9 |
+
sig = inspect.signature(method)
|
| 10 |
+
params = list(sig.parameters.values())
|
| 11 |
+
|
| 12 |
+
@wraps(method)
|
| 13 |
+
def wrapper(session_id: str, *args, **kwargs):
|
| 14 |
+
instance = registry.get(session_id)
|
| 15 |
+
if instance is None:
|
| 16 |
+
raise ValueError(f"Invalid session_id: {session_id}")
|
| 17 |
+
m = getattr(instance, method.__func__.__name__)
|
| 18 |
+
return m(*args, **kwargs)
|
| 19 |
+
|
| 20 |
+
# --- update __annotations__ ---
|
| 21 |
+
wrapper.__annotations__ = method.__annotations__.copy()
|
| 22 |
+
wrapper.__annotations__["session_id"] = str
|
| 23 |
+
|
| 24 |
+
# --- build signature ---
|
| 25 |
+
new_params = (
|
| 26 |
+
inspect.Parameter(
|
| 27 |
+
"session_id",
|
| 28 |
+
kind=inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
| 29 |
+
annotation=str,
|
| 30 |
+
),
|
| 31 |
+
*params,
|
| 32 |
+
)
|
| 33 |
+
wrapper.__signature__ = inspect.Signature(
|
| 34 |
+
parameters=new_params,
|
| 35 |
+
return_annotation=sig.return_annotation,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return wrapper
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get_demo(env_cls: type) -> gr.Blocks:
|
| 42 |
+
sessions = {} # just a dict now
|
| 43 |
+
|
| 44 |
+
def init_env() -> str:
|
| 45 |
+
session_id = str(uuid.uuid4())
|
| 46 |
+
env = env_cls()
|
| 47 |
+
sessions[session_id] = env
|
| 48 |
+
return session_id
|
| 49 |
+
|
| 50 |
+
# Bind methods to session dict
|
| 51 |
+
reset_api = bind_method_to_session(env_cls().reset, sessions)
|
| 52 |
+
step_api = bind_method_to_session(env_cls().step, sessions)
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.api(init_env, api_name="init")
|
| 56 |
+
gr.api(reset_api, api_name="reset")
|
| 57 |
+
gr.api(step_api, api_name="step")
|
| 58 |
+
|
| 59 |
+
return demo
|