Awesome
page_type: sample languages:
- python products:
- azure description: "These samples will show you how to get up and running using the Python SDKs for various Cognitive Services services." urlFragment: cognitive-services-python-sdk-samples
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.
Features
This project framework provides examples for the following services:
Knowledge
- Using the QnA SDK azure-cognitiveservices-knowledge-qnamaker for the QnA API
Language
- Using the LUIS SDK azure-cognitiveservices-language-luis for the LUIS API
- Using the Bing Spell Check SDK azure-cognitiveservices-language-spellcheck for the Bing Spell Check API
- Using the Text Analytics SDK azure-cognitiveservices-language-textanalytics for the Text Analytics API
Search
- Using the Bing Autosuggest SDK azure-cognitiveservices-search-autosuggest for the Autosuggest API
- Using the Bing Custom Search SDK azure-cognitiveservices-search-customsearch for the Custom Search API
- Using the Bing Custom Image Search SDK azure-cognitiveservices-search-customimagesearch for the Custom Image Search API
- Using the Bing Entity Search SDK azure-cognitiveservices-search-entitysearch for the Entity Search API
- Using the Bing Image Search SDK azure-cognitiveservices-search-imagesearch for the Image Search API
- Using the Bing News Search SDK azure-cognitiveservices-search-newssearch for the News Search API
- Using the Bing Video Search SDK azure-cognitiveservices-search-videosearch for the Video Search API
- Using the Bing Visual Search SDK azure-cognitiveservices-search-visualsearch for the Visual Search API
- Using the Bing Web Search SDK azure-cognitiveservices-search-websearch for the Web Search API
Vision
- Using the Face SDK azure-cognitiveservices-vision-face for the Face API
- Using the Computer Vision SDK azure-cognitiveservices-vision-computervision for the Computer Vision API
- Using the Content Moderator SDK azure-cognitiveservices-vision-contentmoderator for the Content Moderator API
- Using the Custom Vision SDK azure-cognitiveservices-vision-customvision for the Custom Vision API
- Using the Ink Recognizer SDK azure-cognitiveservices-inkrecognizer for the Ink Recognizer API
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.
Getting Started
Prerequisites
- 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.
Installation
-
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
Quickstart
-
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 variable
LUIS_SUBSCRIPTION_KEY
with your key if you want to execute LUIS tests. -
Set up the environment variable
SPELLCHECK_SUBSCRIPTION_KEY
with your key if you want to execute SpellCheck tests. -
Set up the environment variable
TEXTANALYTICS_SUBSCRIPTION_KEY
with your key if you want to execute TextAnalytics tests. You might override tooTEXTANALYTICS_LOCATION
(westcentralus by default). -
Set up the environment variable
AUTOSUGGEST_SUBSCRIPTION_KEY
with your key if you want to execute Autosuggest tests. -
Set up the environment variable
CUSTOMSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute CustomSearch tests. -
Set up the environment variable
CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY
with your key if you want to execute CustomImageSearch tests. -
Set up the environment variable
ENTITYSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute EntitySearch tests. -
Set up the environment variable
IMAGESEARCH_SUBSCRIPTION_KEY
with your key if you want to execute ImageSearch tests. -
Set up the environment variable
NEWSSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute NewsSearch tests. -
Set up the environment variable
VIDEOSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute VideoSearch tests. -
Set up the environment variable
VISUALSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute VideoSearch tests. -
Set up the environment variable
WEBSEARCH_SUBSCRIPTION_KEY
with your key if you want to execute WebSearch tests. -
Set up the environment variable
COMPUTERVISION_SUBSCRIPTION_KEY
with your key if you want to execute Computer Vision tests. You might override tooCOMPUTERVISION_LOCATION
(westcentralus by default). -
Set up the environment variable
CONTENTMODERATOR_SUBSCRIPTION_KEY
with your key if you want to execute Content Moderator tests. You might override tooCONTENTMODERATOR_LOCATION
(westcentralus by default). -
Set up the environment variable
CUSTOMVISION_TRAINING_KEY
with your key andCUSTOMVISION_PREDICTION_ID
with a valid prediction resource id if you want to execute CustomVision Training tests. -
Set up the environment variable
CUSTOMVISION_PREDICTION_KEY
with your key andCUSTOMVISION_PREDICTION_ID
with a valid prediction resource id if you want to execute CustomVision Prediction tests.
Demo
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.py
python samples/search/entity_search_samples.py
python samples/search/video_search_samples.py
python 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.