Graphormer保姆级教程:Gradio界面汉化+自定义CSS主题+响应式布局改造

张开发
2026/4/13 7:43:28 15 分钟阅读

分享文章

Graphormer保姆级教程:Gradio界面汉化+自定义CSS主题+响应式布局改造
Graphormer保姆级教程Gradio界面汉化自定义CSS主题响应式布局改造1. 项目介绍与准备工作Graphormer是由微软研究院开发的基于纯Transformer架构的图神经网络模型专门用于分子属性预测任务。该模型通过创新的分子图表示方法在OGB、PCQM4M等分子基准测试中大幅超越传统GNN模型。1.1 模型基本信息属性说明模型名称microsoft/Graphormer (Distributional-Graphormer)模型大小3.7GB主要用途药物发现、材料科学、分子建模输入格式SMILES分子结构字符串预测任务catalyst-adsorption, property-guided1.2 环境准备在开始界面改造前请确保已正确部署Graphormer服务# 检查服务状态 supervisorctl status graphormer # 启动服务如未运行 supervisorctl start graphormer服务默认运行在7860端口可通过以下地址访问http://服务器IP:78602. Gradio界面汉化教程2.1 定位界面文件Graphormer的Gradio界面代码位于/root/graphormer/app.py2.2 基础汉化步骤打开app.py文件找到界面组件定义部分将英文标签替换为中文# 修改前 gr.Interface( fnpredict, inputs[ gr.Textbox(labelSMILES), gr.Dropdown(labelTask, choices[property-guided, catalyst-adsorption]) ], outputsgr.Textbox(labelPrediction Result) ) # 修改后 gr.Interface( fnpredict, inputs[ gr.Textbox(label分子SMILES), gr.Dropdown(label预测任务, choices[属性预测, 催化剂吸附预测]) ], outputsgr.Textbox(label预测结果) )2.3 完整汉化方案对于更彻底的汉化建议修改以下部分按钮文本将Submit改为预测提示信息添加中文使用说明错误提示将英文错误信息转为中文示例代码# 添加中文说明 description ## Graphormer分子属性预测系统 输入分子SMILES结构选择预测任务类型点击预测按钮获取结果 # 修改后的界面定义 demo gr.Interface( fnpredict, inputs[ gr.Textbox(label分子SMILES, placeholder例如CCO乙醇), gr.Dropdown(label预测任务, choices[属性预测, 催化剂吸附预测]) ], outputsgr.Textbox(label预测结果), titleGraphormer分子属性预测, descriptiondescription, allow_flaggingnever, examples[ [CCO, 属性预测], [c1ccccc1, 催化剂吸附预测] ] )3. 自定义CSS主题美化3.1 基础CSS修改在app.py中添加theme参数# 添加自定义主题 demo.launch( server_name0.0.0.0, server_port7860, shareFalse, themegr.themes.Default( primary_hueblue, secondary_hueteal, neutral_hueslate ) )3.2 高级CSS定制创建单独的CSS文件如custom.css/* 主容器样式 */ .gradio-container { font-family: Microsoft YaHei, sans-serif; max-width: 900px; margin: 0 auto; background: #f8f9fa; border-radius: 12px; padding: 20px; } /* 输入框样式 */ input[typetext], select { border: 1px solid #ced4da; border-radius: 6px; padding: 10px 15px; font-size: 16px; } /* 按钮样式 */ button { background: #4e73df; color: white; border: none; border-radius: 6px; padding: 12px 24px; font-size: 16px; cursor: pointer; transition: all 0.3s; } button:hover { background: #2e59d9; }在app.py中引入CSS# 在launch前添加CSS demo.css custom.css4. 响应式布局改造4.1 移动端适配修改布局组件使用gr.Column和gr.Rowwith gr.Blocks() as demo: with gr.Row(): with gr.Column(scale2): smiles_input gr.Textbox(label分子SMILES) task_select gr.Dropdown(label预测任务, choices[属性预测, 催化剂吸附预测]) submit_btn gr.Button(预测) with gr.Column(scale3): output gr.Textbox(label预测结果) gr.Examples( examples[ [CCO, 属性预测], [c1ccccc1, 催化剂吸附预测] ], inputs[smiles_input, task_select] )4.2 媒体查询添加在custom.css中添加响应式规则/* 平板设备适配 */ media (max-width: 768px) { .gradio-container { padding: 15px; } .gradio-row { flex-direction: column; } .gradio-column { width: 100% !important; } } /* 手机设备适配 */ media (max-width: 480px) { input[typetext], select { padding: 8px 12px; font-size: 14px; } button { padding: 10px 20px; } }5. 完整代码示例以下是整合了所有改造的完整app.py示例import gradio as gr from predict import predict # 假设预测函数在predict.py中 # 自定义CSS CSS .gradio-container { font-family: Microsoft YaHei, sans-serif; max-width: 900px; margin: 20px auto; background: #f8f9fa; border-radius: 12px; padding: 25px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); } media (max-width: 768px) { .gradio-container { padding: 15px; } } with gr.Blocks(cssCSS, themegr.themes.Default( primary_hueblue, secondary_hueteal )) as demo: gr.Markdown( # Graphormer分子属性预测系统 输入分子SMILES结构选择预测任务类型点击预测按钮获取结果 ) with gr.Row(): with gr.Column(scale2): smiles_input gr.Textbox(label分子SMILES, placeholder例如CCO乙醇) task_select gr.Dropdown( label预测任务, choices[属性预测, 催化剂吸附预测], value属性预测 ) submit_btn gr.Button(预测, variantprimary) with gr.Column(scale3): output gr.Textbox(label预测结果, interactiveFalse) with gr.Accordion(示例分子, openFalse): gr.Examples( examples[ [CCO, 属性预测], [c1ccccc1, 催化剂吸附预测], [CC(O)O, 属性预测], [O, 催化剂吸附预测] ], inputs[smiles_input, task_select], label点击填充示例 ) submit_btn.click( fnpredict, inputs[smiles_input, task_select], outputsoutput ) if __name__ __main__: demo.launch( server_name0.0.0.0, server_port7860, shareFalse )6. 部署与测试6.1 重启服务应用更改supervisorctl restart graphormer6.2 测试要点汉化验证确认所有界面元素显示为中文响应式测试在PC端检查布局使用浏览器开发者工具模拟移动设备实际手机访问测试功能测试输入示例SMILES验证预测功能测试不同任务类型的切换6.3 常见问题解决问题1修改后界面无变化解决方案确保已重启服务清除浏览器缓存问题2CSS未生效解决方案检查CSS路径是否正确语法是否有误问题3移动端布局错乱解决方案检查媒体查询条件是否覆盖所有断点7. 总结与进阶建议通过本教程我们完成了Graphormer预测系统的三大改造全面汉化使界面更符合中文用户习惯视觉升级通过CSS定制提升美观度响应式布局确保多设备良好体验7.1 进阶优化方向添加可视化使用RDKit渲染分子结构图历史记录保存用户查询记录多语言切换实现中英文自由切换暗黑模式添加夜间主题支持7.2 性能注意事项自定义CSS不宜过于复杂避免影响加载速度响应式设计应考虑不同设备的性能差异定期检查服务日志监控资源使用情况tail -f /root/logs/graphormer.log获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章