Step-by-Step Guide to Install ModelScope and Perform NLP Inference in Python & PHP

This guide walks you through setting up a Conda Python environment, installing PyTorch and the ModelScope library, running NLP pipelines for tasks like word segmentation and text classification, and calling ModelScope models from PHP using the PHPY extension, complete with code examples and troubleshooting tips.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Step-by-Step Guide to Install ModelScope and Perform NLP Inference in Python & PHP

ModelScope Overview

ModelScope is an open‑source Model‑as‑a‑Service platform that provides a unified API for AI models, enabling developers to load, run and fine‑tune models with a single call.

Environment Setup

Python and Conda

Download and install Anaconda, then create an isolated Conda environment for ModelScope.

wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
sh Anaconda3-2024.02-1-Linux-x86_64.sh
conda create -n modelscope-env python=3.10
conda activate modelscope-env
# If "conda activate" fails, run:
source ~/.bashrc

PyTorch

Install the PyTorch stack (CPU or CUDA wheels are selected automatically).

pip3 install torch torchvision torchaudio

ModelScope Python library

pip3 install modelscope
pip install setuptools_scm
# Install NLP extras and point to the ModelScope release index
pip3 install "modelscope[nlp]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

Verification

Run a quick inference to confirm that the library and its dependencies are functional.

python -c "from modelscope.pipelines import pipeline; print(pipeline('word-segmentation')('今天天气不错,适合出去游玩,你说呢'))"

Text Classification with ModelScope

Typical classification scenarios include:

Sentiment analysis – classify a sentence as positive, negative or neutral.

Sentence similarity – decide whether two sentences convey the same meaning.

Natural Language Inference (NLI) – determine logical entailment, contradiction or neutrality between a pair of sentences.

Custom label mapping – assign user‑defined categories to input texts.

ModelScope performs inference through the pipeline helper.

from modelscope.pipelines import pipeline
pipeline_ins = pipeline(task='nli', model='damo/nlp_structbert_nli_chinese-base')
result = pipeline_ins(input=('四川商务职业学院和四川财经职业学院哪个好?',
                          '四川商务职业学院商务管理在哪个校区?'))
print(result)
# Expected output (truncated)
# {'scores': [0.8383, 0.0834, 0.0784], 'labels': ['中立', '矛盾', '蕴涵']}

Calling ModelScope from PHP via PHPY

Build and install PHPY

PHPY is a PHP extension that embeds a Python interpreter, allowing PHP code to import and call Python modules such as ModelScope.

# Clone the repository
git clone https://github.com/swoole/phpy.git
cd phpy/

# Generate the extension build configuration
/usr/local/php-8.2.14/bin/phpize --with-php-config=/usr/local/php-8.2.14/bin/php-config

# Configure the build, pointing to the Conda environment that contains ModelScope
./configure --with-php-config=/usr/local/php-8.2.14/bin/php-config \
    --with-python-dir=/home/www/anaconda3/envs/modelscope-env \
    --with-python-version=3.10

# Compile and install
make -j4
sudo make install

# Enable the extension
echo "extension=phpy.so" >> /usr/local/php-8.2.14/etc/php.ini

# Verify the installation
/usr/local/php-8.2.14/bin/php -m | grep phpy

PHP script for NLI inference

<?php
$pipeline = PyCore::import('modelscope.pipelines')->pipeline;
$pipeline_ins = $pipeline('nli', 'damo/nlp_structbert_nli_chinese-base');

$inputs = [
    "四川商务职业学院和四川财经职业学院哪个好?",
    "四川商务职业学院商务管理在哪个校区?"
];

echo $pipeline_ins(new \PyTuple($inputs));
?>

The script prints a dictionary with scores and labels identical to the Python example, demonstrating that ModelScope can be accessed from PHP.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonPHPAI inferenceNLPPipelineModelScope
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.