Quentin Gallouédec commited on
Commit
939668a
·
1 Parent(s): 5f019b6

simplify gradio

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -12
  2. README.md +1 -1
  3. app.py +12 -38
  4. my_lib.py +0 -62
Dockerfile DELETED
@@ -1,12 +0,0 @@
1
- FROM python:3.10
2
-
3
- RUN useradd -m -u 1000 user
4
- USER user
5
- ENV PATH="/home/user/.local/bin:$PATH"
6
-
7
- WORKDIR /app
8
-
9
- RUN pip install --no-cache-dir --upgrade mcp fastapi
10
-
11
- COPY --chown=user . /app
12
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -3,7 +3,7 @@ title: Counter
3
  emoji: 🏆
4
  colorFrom: green
5
  colorTo: indigo
6
- sdk: docker
7
  app_file: app.py
8
  pinned: false
9
  ---
 
3
  emoji: 🏆
4
  colorFrom: green
5
  colorTo: indigo
6
+ sdk: gradio
7
  app_file: app.py
8
  pinned: false
9
  ---
app.py CHANGED
@@ -1,44 +1,18 @@
1
- from collections.abc import AsyncIterator
2
- from contextlib import asynccontextmanager
3
- from dataclasses import dataclass
4
 
5
- from mcp.server.fastmcp import Context, FastMCP
6
- from mcp.server.session import ServerSession
7
-
8
-
9
- class CounterEnv:
10
- _counter = 0
11
-
12
- @classmethod
13
- async def connect(cls) -> "CounterEnv":
14
- return cls()
15
 
16
  def step(self) -> str:
17
- self._counter += 1
18
- return f"Step result {self._counter}"
19
-
20
- async def close(self) -> None:
21
- pass
22
-
23
- @dataclass
24
- class AppContext:
25
- env: CounterEnv
26
-
27
-
28
- @asynccontextmanager
29
- async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]:
30
- env = await CounterEnv.connect()
31
- try:
32
- yield AppContext(env=env)
33
- finally:
34
- await env.close()
35
 
36
- mcp = FastMCP("My App", lifespan=app_lifespan)
37
 
38
- @mcp.tool()
39
- def step(ctx: Context[ServerSession, AppContext]) -> str:
40
- env = ctx.request_context.lifespan_context.env
41
- return env.step()
42
 
43
- if __name__ == "__main__":
44
- mcp.run('streamable-http')
 
1
+ import gradio as gr
 
 
2
 
3
+ class Counter:
4
+ def reset(self) -> str:
5
+ self.value = 0
6
+ return "Counter reset to 0"
 
 
 
 
 
 
7
 
8
  def step(self) -> str:
9
+ self.value += 1
10
+ return f"Counter is at {self.value}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ counter = Counter()
13
 
14
+ with gr.Blocks() as demo:
15
+ gr.api(counter.reset, api_name="/reset")
16
+ gr.api(counter.step, api_name="/step")
 
17
 
18
+ demo.launch(mcp_server=True)
 
my_lib.py DELETED
@@ -1,62 +0,0 @@
1
- from abc import ABC, abstractmethod
2
- from contextlib import asynccontextmanager
3
- from dataclasses import dataclass
4
- from collections.abc import AsyncIterator
5
- from mcp.server.fastmcp import Context, FastMCP
6
- from mcp.server.session import ServerSession
7
-
8
- class Environment(ABC):
9
- """Simple synchronous interface for environments."""
10
-
11
- async def connect(self) -> "Environment":
12
- """
13
- Optional async setup.
14
- Defaults to returning self if no connection is needed.
15
- """
16
- return self
17
-
18
- async def close(self) -> None:
19
- """Optional async cleanup."""
20
- return
21
-
22
- @abstractmethod
23
- def reset(self) -> str:
24
- """Reset the environment and return an initial state."""
25
- ...
26
-
27
- @abstractmethod
28
- def step(self) -> str:
29
- """Take one step and return a result."""
30
- ...
31
-
32
-
33
-
34
- @dataclass
35
- class _EnvCtx:
36
- env: Environment
37
-
38
- def create_mcp_app(name: str, env_cls: type[Environment]) -> FastMCP:
39
- """
40
- Build a FastMCP app around an Environment subclass.
41
- Handles async lifecycle automatically.
42
- """
43
-
44
- @asynccontextmanager
45
- async def lifespan(server: FastMCP) -> AsyncIterator[_EnvCtx]:
46
- env = await env_cls().connect()
47
- try:
48
- yield _EnvCtx(env)
49
- finally:
50
- await env.close()
51
-
52
- mcp = FastMCP(name, lifespan=lifespan)
53
-
54
- @mcp.tool()
55
- def reset(ctx: Context[ServerSession, _EnvCtx]) -> str:
56
- return ctx.request_context.lifespan_context.env.reset()
57
-
58
- @mcp.tool()
59
- def step(ctx: Context[ServerSession, _EnvCtx]) -> str:
60
- return ctx.request_context.lifespan_context.env.step()
61
-
62
- return mcp