MindSearch CPU-only 版部署

基础任务

按照教程,将 MindSearch 部署到 HuggingFace 并美化 Gradio 的界面,并提供截图和 Hugging Face 的Space的链接。

创建开发机 & 环境配置

打开codespace主页,选择blank template。

Codespaces (github.com)

浏览器会自动在新的页面打开一个web版的vscode。

 

接下来的操作就和我们使用vscode基本没差别了。

然后我们新建一个目录用于存放 MindSearch 的相关代码,并把 MindSearch 仓库 clone 下来。在终端中运行下面的命令:

mkdir -p /workspaces/mindsearch
cd /workspaces/mindsearch
git clone https://github.com/InternLM/MindSearch.git
cd MindSearch && git checkout b832275 && cd ..

 

接下来,我们创建一个 conda 环境来安装相关依赖。

# 创建环境
conda create -n mindsearch python=3.10 -y
# 激活环境
conda activate mindsearch
# 安装依赖
pip install -r /workspaces/mindsearch/MindSearch/requirements.txt

在激活遇到一点小问题

首先,运行以下命令来初始化 Conda 环境:

conda init

 执行完后,关闭并重新打开终端,或者运行以下命令重新加载 shell 配置:

source ~/.bashrc  # 如果你使用的是 Bash
source ~/.zshrc   # 如果你使用的是 Zsh

再次尝试激活环境:

conda activate mindsearch

 

获取硅基流动 API Key

因为要使用硅基流动的 API Key,所以接下来便是注册并获取 API Key 了。

首先,我们打开 硅基流动统一登录 来注册硅基流动的账号(如果注册过,则直接登录即可)。

在完成注册后,打开 硅基流动统一登录 来准备 API Key。首先创建新 API 密钥,然后点击密钥进行复制,以备后续使用。

启动 MindSearch

 启动后端

由于硅基流动 API 的相关配置已经集成在了 MindSearch 中,所以我们可以直接执行下面的代码来启动 MindSearch 的后端。

export SILICON_API_KEY=第二步中复制的密钥
conda activate mindsearch
cd /workspaces/mindsearch/MindSearch
python -m mindsearch.app --lang cn --model_format internlm_silicon --search_engine DuckDuckGoSearch

启动前端

在后端启动完成后,我们打开新终端运行如下命令来启动 MindSearch 的前端。

conda activate mindsearch
cd /workspaces/mindsearch/MindSearch
python frontend/mindsearch_gradio.py

前后端都启动后,我们应该可以看到github自动为这两个进程做端口转发。

由于使用codespace,这里我们不需要使用ssh端口转发了,github会自动提示我们打开一个在公网的前端地址。 

如果遇到了 timeout 的问题,可以按照 文档 换用 Bing 的搜索接口。

部署到 HuggingFace Space

最后,我们来将 MindSearch 部署到 HuggingFace Space。

我们首先打开 https://huggingface.co/spaces ,并点击 Create new Space,如下图所示。

在输入 Space name 并选择 License 后,选择配置如下所示。 

然后,我们进入 Settings,配置硅基流动的 API Key。如下图所示。 

选择 New secrets,name 一栏输入 SILICON_API_KEY,value 一栏输入你的 API Key 的内容。 最后,我们先新建一个目录,准备提交到 HuggingFace Space 的全部文件。

# 创建新目录
mkdir -p /workspaces/mindsearch/mindsearch_deploy
# 准备复制文件
cd /workspaces/mindsearch
cp -r /workspaces/mindsearch/MindSearch/mindsearch /workspaces/mindsearch/mindsearch_deploy
cp /workspaces/mindsearch/MindSearch/requirements.txt /workspaces/mindsearch/mindsearch_deploy
# 创建 app.py 作为程序入口
touch /workspaces/mindsearch/mindsearch_deploy/app.py

app.py 的内容如下:

import json
import os

import gradio as gr
import requests
from lagent.schema import AgentStatusCode

os.system("python -m mindsearch.app --lang cn --model_format internlm_silicon &")

PLANNER_HISTORY = []
SEARCHER_HISTORY = []


def rst_mem(history_planner: list, history_searcher: list):
    '''
    Reset the chatbot memory.
    '''
    history_planner = []
    history_searcher = []
    if PLANNER_HISTORY:
        PLANNER_HISTORY.clear()
    return history_planner, history_searcher


def format_response(gr_history, agent_return):
    if agent_return['state'] in [
            AgentStatusCode.STREAM_ING, AgentStatusCode.ANSWER_ING
    ]:
        gr_history[-1][1] = agent_return['response']
    elif agent_return['state'] == AgentStatusCode.PLUGIN_START:
        thought = gr_history[-1][1].split('```')[0]
        if agent_return['response'].startswith('```'):
            gr_history[-1][1] = thought + '\n' + agent_return['response']
    elif agent_return['state'] == AgentStatusCode.PLUGIN_END:
        thought = gr_history[-1][1].split('```')[0]
        if isinstance(agent_return['response'], dict):
            gr_history[-1][
                1] = thought + '\n' + f'```json\n{json.dumps(agent_return["response"], ensure_ascii=False, indent=4)}\n```'  # noqa: E501
    elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:
        assert agent_return['inner_steps'][-1]['role'] == 'environment'
        item = agent_return['inner_steps'][-1]
        gr_history.append([
            None,
            f"```json\n{json.dumps(item['content'], ensure_ascii=False, indent=4)}\n```"
        ])
        gr_history.append([None, ''])
    return


def predict(history_planner, history_searcher):

    def streaming(raw_response):
        for chunk in raw_response.iter_lines(chunk_size=8192,
                                             decode_unicode=False,
                                             delimiter=b'\n'):
            if chunk:
                decoded = chunk.decode('utf-8')
                if decoded == '\r':
                    continue
                if decoded[:6] == 'data: ':
                    decoded = decoded[6:]
                elif decoded.startswith(': ping - '):
                    continue
                response = json.loads(decoded)
                yield (response['response'], response['current_node'])

    global PLANNER_HISTORY
    PLANNER_HISTORY.append(dict(role='user', content=history_planner[-1][0]))
    new_search_turn = True

    url = 'http://localhost:8002/solve'
    headers = {'Content-Type': 'application/json'}
    data = {'inputs': PLANNER_HISTORY}
    raw_response = requests.post(url,
                                 headers=headers,
                                 data=json.dumps(data),
                                 timeout=20,
                                 stream=True)

    for resp in streaming(raw_response):
        agent_return, node_name = resp
        if node_name:
            if node_name in ['root', 'response']:
                continue
            agent_return = agent_return['nodes'][node_name]['detail']
            if new_search_turn:
                history_searcher.append([agent_return['content'], ''])
                new_search_turn = False
            format_response(history_searcher, agent_return)
            if agent_return['state'] == AgentStatusCode.END:
                new_search_turn = True
            yield history_planner, history_searcher
        else:
            new_search_turn = True
            format_response(history_planner, agent_return)
            if agent_return['state'] == AgentStatusCode.END:
                PLANNER_HISTORY = agent_return['inner_steps']
            yield history_planner, history_searcher
    return history_planner, history_searcher


with gr.Blocks() as demo:
    gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")
    gr.HTML("""<p style="text-align: center; font-family: Arial, sans-serif;">MindSearch is an open-source AI Search Engine Framework with Perplexity.ai Pro performance. You can deploy your own Perplexity.ai-style search engine using either closed-source LLMs (GPT, Claude) or open-source LLMs (InternLM2.5-7b-chat).</p>""")
    gr.HTML("""
    <div style="text-align: center; font-size: 16px;">
        <a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">🔗 GitHub</a>
        <a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📄 Arxiv</a>
        <a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📚 Hugging Face Papers</a>
        <a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;">🤗 Hugging Face Demo</a>
    </div>
    """)
    with gr.Row():
        with gr.Column(scale=10):
            with gr.Row():
                with gr.Column():
                    planner = gr.Chatbot(label='planner',
                                         height=700,
                                         show_label=True,
                                         show_copy_button=True,
                                         bubble_full_width=False,
                                         render_markdown=True)
                with gr.Column():
                    searcher = gr.Chatbot(label='searcher',
                                          height=700,
                                          show_label=True,
                                          show_copy_button=True,
                                          bubble_full_width=False,
                                          render_markdown=True)
            with gr.Row():
                user_input = gr.Textbox(show_label=False,
                                        placeholder='帮我搜索一下 InternLM 开源体系',
                                        lines=5,
                                        container=False)
            with gr.Row():
                with gr.Column(scale=2):
                    submitBtn = gr.Button('Submit')
                with gr.Column(scale=1, min_width=20):
                    emptyBtn = gr.Button('Clear History')

    def user(query, history):
        return '', history + [[query, '']]

    submitBtn.click(user, [user_input, planner], [user_input, planner],
                    queue=False).then(predict, [planner, searcher],
                                      [planner, searcher])
    emptyBtn.click(rst_mem, [planner, searcher], [planner, searcher],
                   queue=False)

demo.queue()
demo.launch(server_name='0.0.0.0',
            server_port=7860,
            inbrowser=True,
            share=True)

在最后,将 /root/mindsearch/mindsearch_deploy 目录下的文件(使用 git)提交到 HuggingFace Space 即可完成部署了。将代码提交到huggingface space的流程如下:

首先创建一个有写权限的token。

然后从huggingface把空的代码仓库clone到codespace。

/workspaces/codespaces-blank
git clone https://huggingface.co/spaces/<你的名字>/<仓库名称>
# 把token挂到仓库上,让自己有写权限
git remote set-url space https://<你的名字>:<上面创建的token>@huggingface.co/spaces/<你的名字>/<仓库名称>

 现在codespace就是本地仓库,huggingface space是远程仓库,接下来使用方法就和常规的git一样了。

cd <仓库名称>
# 把刚才准备的文件都copy进来
cp /workspaces/mindsearch/mindsearch_deploy/* .

最后把代码提交到huggingface space会自动启动项目。

git add .
git commit -m "update"
git push
(base) @nusakom ➜ /workspaces/codespaces-blank $ git clone https://huggingface.co/spaces/nusakom/123
Cloning into '123'...
remote: Enumerating objects: 4, done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 4 (from 1)
Unpacking objects: 100% (4/4), 1.27 KiB | 1.27 MiB/s, done.
(base) @nusakom ➜ /workspaces/codespaces-blank $ git remote set-url space https://nusakom:12345@huggingface.co/spaces/nusakom/123
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
(base) @nusakom ➜ /workspaces/codespaces-blank $ cd 123
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git remote set-url origin https://nusakom:12345@huggingface.co/spaces/nusakom/123
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git remote -v
origin  https://nusakom:12345@huggingface.co/spaces/nusakom/123 (fetch)
origin  https://nusakom:12345@huggingface.co/spaces/nusakom/123 (push)
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ cd 123
bash: cd: 123: No such file or directory
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ cp /workspaces/mindsearch/mindsearch_deploy/* .
cp: -r not specified; omitting directory '/workspaces/mindsearch/mindsearch_deploy/mindsearch'
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ cp -r /workspaces/mindsearch/mindsearch_deploy/* .
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git add .
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git commit -m "update"
[main 9c813dd] update
 Author: TENGY_Z <110544069+nusakom@users.noreply.github.com>
 13 files changed, 1237 insertions(+)
 create mode 100644 app.py
 create mode 100644 mindsearch/__pycache__/app.cpython-310.pyc
 create mode 100644 mindsearch/agent/__init__.py
 create mode 100644 mindsearch/agent/__pycache__/__init__.cpython-310.pyc
 create mode 100644 mindsearch/agent/__pycache__/mindsearch_agent.cpython-310.pyc
 create mode 100644 mindsearch/agent/__pycache__/mindsearch_prompt.cpython-310.pyc
 create mode 100644 mindsearch/agent/__pycache__/models.cpython-310.pyc
 create mode 100644 mindsearch/agent/mindsearch_agent.py
 create mode 100644 mindsearch/agent/mindsearch_prompt.py
 create mode 100644 mindsearch/agent/models.py
 create mode 100644 mindsearch/app.py
 create mode 100644 mindsearch/terminal.py
 create mode 100644 requirements.txt
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git push
remote: Password authentication in git is no longer supported. You must use a user access token or an SSH key instead. See https://huggingface.co/blog/password-git-deprecation
fatal: Authentication failed for 'https://huggingface.co/spaces/nusakom/123/'
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git remote set-url origin https://nusakom:<your_token>@huggingface.co/spaces/nusakom/123
bash: your_token: No such file or directory
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git remote set-url origin https://nusakom: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@huggingface.co/spaces/nusakom/123
(base) @nusakom ➜ /workspaces/codespaces-blank/123 (main) $ git push
Enumerating objects: 20, done.
Counting objects: 100% (20/20), done.
Delta compression using up to 2 threads
Compressing objects: 100% (18/18), done.
Writing objects: 100% (19/19), 27.86 KiB | 5.57 MiB/s, done.
Total 19 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
To https://huggingface.co/spaces/nusakom/123
   e5c2a54..9c813dd  main -> main

 

然后就可以测试啦。

 

hugface的部署的链接:123 - a Hugging Face Space by nusakom

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值