jackkuo commited on
Commit
460107d
·
1 Parent(s): 0469268
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -40,11 +40,20 @@ def stream_words_mcp(query: str):
40
  tokens = loop.run_until_complete(collect_tokens())
41
  loop.close()
42
 
43
- # 返回结果给MCP客户端
44
- return f"成功生成 {len(tokens)} 个英文单词: {', '.join(tokens[:10])}{'...' if len(tokens) > 10 else ''}"
45
  except Exception as e:
46
  return f"生成单词时出错: {str(e)}"
47
 
 
 
 
 
 
 
 
 
 
48
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
49
  gr.Markdown("# 🧠 English Words Generator")
50
  gr.Markdown("输入你的查询,AI 将为你生成 200 个英文单词(流式输出)")
@@ -112,6 +121,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
112
 
113
  # 注册MCP工具 - 使用包装函数并正确配置输出
114
  demo.load(stream_words_mcp, inputs=None, outputs=gr.Textbox(label="MCP结果", visible=False))
 
 
115
 
116
  # 开启 MCP server
117
  demo.launch(mcp_server=True, server_port=7860)
 
40
  tokens = loop.run_until_complete(collect_tokens())
41
  loop.close()
42
 
43
+ # 返回结果给MCP客户端 - 返回全部单词
44
+ return f"成功生成 {len(tokens)} 个英文单词: {tokens}"
45
  except Exception as e:
46
  return f"生成单词时出错: {str(e)}"
47
 
48
+ async def stream_words_mcp_stream(query: str):
49
+ """MCP工具:流式返回英文单词"""
50
+ try:
51
+ async for token in stream_words(query):
52
+ if token.strip():
53
+ yield token
54
+ except Exception as e:
55
+ yield f"生成单词时出错: {str(e)}"
56
+
57
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
  gr.Markdown("# 🧠 English Words Generator")
59
  gr.Markdown("输入你的查询,AI 将为你生成 200 个英文单词(流式输出)")
 
121
 
122
  # 注册MCP工具 - 使用包装函数并正确配置输出
123
  demo.load(stream_words_mcp, inputs=None, outputs=gr.Textbox(label="MCP结果", visible=False))
124
+ # 注册流式MCP工具
125
+ demo.load(stream_words_mcp_stream, inputs=None, outputs=gr.Textbox(label="流式MCP结果", visible=False))
126
 
127
  # 开启 MCP server
128
  demo.launch(mcp_server=True, server_port=7860)