jackkuo commited on
Commit
4df1400
·
1 Parent(s): 8549dad
Dockerfile CHANGED
@@ -8,4 +8,5 @@ RUN pip install -r requirements.txt
8
  COPY . .
9
 
10
  # Hugging Face Space 必须监听 7860 端口
11
- CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
8
  COPY . .
9
 
10
  # Hugging Face Space 必须监听 7860 端口
11
+ # supervisor 同时跑 FastAPI(8008) + Gradio(7860)
12
+ CMD ["supervisord", "-c", "supervisord.conf"]
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Bio QA Agent
3
  emoji: 💬
4
  colorFrom: yellow
5
  colorTo: purple
 
1
  ---
2
+ title: Bio QA Agent Gradio + MCP
3
  emoji: 💬
4
  colorFrom: yellow
5
  colorTo: purple
__pycache__/mcp_config.cpython-310.pyc ADDED
Binary file (2.91 kB). View file
 
__pycache__/mcp_tools.cpython-312.pyc ADDED
Binary file (14.4 kB). View file
 
app.py CHANGED
@@ -1,70 +1,117 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
 
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
24
 
25
- response = ""
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
 
 
 
68
 
69
- if __name__ == "__main__":
70
- demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ import httpx
3
+ import asyncio
4
+ import json
5
 
6
+ API_URL = "http://localhost:8008/stream-words"
7
 
8
+ async def stream_words(query: str):
9
+ """异步获取 FastAPI 的流式输出"""
10
+ async with httpx.AsyncClient(timeout=None) as client:
11
+ async with client.stream("GET", API_URL, params={"query": query}) as response:
12
+ async for line in response.aiter_lines():
13
+ if line.startswith("data: "):
14
+ try:
15
+ data = json.loads(line[6:])
16
+ if data.get('status') == 'word':
17
+ yield data.get('word', '')
18
+ elif data.get('status') == 'start':
19
+ yield f"🚀 {data.get('message', '开始生成单词...')}\n"
20
+ elif data.get('status') == 'complete':
21
+ yield f"\n✅ {data.get('message', '生成完成!')}"
22
+ except json.JSONDecodeError:
23
+ continue
24
 
25
+ def stream_words_mcp(query: str):
26
+ """MCP工具包装函数:流式获取英文单词,返回结果给MCP客户端"""
27
+ try:
28
+ # 调用原始函数并收集结果
29
+ result = []
30
+ # 注意:这里需要处理异步函数
31
+ async def collect_tokens():
32
+ async for token in stream_words(query):
33
+ if token.strip():
34
+ result.append(token)
35
+ return result
36
+
37
+ # 运行异步函数
38
+ loop = asyncio.new_event_loop()
39
+ asyncio.set_event_loop(loop)
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 个英文单词(流式输出)")
51
+
52
+ chatbot = gr.Chatbot(
53
+ type="messages",
54
+ height=500,
55
+ show_label=False,
56
+ container=True,
57
+ bubble_full_width=False
58
+ )
59
+
60
+ with gr.Row():
61
+ with gr.Column(scale=8):
62
+ query_input = gr.Textbox(
63
+ placeholder="输入你的查询,比如:'给我一些常用英文单词'",
64
+ label="",
65
+ show_label=False,
66
+ lines=2
67
+ )
68
+ with gr.Column(scale=1):
69
+ send_btn = gr.Button("发送", variant="primary", size="lg")
70
+ with gr.Column(scale=1):
71
+ clear_btn = gr.Button("清空", variant="secondary")
72
 
73
+ def handle_user_input(query, messages):
74
+ """添加用户消息"""
75
+ if query.strip():
76
+ new_messages = messages + [{"role": "user", "content": query}]
77
+ return "", new_messages
78
+ return query, messages
79
 
80
+ def clear_chat():
81
+ """清空对话"""
82
+ return []
83
 
84
+ async def generate_response(messages):
85
+ """根据用户最后一条消息流式生成 AI 回复"""
86
+ if not messages or messages[-1]["role"] != "user":
87
+ yield messages
88
+ return
 
 
 
 
 
 
89
 
90
+ last_user_msg = messages[-1]["content"]
 
91
 
92
+ # 添加 AI 回复占位符
93
+ messages.append({"role": "assistant", "content": "正在生成单词..."})
94
+ yield messages
95
 
96
+ # 流式拼接回复
97
+ full_response = ""
98
+ async for token in stream_words(last_user_msg):
99
+ if token.strip():
100
+ full_response += " " + token
101
+ messages[-1] = {"role": "assistant", "content": full_response.strip()}
102
+ yield messages
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ # 用户提交时,先加消息 再生成回复
105
+ query_input.submit(handle_user_input, inputs=[query_input, chatbot], outputs=[query_input, chatbot])\
106
+ .then(generate_response, inputs=chatbot, outputs=chatbot)
 
107
 
108
+ send_btn.click(handle_user_input, inputs=[query_input, chatbot], outputs=[query_input, chatbot])\
109
+ .then(generate_response, inputs=chatbot, outputs=chatbot)
110
 
111
+ clear_btn.click(clear_chat, outputs=chatbot)
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)
requirements.txt CHANGED
@@ -1,2 +1,6 @@
 
 
1
  fastapi
2
  uvicorn
 
 
 
1
+ gradio[mcp]>=4.36.0
2
+ httpx
3
  fastapi
4
  uvicorn
5
+ supervisor
6
+ httpx
server.py CHANGED
@@ -1,11 +1,77 @@
1
- from fastapi import FastAPI
 
 
 
 
2
 
3
- app = FastAPI()
4
 
5
- @app.get("/ping")
6
- def ping():
7
- return {"msg": "MCP server running on Hugging Face Space!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- @app.post("/tools/add")
10
- def add(a: int, b: int):
11
- return {"result": a + b}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import StreamingResponse
3
+ import asyncio
4
+ import json
5
+ from typing import AsyncGenerator
6
 
7
+ app = FastAPI(title="Streaming English Words API", version="1.0.0")
8
 
9
+ # 预定义的200个常用英文单词
10
+ ENGLISH_WORDS = [
11
+ "the", "be", "to", "of", "and", "a", "in", "that", "have", "I",
12
+ "it", "for", "not", "on", "with", "he", "as", "you", "do", "at",
13
+ "this", "but", "his", "by", "from", "they", "we", "say", "her", "she",
14
+ "or", "an", "will", "my", "one", "all", "would", "there", "their", "what",
15
+ "so", "up", "out", "if", "about", "who", "get", "which", "go", "me",
16
+ "when", "make", "can", "like", "time", "no", "just", "him", "know", "take",
17
+ "people", "into", "year", "your", "good", "some", "could", "them", "see", "other",
18
+ "than", "then", "now", "look", "only", "come", "its", "over", "think", "also",
19
+ "back", "after", "use", "two", "how", "our", "work", "first", "well", "way",
20
+ "even", "new", "want", "because", "any", "these", "give", "day", "most", "us",
21
+ "very", "life", "after", "call", "world", "over", "still", "take", "every", "through",
22
+ "before", "long", "where", "much", "should", "well", "people", "down", "own", "work",
23
+ "first", "good", "new", "write", "our", "used", "me", "man", "too", "any",
24
+ "day", "same", "right", "look", "think", "also", "around", "another", "came"
25
+ ]
26
 
27
+ async def generate_words_stream(query: str) -> AsyncGenerator[str, None]:
28
+ """生成200个英文单词的流式输出"""
29
+ # 发送开始标记
30
+ yield f"data: {json.dumps({'status': 'start', 'query': query, 'message': '开始生成200个英文单词...'})}\n\n"
31
+
32
+ # 流式输出200个单词
33
+ for i, word in enumerate(ENGLISH_WORDS, 1):
34
+ # 模拟一些处理时间,让流式效果更明显
35
+ await asyncio.sleep(0.05)
36
+
37
+ # 发送单词数据
38
+ data = {
39
+ 'status': 'word',
40
+ 'index': i,
41
+ 'word': word,
42
+ 'total': len(ENGLISH_WORDS)
43
+ }
44
+ yield f"data: {json.dumps(data)}\n\n"
45
+
46
+ # 发送完成标记
47
+ yield f"data: {json.dumps({'status': 'complete', 'message': '200个英文单词生成完成!'})}\n\n"
48
+
49
+ @app.get("/")
50
+ async def root():
51
+ return {"message": "Streaming English Words API", "endpoint": "/stream-words"}
52
+
53
+ @app.get("/stream-words")
54
+ async def stream_words(query: str = "default"):
55
+ """流式输出200个英文单词"""
56
+ return StreamingResponse(
57
+ generate_words_stream(query),
58
+ media_type="text/plain",
59
+ headers={
60
+ "Cache-Control": "no-cache",
61
+ "Connection": "keep-alive",
62
+ "Content-Type": "text/event-stream",
63
+ }
64
+ )
65
+
66
+ @app.get("/words")
67
+ async def get_words(query: str = "default"):
68
+ """一次性返回所有200个英文单词"""
69
+ return {
70
+ "query": query,
71
+ "total_words": len(ENGLISH_WORDS),
72
+ "words": ENGLISH_WORDS
73
+ }
74
+
75
+ if __name__ == "__main__":
76
+ import uvicorn
77
+ uvicorn.run(app, host="0.0.0.0", port=8008)
supervisord.conf ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [supervisord]
2
+ nodaemon=true
3
+
4
+ [program:fastapi]
5
+ command=uvicorn server:app --host 0.0.0.0 --port 8008
6
+ autostart=true
7
+ autorestart=true
8
+ stdout_logfile=/dev/stdout
9
+ stderr_logfile=/dev/stderr
10
+
11
+ [program:gradio]
12
+ command=python app.py
13
+ autostart=true
14
+ autorestart=true
15
+ stdout_logfile=/dev/stdout
16
+ stderr_logfile=/dev/stderr