### 使用pip安装MMEdu基础版 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/mmedu/installation.md.txt 安装MMEdu基础版,可以使用pip install mmedu或pip install MMEdu。 ```bash pip install mmedu ``` ```bash pip install MMEdu ``` -------------------------------- ### Launch Gradio interface with run_gradio Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/introduction.md.txt Starts a Gradio web interface for the loaded model. Requires installing the Gradio library with `pip3 install gradio` beforehand. The interface lets users select an image path and set a `score` threshold; images below the threshold are not drawn. ```python import BaseDeploy as bd model = bd(model_path) model.run_gradio() ``` -------------------------------- ### Complete segmentation workflow with segment_anything Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_hub/preset_task.md.txt This example shows the full workflow: importing the Workflow class, instantiating the segment_anything model, performing inference on an image, formatting the output, and visualizing/saving the masks and the segmented image. ```python from XEdu.hub import Workflow as wf # 导入库 seg = wf(task = 'segment_anything') # 实例化模型 img_path = 'demo/seg.jpg' # 指定进行推理的图像路径 masks, img = seg.inference(data=img_path, img_type='pil') # 进行模型推理 format_result = seg.format_output(lang='zh') # 将推理结果进行格式化输出 seg.show(masks[0]) # 可视化第一个分割掩码 seg.save(masks[0],"demo/first_mask.png") # 保存可视化第一个分割掩码为'first_mask.png' seg.show(img) # 显示分割后的图像,带有分割掩码和提升点或框prompt seg.save(img,"demo/mask_with_image.png")# 保存分割后的图像为'mask_with_image.png' ``` -------------------------------- ### 安装FastAPI:pip install FastAPI Source: https://xedu.readthedocs.io/zh-cn/latest/how_to_use/scitech_tools/fastapi.html 使用pip命令安装FastAPI库。 ```bash pip install FastAPI ``` -------------------------------- ### 安装XEduHub和ONNX依赖 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/how_to_use/scitech_tools/unihiker.md.txt 使用pip安装xedu-python、onnx和onnxruntime,用于在行空板上部署模型。安装成功后提示"Successfully installed xxxx"。 ```bash pip install xedu-python==0.2.0 onnx==1.13.0 onnxruntime==1.13.1 ``` -------------------------------- ### pip install labelme Source: https://xedu.readthedocs.io/zh-cn/latest/how_to_use/scitech_tools/data_labeling.html 安装 labelme 标注工具的命令,建议使用 Python 3.8 版本。 ```bash pip install labelme ``` -------------------------------- ### 更新BaseDeploy:pip install --upgrade BaseDeploy Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/installation.md.txt 使用pip升级BaseDeploy到最新版本,命令为pip install --upgrade BaseDeploy。 ```bash pip install --upgrade BaseDeploy ``` -------------------------------- ### 使用 run_gradio 启动 Gradio 部署 Source: https://xedu.readthedocs.io/zh-cn/latest/basedeploy/introduction.html 在载入模型后使用 run_gradio 函数一键启动 Gradio Web 界面。启动后输入界面可选择路径和设置 score 阈值,低于 score 的图片不会被绘制。使用前需安装 gradio 库:pip3 install gradio。 ```python import BaseDeploy as bd model = bd(model_path) model.run_gradio() ``` -------------------------------- ### XEdu one-click package root directory structure Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt Shows the root directory structure of the XEdu one-click installation package. Use this to understand where checkpoints, datasets, environments, and example code are located. ```plain XEdu ├── checkpoints ├── datasets ├── EasyDL ├── envs ├── utils ├── XEdu示例代码 ├── 教学资源 ├── bug解决脚本.bat ├── jupyter编辑器.bat ├── IDLE.bat ├── jupyter编辑器.bat ├── PythonTutor代码可视化.bat ├── Thonny编辑器.bat ├── XEdu简介 v1.6.pdf ├── 启动cmd.bat ``` -------------------------------- ### 编程闯关自动批改(Gradio + XEdu.LLM) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_llm/introduction.md.txt 构建一个基于 Gradio 的网页应用,实现编程闯关功能。使用 XEdu.LLM 的 Client 调用 qwen 模型,提供代码提示和自动批改功能,并支持多关卡。需要提前准备 tests.txt 文件,每行一道题目。 ```python import gradio as gr from XEdu.LLM import Client # 使用你的 API 密钥实例化客户端 provider = 'qwen' # 选择模型为阿里-通义千问 # 填写用户密钥,用于确定身份,若失效,请自行注册:https://dashscope.console.aliyun.com/apiKey api_key ='sk-946498*************32ae4' chatbot = Client(provider = provider, api_key = api_key) with open('tests.txt','r',encoding='UTF-8') as f: tests = f.readlines() print(tests) question_num = 0 question_max = len(tests) def check_code_tips(stu_id,question, code): # 调用推理接口,检查代码中的错误 prompt = f'你是我的编程助手,我要用python编程解决【 "{question}" 】,请给我一点提示。但禁止直接给我代码,只允许给我代码的一部分提示。' res = chatbot.inference(prompt) if stu_id == '': stu_id = 'logs' with open(str(stu_id)+'.txt','a') as f: f.write('[TIP]') f.write(question+'\n') f.write(str(res)+'\n') return res def check_code_answer(stu_id,question, code): # 拼接 prompt,调用推理接口,判断代码是否正确 code = "```python\n"+code+"\n```" #print(code) prompt = f'你是我的编程助手,我要用python编程解决【 "{question}" 】,我写的代码是:\n{code}\n请帮我判断我写的代码对不对,请按照下列要求检查代码:1.必须是python3语言;2.程序没有语法错误;3.程序没有格式错误;4.代码功能和题目要求一致。如果符合上述要求,请输出【回答正确,闯关成功】,如果不符合,则先输出【回答错误,请再次尝试】,再输出错误和提示,但禁止给我完整的代码。' res = chatbot.inference(prompt) global question_num,question_max if res[:4]=='回答正确': res = '回答正确,闯关成功!可以点击“下一关”!' if question_num+1>= question_max: res = '回答正确,闯关成功。恭喜通过全部关卡!' if stu_id == '': stu_id = 'logs' with open(str(stu_id)+'.txt','a') as f: f.write('[ANS]') f.write(question+'\n') f.write(code+'\n') f.write(str(res)+'\n') return res def next_question(question,res): global question_num if res[:10]=='回答正确,闯关成功!': question_num += 1 return tests[question_num],'' elif res[:10]=='回答正确,闯关成功。': return '闯关成功!恭喜通过全部关卡!','' else: return '未通过关卡,请继续作答:'+question,'' # 构建网页界面 with gr.Blocks() as demo: gr.Markdown("# XEduLLM项目:编程闯关自动批改") gr.Markdown("根据每道编程作业题,提交你的代码,利用大模型自动批改并获取反馈。") stu_id = gr.Textbox(label='学号') with gr.Row(): question_input = gr.Textbox(lines=7, label="输入题目",value=tests[0]) code_input = gr.Code(lines=10, label="输入代码") with gr.Row(): error_button = gr.Button("获取帮助") explain_button = gr.Button("提交作答") next_button = gr.Button("下一关") error_output = gr.Textbox(label="帮助信息") explain_output = gr.Textbox(label="代码批阅结果") # 定义按钮的响应 error_button.click(check_code_tips, inputs=[stu_id,question_input, code_input], outputs=error_output) explain_button.click(check_code_answer, inputs=[stu_id,question_input, code_input], outputs=explain_output) next_button.click(next_question,inputs=[question_input,explain_output],outputs=[question_input,explain_output]) # 启动 Gradio 界面 demo.launch(server_name='0.0.0.0') ``` -------------------------------- ### 安装BaseDeploy:pip install basedeploy Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/installation.md.txt 使用pip安装BaseDeploy,命令为pip install basedeploy或pip install BaseDeploy。BaseDeploy依赖ONNXRunTime,兼容性很好。 ```bash pip install basedeploy ``` -------------------------------- ### 卸载CPU版本库 Source: https://xedu.readthedocs.io/zh-cn/latest/about/installation.html 在cmd中执行此命令以卸载CPU版本的torch、torchvision和mmcv-full,为GPU升级做准备。 ```bash pip uninstall torch torchvision mmcv-full -y ``` -------------------------------- ### from XEdu.utils import * Source: https://xedu.readthedocs.io/zh-cn/latest/about/functions.html 导入XEdu.utils模块中的所有常用函数,包括softmax、cosine_similarity、get_similarity和visualize_similarity。 ```APIDOC ## Import XEdu.utils ### Description 导入XEdu.utils模块中的所有常用函数,以便直接调用softmax、cosine_similarity、get_similarity和visualize_similarity。 ### Method Import ### Endpoint from XEdu.utils import * ### Parameters 无 ### Request Example ```python from XEdu.utils import * ``` ### Response 无返回值,导入模块后即可使用其中的函数。 ``` -------------------------------- ### pip install xedu-python 或 pip install XEdu-python Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_hub/installation.md.txt 使用pip安装XEduHub。支持两种包名写法:xedu-python 和 XEdu-python。 ```bash pip install xedu-python ``` ```bash pip install XEdu-python ``` -------------------------------- ### 列出待迁移的 WSL 数据 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 在 CMD 中运行此命令,列出所有 WSL 发行版及其状态,用于确定需要迁移的 Docker 相关数据。 ```bash wsl --list -v ``` -------------------------------- ### 使用XEdu.LLM客户端进行逻辑推理问答 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_llm/introduction.md.txt 使用XEdu.LLM的Client类,指定qwen模型和API密钥,通过多轮对话实现逻辑推理问答。输出为因果链和解释。 ```python from XEdu.LLM import Client chatbot = Client(provider='qwen', # 选择模型为阿里-通义千问 api_key='sk-94x...ae4') # 引号内为用户密钥,用于确定身份,若失效,请自行注册:https://dashscope.console.aliyun.com/apiKey talk = [ {'role':'system' ,'content':'你将作为一位善于在两种事物中建立因果联系的智者去构建事物“此物”到“彼物”的因果链,以 “此物->事物A->事物B->事物C...等事物->彼物”这样的形式输出,中间事物数量不限。举个例子 “此物:祖父, 彼物:我”,输出为“祖父->爷爷->爸爸->我”,然后解释其因果联系。'}, {'role':'user' ,'content':'现在请告诉我 “此物:Transformer,彼物:GPT” 的因果联系。'}, ] res = chatbot.inference(talk) print(res) ``` ```text Transformer->自注意力机制->序列建模改进->预训练模型发展->GPT 解释如下: 1. Transformer:2017年,Google的研究人员提出了Transformer架构,这是一种基于自注意力机制的深度学习模型,用于解决序列数据的处理问题,如自然语言处理。 2. 自注意力机制:Transformer的核心创新是自注意力(Self-Attention)机制,它允许模型在处理序列数据时同时考虑所有元素,而不是传统的递归或滑动窗口方法,这提高了模型并行化能力和理解上下文的能力。 3. 序列建模改进:Transformer的自注意力机制显著改进了序列建模,使得模型能够更好地理解和生成复杂的语言结构,这对于自然语言生成和理解任务至关重要。 4. 预训练模型发展:Transformer的成功推动了预训练模型的发展,即在大规模无标注文本上预先训练模型,然后在特定任务上进行微调。这种范式降低了对大量标注数据的依赖,提高了模型的泛化能力。 5. GPT:OpenAI的GPT(Generative Pre-trained Transformer)系列是基于Transformer架构的预训练模型,它使用自注意力机制进行语言建模,展示了强大的语言生成和理解能力,如GPT-3更是成为了预训练模型领域的里程碑式作品。 ``` -------------------------------- ### Example layer_data.txt output for visual_feature Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basenn/adv_function.md.txt Example output of a visual_feature txt file for a model with one hidden neuron, showing layer names and values. ```text 0.Reshape() [27.524368286132812, 28.948490142822266] 1.Linear(in_features=2, out_features=1, bias=True) -0.0438382625579834 1.1.ReLU() 0.0 2.Linear(in_features=1, out_features=2, bias=True) [-2.858757495880127, 3.28355073928833] ``` -------------------------------- ### 编程助手:使用Gradio Blocks实现代码错误检查和解释 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_llm/introduction.md.txt 使用XEduLLM的Client和Gradio Blocks构建编程助手界面,提供检查错误和解释代码两个功能。需要替换api_key为有效的密钥。可通过demo.launch(server_name="0.0.0.0")允许局域网访问。 ```python import gradio as gr from XEdu.LLM import Client # 使用你的 API 密钥实例化客户端 provider = 'qwen' # 选择模型为阿里-通义千问 # 填写用户密钥,用于确定身份,若失效,请自行注册:https://dashscope.console.aliyun.com/apiKey api_key ='sk-946498*************32ae4' chatbot = Client(provider = provider, api_key = api_key) def check_code_errors(code): # 调用推理接口,检查代码中的错误 prompt = f'你是我的编程助手,请帮我检查以下代码,并给出修改建议。\n{code}' res = chatbot.inference(prompt) return res def explain_code(code): # 调用推理接口,解释代码 prompt = f'你是我的编程助手,请帮我解释以下代码的功能:\n{code}' res = chatbot.inference(prompt) return res # 使用 Gradio Blocks API 定义界面 with gr.Blocks() as demo: gr.Markdown("# 编程助手") gr.Markdown("输入你的代码,检查其中的错误并获取建议,或获取代码的功能解释。") with gr.Row(): code_input = gr.Textbox(lines=10, label="输入代码") with gr.Row(): error_button = gr.Button("检查错误") explain_button = gr.Button("解释代码") error_output = gr.Textbox(label="错误检查结果") explain_output = gr.Textbox(label="代码解释结果") error_button.click(check_code_errors, inputs=code_input, outputs=error_output) explain_button.click(explain_code, inputs=code_input, outputs=explain_output) # 启动 Gradio 界面 demo.launch() ``` -------------------------------- ### Example output of masks array Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_hub/preset_task.md.txt This is an example of the masks output, showing a 4D array of boolean values. Each mask is a 2D array with False values indicating non-target areas. ```text [[[False False False ... False False False] [False False False ... False False False] [False False False ... False False False] ... [False False False ... False False False] [False False False ... False False False] [False False False ... False False False]] [[False False False ... False False False] [False False False ... False False False] [False False False ... False False False] ... [False False False ... False False False] [False False False ... False False False] [False False False ... False False False]] [[False False False ... False False False] [False False False ... False False False] [False False False ... False False False] ... [False False False ... False False False] [False False False ... False False False] [False False False ... False False False]] [[False False False ... False False False] [False False False ... False False False] [False False False ... False False False] ... [False False False ... False False False] [False False False ... False False False] [False False False ... False False False]]] ``` -------------------------------- ### 使用os.path获取仓库文件绝对路径 Source: https://xedu.readthedocs.io/zh-cn/latest/xedu_hub/share_my_model.html 由于相对路径基于main程序,建议用此代码获取仓库文件夹路径并拼接文件路径。示例以model.onnx为例。 ```python import os path = os.path.dirname(os.path.abspath(__file__)) model_dir = os.path.join(path,'model.onnx') # 这里以仓库中根目录下model.onnx为例 model_dir = model_dir.replace('\\','/') print('模型所在路径:',model_dir) ``` -------------------------------- ### 创建conda环境并安装MMEdu(Linux) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 为避免版本冲突,建议新建conda环境(python=3.8)并在新环境中执行pip install MMEdu。将your_env_name替换为喜欢的名称。 ```bash $ conda create -n your_env_name python=3.8 $ conda activate your_env_name $ pip install MMEdu ``` -------------------------------- ### XEdu最新版本信息 Source: https://xedu.readthedocs.io/zh-cn/latest/about/installation.html 列出当前在PyPi开源的最新版本号,包括XEdu-python、MMEdu、BaseML、BaseNN、BaseDT、easy-xedu和BaseDeploy。 ```text XEdu-python==2.0.0 MMEdu==2.0.0 BaseML==2.0.0 BaseNN==2.0.0 BaseDT==0.1.3 easy-xedu==0.2.3 BaseDeploy==0.0.4 ``` -------------------------------- ### 导入XEdu.utils所有函数 Source: https://xedu.readthedocs.io/zh-cn/latest/about/functions.html 使用通配符导入XEdu.utils中的所有函数。 ```python from XEdu.utils import * ``` -------------------------------- ### Linux环境安装MMEdu(CPU版本) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 在Linux终端中执行pip install MMEdu即可安装。建议新建conda环境(python=3.8)以避免版本冲突。 ```bash $ pip install MMEdu ``` -------------------------------- ### 更新easy-xedu库 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/easydl/howtoget.md.txt 使用pip命令更新easy-xedu库到最新版本。 ```bash pip install --upgrade easy-xedu ``` -------------------------------- ### Start SIoT listener with run_siot Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/introduction.md.txt Starts SIoT message listening on the local IP 127.0.0.1 by default. Use `model.run_siot(ip=ip地址)` to change the listening IP. The server prints inference results when clients access it. ```python import BaseDeploy as bd model = bd(model_path) model.run_siot() ``` -------------------------------- ### 注销 WSL 中的 Docker 数据 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 注销 docker-desktop-data 以释放原位置,为迁移做准备。如有其他发行版,可类似注销。 ```bash wsl --unregister docker-desktop-data ``` ```bash wsl --unregister docker-desktop ``` -------------------------------- ### 创建conda环境并安装MMEdu(Windows) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 为避免版本冲突,建议新建conda环境(python=3.8)并在新环境中执行pip install MMEdu -f ...。将your_env_name替换为喜欢的名称。 ```bash $ conda create -n your_env_name python=3.8 $ conda activate your_env_name $ pip install MMEdu -f https://download.openmmlab.com/mmcv/dist/cpu/torch1.8.0/index.html ``` -------------------------------- ### Windows环境安装MMEdu(CPU版本) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 在命令行中执行pip install MMEdu -f ... 以安装CPU版本。建议新建conda环境(python=3.8)以避免版本冲突。 ```bash $ pip install MMEdu -f https://download.openmmlab.com/mmcv/dist/cpu/torch1.8.0/index.html ``` -------------------------------- ### 流式响应语音回复(XEdu.LLM + pyttsx3) Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_llm/introduction.md.txt 使用 XEdu.LLM 的流式输出功能,逐字打印并语音合成回复,缩短等待时间。需要安装 pyttsx3 库,并替换有效的 API 密钥。 ```python from XEdu.LLM import Client import pyttsx3 chatbot = Client(provider='qwen', # 选择模型为阿里-通义千问 api_key='sk-946498b7c00b……46dd32ae4') # 引号内为用户密钥,用于确定身份,请自行注册:https://dashscope.console.aliyun.com/apiKey res = chatbot.inference("你好,用中文介绍一下你自己",stream=True) for i in res: print(i, flush=True, end='') pyttsx3.speak(i) ``` -------------------------------- ### 命令行启动一键运行版 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_llm/installation.md.txt 在Mac/Linux上通过命令行启动XEduLLM一键运行版。 ```bash bash <(curl https://cdn.openinnolab.org.cn/res/api/v1/file/creator/b5121868-53ab-4ea8-b75f-8458b768b9e4&name=xedu) ``` -------------------------------- ### 安装 mmcv-full:pip install mmcv-full==1.4.5 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 安装与 CUDA 10.1 和 torch 1.8.0 对应的预编译版本 mmcv-full。建议直接输入此命令。 ```bash pip install mmcv-full==1.4.5 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.8.0/index.html ``` -------------------------------- ### 模型测试 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/mmedu/projects.md.txt 使用新的图片进行推理测试,指定checkpoint为训练好的权重文件,并设置show=True弹出识别结果窗口。使用print_result输出具体信息。 ```python img = "/data/H47U12/cat_dog_det/images/valid/001.jpg" checkpoint = "checkpoints/det_model/catdogs_pretrain/best_bbox_mAP_epoch_7.pth" # 推理,“show=True”表示弹出识别结果窗口 result = model.inference(image=img, show=True, checkpoint = checkpoint,device='cuda')#启动cpu容器将device='cpu',启动GPU容器将device='cuda' # 输出结果,将inference函数输出的结果修饰后输出具体信息 r=model.print_result(result) ``` -------------------------------- ### 模型转换 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/mmedu/projects.md.txt 将训练好的模型权重转换为ONNX格式,指定backbone为SSD_Lite,checkpoint为权重文件路径,out_file为输出文件名。 ```python from MMEdu import MMDetection as det model = det(backbone='SSD_Lite') checkpoint = 'checkpoints/best_bbox_mAP_epoch_7.pth' out_file='cats_dogs_det.onnx' # 指定输出的文件即转换后的文件 model.convert(checkpoint=checkpoint, backend="ONNX", out_file=out_file) ``` -------------------------------- ### 安装 GPU 版本 torch:pip install torch==1.8.1+cu101 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 安装指定 CUDA 10.1 版本的 torch、torchvision 和 torchaudio。使用官方 PyTorch 下载源。 ```bash pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html ``` -------------------------------- ### 启动 Docker 容器 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 启动容器并映射端口 5000 和 8888,同时挂载 D:/share 目录到容器 /xedu/share。首次使用会询问是否绑定磁盘,选择 Yes。 ```bash docker run -it -p 5000:5000 -p 8888:8888 --mount type=bind,source=D:/share,target=/xedu/share xedu/xedu:v3s ``` -------------------------------- ### Output of layer names Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basenn/adv_function.md.txt Example output from printing layer names. Shows that the model has three linear layers, each with weight and bias parameters. ```text layer_name: linear1.weight layer_name: linear1.bias layer_name: linear2.weight layer_name: linear2.bias layer_name: linear3.weight layer_name: linear3.bias ``` -------------------------------- ### 导入XEdu.utils模块 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/functions.md.txt 使用通配符导入或具体导入所需函数。 ```python from XEdu.utils import * ``` ```python from XEdu.utils import softmax, cosine_similarity, get_similarity, visualize_similarity ``` -------------------------------- ### Inference with returning image (img_type='pil') Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_hub/preset_task.md.txt This is the second inference method, which returns both masks and an image. The img_type parameter is set to 'pil' to get a PIL image. ```python masks, img = seg.inference(data='data/seg.jpg', img_type='pil') # 进行模型推理 ``` -------------------------------- ### 使用Workflow和Client实现多模型数据流通 Source: https://xedu.readthedocs.io/zh-cn/latest/xedu_hub/share_my_model.html 此示例展示如何将语音识别、智能问答和语音合成三个模型串联,前一个模型的输出作为后一个模型的输入。需替换repo和xedu_url为实际值。 ```python from XEdu.hub import Workflow as wf from XEdu.LLM import Client model1 = wf(repo='yikshing/funasr-onnx-small') # 语音识别模型 model2 = Client(xedu_url='http://192.168.31.251:7860')# 智能问答 model3 = wf(repo=r'yikshing/edge-tts-zh') # 语音合成 res1 = model1.inference('a.mp3',record_seconds=5) print('语音识别为:',res1) # 第一个模型的结果,作为第二个模型的输入 res2 = model2.inference(res1[0]['preds'][0]) print('大模型答复:',res2) # 第二个模型的结果,作为第三个模型的输入 res3 = model3.inference(res2,output='audio.mp3',gender='male') print('音频保存至:',res3) # 第三个模型的结果,作为最终展示效果 ``` -------------------------------- ### 修改Docker安装路径 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 用管理员权限打开CMD,输入mklink /j命令将Docker安装路径指向新路径。 ```cmd mklink /j "C:\Program Files\Docker" "D:\Program Files\Docker" ``` -------------------------------- ### XEdu latest package versions on PyPi Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt Lists the latest version numbers of the XEdu core packages as published on PyPi. Use this to verify the current versions before installing or upgrading. ```plain XEdu-python==2.0.0 MMEdu==2.0.0 BaseML==2.0.0 BaseNN==2.0.0 BaseDT==0.1.3 easy-xedu==0.2.3 BaseDeploy==0.0.4 ``` -------------------------------- ### Output of layer parameter values Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basenn/adv_function.md.txt Example output showing the actual weight and bias tensor values for each layer. These values can be used to manually compute the forward pass of the network. ```text layer: linear1.weight shape: torch.Size([10, 4]) tensor([[-0.1671, -0.2946, -0.1523, 0.2340], [-0.2319, -0.2094, 0.8307, 0.7168], [-0.4024, 0.0528, -0.4063, 0.3279], [ 0.5091, -0.0214, 0.9994, 0.5224], [ 0.6709, 1.0019, 0.1012, -0.8173], [ 0.7910, 0.6090, -0.6661, -0.8460], [-0.3291, 0.1794, 0.3013, -0.5664], [-0.0976, -0.1166, -0.1597, -0.1705], [-0.2752, 0.3727, -0.4080, -0.4774], [-0.3167, -0.1678, 1.0979, 0.7183]]) layer: linear1.bias shape: torch.Size([10]) tensor([ 0.0276, 0.0367, -0.1092, 0.5315, 0.7263, 0.3025, -0.1470, 0.0737, -0.1824, 0.0029]) layer: linear2.weight shape: torch.Size([5, 10]) tensor([[-0.1355, 0.7558, 0.0546, 0.1230, -0.3620, -0.7619, -0.3141, -0.0608, -0.0821, 0.6960], [-0.2451, -0.8411, -0.1880, 0.2396, 0.4853, 1.0186, -0.0673, -0.2480, 0.2201, -0.7702], [-0.0184, -0.1989, -0.2507, -0.0921, 0.3093, 0.7214, -0.0233, 0.2101, -0.2183, -0.2256], [ 0.1090, -0.1517, 0.1152, -0.2598, -0.0861, 0.1829, -0.3004, -0.0835, 0.0937, -0.1331], [ 0.1686, 0.8030, -0.2627, 0.5540, -0.0460, -0.4488, -0.1358, -0.2360, 0.0522, 0.7765]]) layer: linear2.bias shape: torch.Size([5]) tensor([-0.8387, 0.7115, 0.5185, -0.1147, 0.0033]) layer: linear3.weight shape: torch.Size([3, 5]) tensor([[-0.7702, 0.6576, 0.5163, -0.0201, -1.1092], [-1.1128, 0.2404, 0.2973, 0.3267, 0.1670], [ 0.5992, -1.0186, -0.7638, 0.3445, 0.5820]]) layer: linear3.bias shape: torch.Size([3]) tensor([ 0.6430, 0.3829, -0.1744]) ``` -------------------------------- ### 编程闯关自动批改:Gradio 界面与 XEdu.LLM 调用 Source: https://xedu.readthedocs.io/zh-cn/latest/xedu_llm/introduction.html 构建一个基于 Gradio 的编程闯关自动批改网页应用。使用 XEdu.LLM 的 Client 调用通义千问模型,提供获取提示、提交作答和下一关三个功能,并将操作记录写入以学号命名的日志文件。启动服务后可通过浏览器访问,适合老师分享给学生进行自主闯关学习。 ```python import gradio as gr from XEdu.LLM import Client # 使用你的 API 密钥实例化客户端 provider = 'qwen' # 选择模型为阿里-通义千问 # 填写用户密钥,用于确定身份,若失效,请自行注册:https://dashscope.console.aliyun.com/apiKey api_key ='sk-946498*************32ae4' chatbot = Client(provider = provider, api_key = api_key) with open('tests.txt','r',encoding='UTF-8') as f: tests = f.readlines() print(tests) question_num = 0 question_max = len(tests) def check_code_tips(stu_id,question, code): # 调用推理接口,检查代码中的错误 prompt = f'你是我的编程助手,我要用python编程解决【 "{question}" 】,请给我一点提示。但禁止直接给我代码,只允许给我代码的一部分提示。' res = chatbot.inference(prompt) if stu_id == '': stu_id = 'logs' with open(str(stu_id)+'.txt','a') as f: f.write('[TIP]') f.write(question+'\n') f.write(str(res)+'\n') return res def check_code_answer(stu_id,question, code): # 拼接 prompt,调用推理接口,判断代码是否正确 code = "```python\n"+code+"\n```" #print(code) prompt = f'你是我的编程助手,我要用python编程解决【 "{question}" 】,我写的代码是:\n{code}\n请帮我判断我写的代码对不对,请按照下列要求检查代码:1.必须是python3语言;2.程序没有语法错误;3.程序没有格式错误;4.代码功能和题目要求一致。如果符合上述要求,请输出【回答正确,闯关成功】,如果不符合,则先输出【回答错误,请再次尝试】,再输出错误和提示,但禁止给我完整的代码。' res = chatbot.inference(prompt) global question_num,question_max if res[:4]=='回答正确': res = '回答正确,闯关成功!可以点击“下一关”!' if question_num+1>= question_max: res = '回答正确,闯关成功。恭喜通过全部关卡!' if stu_id == '': stu_id = 'logs' with open(str(stu_id)+'.txt','a') as f: f.write('[ANS]') f.write(question+'\n') f.write(code+'\n') f.write(str(res)+'\n') return res def next_question(question,res): global question_num if res[:10]=='回答正确,闯关成功!': question_num += 1 return tests[question_num],'' elif res[:10]=='回答正确,闯关成功。': return '闯关成功!恭喜通过全部关卡!','' else: return '未通过关卡,请继续作答:'+question,'' # 构建网页界面 with gr.Blocks() as demo: gr.Markdown("# XEduLLM项目:编程闯关自动批改") gr.Markdown("根据每道编程作业题,提交你的代码,利用大模型自动批改并获取反馈。") stu_id = gr.Textbox(label='学号') with gr.Row(): question_input = gr.Textbox(lines=7, label="输入题目",value=tests[0]) code_input = gr.Code(lines=10, label="输入代码") with gr.Row(): error_button = gr.Button("获取帮助") explain_button = gr.Button("提交作答") next_button = gr.Button("下一关") error_output = gr.Textbox(label="帮助信息") explain_output = gr.Textbox(label="代码批阅结果") # 定义按钮的响应 error_button.click(check_code_tips, inputs=[stu_id,question_input, code_input], outputs=error_output) explain_button.click(check_code_answer, inputs=[stu_id,question_input, code_input], outputs=explain_output) next_button.click(next_question,inputs=[question_input,explain_output],outputs=[question_input,explain_output]) # 启动 Gradio 界面 demo.launch(server_name='0.0.0.0') ``` -------------------------------- ### Launch PyWebIO interface with run_pywebio Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/introduction.md.txt Starts a PyWebIO inference interface for the loaded model. Not supported in Jupyter due to PyWebIO limitations. The server listens on port 1956 by default. ```python import BaseDeploy as bd model = bd(model_path) model.run_pywebio() ``` -------------------------------- ### 推理步骤:图片准备 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/mmedu/mmeducls.md.txt 指定推理图片的路径,可直接在代码所在的 demo 文件夹中选择图片。 ```python img = 'testrock01-02.png' # 指定推理图片的路径,直接在代码所在的demo文件夹中选择图片 ``` -------------------------------- ### 调用本地模型仓库进行推理 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/xedu_hub/share_my_model.md.txt 使用XEdu.hub的Workflow加载本地模型仓库(包含data_process.py和model.onnx)进行推理。 ```python from XEdu.hub import Workflow as wf model = wf(repo='D:/my_model') # 注意这里的路径要使用绝对路径 res = model.inference('./Lenna_400x400.jpg',box=None) print(res) ``` -------------------------------- ### Output of layer shapes Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basenn/adv_function.md.txt Example output showing the shapes of each layer's parameters. The first linear layer has shape (10, 4), indicating 4 input neurons connected to 10 hidden neurons. ```text layer_name: linear1.weight shape: torch.Size([10, 4]) layer_name: linear1.bias shape: torch.Size([10]) layer_name: linear2.weight shape: torch.Size([5, 10]) layer_name: linear2.bias shape: torch.Size([5]) layer_name: linear3.weight shape: torch.Size([3, 5]) layer_name: linear3.bias shape: torch.Size([3]) ``` -------------------------------- ### 拉取 Xedu 镜像 Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/about/installation.md.txt 从 Docker Hub 拉取 xedu/xedu:v3s 镜像,需要确保系统盘有足够空间(建议 10GB 以上)。 ```bash docker pull xedu/xedu:v3s ``` -------------------------------- ### Call FastAPI endpoint with requests Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/introduction.md.txt Calls the FastAPI endpoint started by `run_fastapi`. Set the `url` to the server address and `img_path` to the image file path. The response contains the inference result or the drawn image depending on the server's `mode`. ```python import requests url = "http://192.168.31.38:1956/upload" img_path = '' files = {'files': open(img_path, 'rb')} result = requests.post(url=url, files=files) ``` -------------------------------- ### Launch FastAPI server with run_fastapi Source: https://xedu.readthedocs.io/zh-cn/latest/_sources/basedeploy/introduction.md.txt Starts a FastAPI server for the loaded model. Not supported in Jupyter due to FastAPI limitations. The server listens on port 1956 by default and returns inference results or drawn images depending on the `mode` parameter. ```python import BaseDeploy as bd model = bd(model_path) model.run_fastapi() ``` -------------------------------- ### 在行空板上安装推理所需Python库 Source: https://xedu.readthedocs.io/zh-cn/latest/how_to_use/support_resources/works/p4-smartspeaker.html 在行空板上部署ONNX模型前,需安装librosa、pyaudio和onnxruntime库,分别用于音频信号处理和模型推理。 ```bash !pip install librosa !pip install pyaudio !pip install onnxruntime ```