| page_type | languages | products | description | urlFragment | ||
|---|---|---|---|---|---|---|
sample |
|
|
These samples will show you how to get up and running using the Python SDKs for various Cognitive Services services. |
cognitive-services-python-sdk-samples |
These samples will show you how to get up and running using the Python SDKs for various Cognitive Services services. They'll cover a few rudimentary use cases and hopefully express best practices for interacting with the data from these APIs.
This project framework provides examples for the following services:
We provide several meta-packages to help you install several packages at a time. Please note that meta-packages are only recommended for development purpose. It's recommended in production to always pin specific version of individual packages.
- A cognitive services API key with which to authenticate the SDK's calls. Create a new Azure account, and try Cognitive Services for free.
Subscription keys are usually per service. For example, the subscription key for Spell Check will not be the same than Custom Search. Read the previous sign up link or the Azure portal for details on subscription keys.
-
If you don't already have it, install Python.
This sample (and the SDK) is compatible with Python 2.7, 3.3, 3.4, 3.5 and 3.6.
-
General recommendation for Python development is to use a Virtual Environment. For more information, see https://docs.python.org/3/tutorial/venv.html
Install and initialize the virtual environment with the "venv" module on Python 3 (you must install virtualenv for Python 2.7):
python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation cd mytestenv source bin/activate # Linux shell (Bash, ZSH, etc.) only ./scripts/activate # PowerShell only ./scripts/activate.bat # Windows CMD only
-
Clone the repository.
git clone https://github.com/Azure-Samples/cognitive-services-python-sdk-samples.git
-
Install the dependencies using pip.
cd cognitive-services-python-sdk-samples pip install -r requirements.txt -
Set up the environment variables with corresponding keys to execute samples of interest
| Environment variable | Service | Notes |
|---|---|---|
LUIS_SUBSCRIPTION_KEY |
LUIS | |
SPELLCHECK_SUBSCRIPTION_KEY |
SpellCheck | |
TEXTANALYTICS_SUBSCRIPTION_KEY |
TextAnalytics | You might override too TEXTANALYTICS_LOCATION (westcentralus by default). |
AUTOSUGGEST_SUBSCRIPTION_KEY |
Autosuggest | |
CUSTOMSEARCH_SUBSCRIPTION_KEY |
CustomSearch | |
CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY |
CustomImageSearch | |
ENTITYSEARCH_SUBSCRIPTION_KEY |
EntitySearch | |
IMAGESEARCH_SUBSCRIPTION_KEY |
ImageSearch | |
NEWSSEARCH_SUBSCRIPTION_KEY |
NewsSearch | |
VIDEOSEARCH_SUBSCRIPTION_KEY |
VideoSearch | |
VISUALSEARCH_SUBSCRIPTION_KEY |
VideoSearch | |
WEBSEARCH_SUBSCRIPTION_KEY |
WebSearch | |
COMPUTERVISION_SUBSCRIPTION_KEY |
Computer Vision | You might override too COMPUTERVISION_LOCATION (westcentralus by default). |
CONTENTMODERATOR_SUBSCRIPTION_KEY |
Content Moderator | You might override too CONTENTMODERATOR_LOCATION (westcentralus by default). |
CUSTOMVISION_TRAINING_KEY, CUSTOMVISION_TRAINING_ID |
CustomVision Training | |
CUSTOMVISION_PREDICTION_KEY, CUSTOMVISION_PREDICTION_ID |
CustomVision Prediction |
A demo app is included to show how to use the project.
To run the complete demo, execute python example.py
To run each individual demo, point directly to the file. For example (i.e. not complete list):
python samples/language/spellcheck_samples.pypython samples/search/entity_search_samples.pypython samples/search/video_search_samples.pypython samples/vision/inkrecognizer_sample.py
To see the code of each example, simply look at the examples in the Samples folder. They are written to be isolated in scope so that you can see only what you're interested in.