alan5543 commited on
Commit
132d45b
·
1 Parent(s): adcc516
Files changed (3) hide show
  1. Dockerfile +4 -4
  2. app.py +46 -30
  3. requirements.txt +3 -3
Dockerfile CHANGED
@@ -1,16 +1,16 @@
1
  FROM python:3.10
2
 
3
- # Set working directory
4
 
5
- WORKDIR /app
6
 
7
  # Copy requirements and install dependencies
8
 
9
- COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt
10
 
11
  # Copy the application code
12
 
13
- COPY app.py .
14
 
15
  # Expose the port
16
 
 
1
  FROM python:3.10
2
 
3
+ # Set working directory and ensure it's clean
4
 
5
+ WORKDIR /app RUN rm -rf /app/* # Clear any existing files to avoid conflicts
6
 
7
  # Copy requirements and install dependencies
8
 
9
+ COPY requirements.txt /app/requirements.txt RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt
10
 
11
  # Copy the application code
12
 
13
+ COPY app.py /app/app.py
14
 
15
  # Expose the port
16
 
app.py CHANGED
@@ -10,7 +10,6 @@ logger = logging.getLogger(__name__)
10
  # Initialize FastMCP server
11
  try:
12
  mcp = FastMCP("gradio-spaces")
13
- # Log attributes of mcp to debug
14
  logger.info("Attributes of mcp: %s", dir(mcp))
15
  except Exception as e:
16
  logger.error("Failed to initialize FastMCP: %s", str(e))
@@ -23,7 +22,11 @@ def get_client(space_id: str) -> Client:
23
  """Get or create a Gradio client for the specified space."""
24
  if space_id not in clients:
25
  logger.info("Creating Gradio client for space: %s", space_id)
26
- clients[space_id] = Client(space_id)
 
 
 
 
27
  return clients[space_id]
28
 
29
  @mcp.tool()
@@ -37,19 +40,23 @@ async def generate_image(prompt: str, space_id: str = "ysharma/SanaSprint") -> s
37
  str: URL or path to the generated image
38
  """
39
  logger.info("Generating image with prompt: %s, space: %s", prompt, space_id)
40
- client = get_client(space_id)
41
- result = client.predict(
42
- prompt=prompt,
43
- model_size="1.6B",
44
- seed=0,
45
- randomize_seed=True,
46
- width=1024,
47
- height=1024,
48
- guidance_scale=4.5,
49
- num_inference_steps=2,
50
- api_name="/infer"
51
- )
52
- return result
 
 
 
 
53
 
54
  @mcp.tool()
55
  async def run_dia_tts(prompt: str, space_id: str = "ysharma/Dia-1.6B") -> str:
@@ -62,27 +69,36 @@ async def run_dia_tts(prompt: str, space_id: str = "ysharma/Dia-1.6B") -> str:
62
  str: URL or path to the generated audio
63
  """
64
  logger.info("Generating TTS with prompt: %s, space: %s", prompt, space_id)
65
- client = get_client(space_id)
66
- result = client.predict(
67
- text_input=f"{prompt}",
68
- audio_prompt_input=None,
69
- max_new_tokens=3072,
70
- cfg_scale=3,
71
- temperature=1.3,
72
- top_p=0.95,
73
- cfg_filter_top_k=30,
74
- speed_factor=0.94,
75
- api_name="/generate_audio"
76
- )
77
- return result
 
 
 
 
78
 
79
  # Use mcp.sse_app as the FastAPI application
80
  try:
81
  fastapi_app = mcp.sse_app # Use SSE app for MCP server
82
  logger.info("Using mcp.sse_app as the FastAPI application")
83
  except AttributeError:
84
- logger.error("mcp.sse_app not found; FastMCP configuration may be incorrect")
85
- raise
 
 
 
 
 
86
 
87
  if __name__ == "__main__":
88
  uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
 
10
  # Initialize FastMCP server
11
  try:
12
  mcp = FastMCP("gradio-spaces")
 
13
  logger.info("Attributes of mcp: %s", dir(mcp))
14
  except Exception as e:
15
  logger.error("Failed to initialize FastMCP: %s", str(e))
 
22
  """Get or create a Gradio client for the specified space."""
23
  if space_id not in clients:
24
  logger.info("Creating Gradio client for space: %s", space_id)
25
+ try:
26
+ clients[space_id] = Client(space_id) # Add hf_token if private Spaces are used
27
+ except Exception as e:
28
+ logger.error("Failed to create Gradio client for %s: %s", space_id, str(e))
29
+ raise
30
  return clients[space_id]
31
 
32
  @mcp.tool()
 
40
  str: URL or path to the generated image
41
  """
42
  logger.info("Generating image with prompt: %s, space: %s", prompt, space_id)
43
+ try:
44
+ client = get_client(space_id)
45
+ result = client.predict(
46
+ prompt=prompt,
47
+ model_size="1.6B",
48
+ seed=0,
49
+ randomize_seed=True,
50
+ width=1024,
51
+ height=1024,
52
+ guidance_scale=4.5,
53
+ num_inference_steps=2,
54
+ api_name="/infer"
55
+ )
56
+ return result
57
+ except Exception as e:
58
+ logger.error("Failed to generate image: %s", str(e))
59
+ raise
60
 
61
  @mcp.tool()
62
  async def run_dia_tts(prompt: str, space_id: str = "ysharma/Dia-1.6B") -> str:
 
69
  str: URL or path to the generated audio
70
  """
71
  logger.info("Generating TTS with prompt: %s, space: %s", prompt, space_id)
72
+ try:
73
+ client = get_client(space_id)
74
+ result = client.predict(
75
+ text_input=f"{prompt}",
76
+ audio_prompt_input=None,
77
+ max_new_tokens=3072,
78
+ cfg_scale=3,
79
+ temperature=1.3,
80
+ top_p=0.95,
81
+ cfg_filter_top_k=30,
82
+ speed_factor=0.94,
83
+ api_name="/generate_audio"
84
+ )
85
+ return result
86
+ except Exception as e:
87
+ logger.error("Failed to generate TTS: %s", str(e))
88
+ raise
89
 
90
  # Use mcp.sse_app as the FastAPI application
91
  try:
92
  fastapi_app = mcp.sse_app # Use SSE app for MCP server
93
  logger.info("Using mcp.sse_app as the FastAPI application")
94
  except AttributeError:
95
+ logger.error("mcp.sse_app not found; trying streamable_http_app")
96
+ try:
97
+ fastapi_app = mcp.streamable_http_app # Fallback to HTTP app
98
+ logger.info("Using mcp.streamable_http_app as the FastAPI application")
99
+ except AttributeError:
100
+ logger.error("mcp.streamable_http_app not found; FastMCP configuration may be incorrect")
101
+ raise
102
 
103
  if __name__ == "__main__":
104
  uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- mcp>=1.0.0
2
- gradio-client>=1.0.0
3
- uvicorn>=0.30.0
4
  fastapi>=0.115.0
 
1
+ mcp>=1.0.0
2
+ gradio-client>=1.0.0
3
+ uvicorn>=0.30.0
4
  fastapi>=0.115.0