Skip to content

Commit

Permalink
Cognitive Service demo
Browse files Browse the repository at this point in the history
  • Loading branch information
loleg committed Mar 31, 2023
1 parent db41f09 commit 29fe5db
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
__pycache__
.ipynb_checkpoints/

.env
126 changes: 126 additions & 0 deletions azure_cognitive.ipynb
@@ -0,0 +1,126 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "8d1ea2fe",
"metadata": {},
"outputs": [],
"source": [
"import requests, uuid, json\n",
"from decouple import config"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eabc827e",
"metadata": {},
"outputs": [
{
"ename": "UndefinedValueError",
"evalue": "TRANSLATOR_KEY not found. Declare it as envvar or define a default value.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mUndefinedValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m TRANSLATOR_KEY \u001b[38;5;241m=\u001b[39m config(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTRANSLATOR_KEY\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m TRANSLATOR_KEY: \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo key!\u001b[39m\u001b[38;5;124m\"\u001b[39m); exit\n",
"File \u001b[0;32m~/.conda/envs/h4sg/lib/python3.11/site-packages/decouple.py:248\u001b[0m, in \u001b[0;36mAutoConfig.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 245\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconfig:\n\u001b[1;32m 246\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_load(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msearch_path \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_caller_path())\n\u001b[0;32m--> 248\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconfig(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/.conda/envs/h4sg/lib/python3.11/site-packages/decouple.py:107\u001b[0m, in \u001b[0;36mConfig.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 104\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;124;03m Convenient shortcut to get.\u001b[39;00m\n\u001b[1;32m 106\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 107\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m~/.conda/envs/h4sg/lib/python3.11/site-packages/decouple.py:92\u001b[0m, in \u001b[0;36mConfig.get\u001b[0;34m(self, option, default, cast)\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 91\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(default, Undefined):\n\u001b[0;32m---> 92\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m UndefinedValueError(\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m not found. Declare it as envvar or define a default value.\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mformat(option))\n\u001b[1;32m 94\u001b[0m value \u001b[38;5;241m=\u001b[39m default\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(cast, Undefined):\n",
"\u001b[0;31mUndefinedValueError\u001b[0m: TRANSLATOR_KEY not found. Declare it as envvar or define a default value."
]
}
],
"source": [
"TRANSLATOR_KEY = config('TRANSLATOR_KEY')\n",
"if not TRANSLATOR_KEY: print(\"No key!\"); exit"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d544fe7a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" {\n",
" \"translations\": [\n",
" {\n",
" \"text\": \"J’aimerais vraiment conduire votre voiture autour du pâté de maisons plusieurs fois!\",\n",
" \"to\": \"fr\"\n",
" },\n",
" {\n",
" \"text\": \"Ngingathanda ngempela ukushayela imoto yakho emhlabeni block izikhathi ezimbalwa!\",\n",
" \"to\": \"zu\"\n",
" }\n",
" ]\n",
" }\n",
"]\n"
]
}
],
"source": [
"# Add your key and endpoint\n",
"key = '7b46a7ae268c49ffb80bbf778a05311e'\n",
"endpoint = \"https://api.cognitive.microsofttranslator.com\"\n",
"\n",
"# location, also known as region.\n",
"# required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.\n",
"location = \"westeurope\"\n",
"\n",
"path = '/translate'\n",
"constructed_url = endpoint + path\n",
"\n",
"params = {\n",
" 'api-version': '3.0',\n",
" 'from': 'en',\n",
" 'to': ['np']\n",
"}\n",
"\n",
"headers = {\n",
" 'Ocp-Apim-Subscription-Key': key,\n",
" # location required if you're using a multi-service or regional (not global) resource.\n",
" 'Ocp-Apim-Subscription-Region': location,\n",
" 'Content-type': 'application/json',\n",
" 'X-ClientTraceId': str(uuid.uuid4())\n",
"}\n",
"\n",
"# You can pass more than one object in body.\n",
"body = [{\n",
" 'text': 'I would really like to drive your car around the block a few times!'\n",
"}]\n",
"\n",
"request = requests.post(constructed_url, params=params, headers=headers, json=body)\n",
"response = request.json()\n",
"\n",
"print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
158 changes: 158 additions & 0 deletions requirements.txt
@@ -0,0 +1,158 @@
anyio==3.6.2
appdirs==1.4.4
argon2-cffi @ file:///opt/conda/conda-bld/argon2-cffi_1645000214183/work
argon2-cffi-bindings @ file:///work/ci_py311/argon2-cffi-bindings_1676823553406/work
asttokens @ file:///opt/conda/conda-bld/asttokens_1646925590279/work
attrs @ file:///work/ci_py311/attrs_1676822758931/work
Babel @ file:///work/ci_py311/babel_1676825020543/work
backcall @ file:///home/ktietz/src/ci/backcall_1611930011877/work
beautifulsoup4 @ file:///work/ci_py311/beautifulsoup4_1676823573470/work
bleach @ file:///opt/conda/conda-bld/bleach_1641577558959/work
Bottleneck @ file:///work/ci_py311/bottleneck_1676827011612/work
brotlipy==0.7.0
build==0.10.0
certifi @ file:///work/ci_py311/certifi_1676822443922/work/certifi
cffi @ file:///work/ci_py311/cffi_1676822533496/work
charset-normalizer @ file:///tmp/build/80754af9/charset-normalizer_1630003229654/work
cleo==2.0.1
cmake==3.26.1
comm @ file:///work/ci_py311/comm_1677709131612/work
contourpy @ file:///work/ci_py311/contourpy_1676827066340/work
crashtest==0.4.1
cryptography @ file:///work/ci_py311_2/cryptography_1679335572456/work
cycler @ file:///tmp/build/80754af9/cycler_1637851556182/work
daal4py==2023.0.2
debugpy @ file:///work/ci_py311/debugpy_1676824903649/work
decorator @ file:///opt/conda/conda-bld/decorator_1643638310831/work
defusedxml @ file:///tmp/build/80754af9/defusedxml_1615228127516/work
dulwich==0.21.3
entrypoints @ file:///work/ci_py311/entrypoints_1676823319002/work
executing @ file:///opt/conda/conda-bld/executing_1646925071911/work
fastjsonschema @ file:///work/ci_py311_2/python-fastjsonschema_1679340124475/work
filelock==3.10.0
flit_core @ file:///croot/flit-core_1679397103445/work/source/flit_core
fonttools==4.25.0
germansentiment==1.1.0
h11==0.14.0
httpcore==0.16.3
httpx==0.23.3
huggingface-hub==0.13.3
idna @ file:///work/ci_py311/idna_1676822698822/work
installer==0.6.0
ipykernel @ file:///work/ci_py311/ipykernel_1677867938687/work
ipython @ file:///work/ci_py311/ipython_1676824927689/work
ipython-genutils @ file:///tmp/build/80754af9/ipython_genutils_1606773439826/work
ipywidgets @ file:///croot/ipywidgets_1679394798311/work
jedi @ file:///work/ci_py311_2/jedi_1679336495545/work
Jinja2 @ file:///work/ci_py311/jinja2_1676823587943/work
joblib @ file:///work/ci_py311/joblib_1676837020463/work
json5 @ file:///tmp/build/80754af9/json5_1624432770122/work
jsonschema @ file:///croot/jsonschema_1678983421637/work
jupyter @ file:///work/ci_py311/jupyter_1676837039433/work
jupyter-console @ file:///croot/jupyter_console_1679999630278/work
jupyter-server @ file:///work/ci_py311/jupyter_server_1676824947163/work
jupyter_client @ file:///croot/jupyter_client_1680171862562/work
jupyter_core @ file:///croot/jupyter_core_1679906564508/work
jupyterlab @ file:///work/ci_py311/jupyterlab_1677874017039/work
jupyterlab-pygments @ file:///tmp/build/80754af9/jupyterlab_pygments_1601490720602/work
jupyterlab-widgets @ file:///croot/jupyterlab_widgets_1679055282532/work
jupyterlab_server @ file:///croot/jupyterlab_server_1679906300521/work
kiwisolver @ file:///work/ci_py311/kiwisolver_1676827230232/work
lit==16.0.0
lxml @ file:///work/ci_py311/lxml_1676823603437/work
MarkupSafe @ file:///work/ci_py311/markupsafe_1676823507015/work
matplotlib @ file:///croot/matplotlib-suite_1679593461707/work
matplotlib-inline @ file:///work/ci_py311/matplotlib-inline_1676823841154/work
mistune @ file:///work/ci_py311/mistune_1676823698136/work
mkl-fft==1.3.1
mkl-random @ file:///work/ci_py311/mkl_random_1676828032909/work
mkl-service==2.4.0
munkres==1.1.4
nbclassic @ file:///croot/nbclassic_1680008784177/work
nbclient @ file:///work/ci_py311/nbclient_1676823713954/work
nbconvert @ file:///work/ci_py311/nbconvert_1676823854564/work
nbformat @ file:///work/ci_py311/nbformat_1676823523130/work
nest-asyncio @ file:///work/ci_py311/nest-asyncio_1676823382924/work
nlpcloud==1.0.38
notebook @ file:///croot/notebook_1680011997181/work
notebook_shim @ file:///work/ci_py311/notebook-shim_1676825053954/work
numexpr @ file:///work/ci_py311_2/numexpr_1679336657660/work
numpy @ file:///work/ci_py311/numpy_and_numpy_base_1676823870601/work
nvidia-cublas-cu11==11.10.3.66
nvidia-cuda-cupti-cu11==11.7.101
nvidia-cuda-nvrtc-cu11==11.7.99
nvidia-cuda-runtime-cu11==11.7.99
nvidia-cudnn-cu11==8.5.0.96
nvidia-cufft-cu11==10.9.0.58
nvidia-curand-cu11==10.2.10.91
nvidia-cusolver-cu11==11.4.0.1
nvidia-cusparse-cu11==11.7.4.91
nvidia-nccl-cu11==2.14.3
nvidia-nvtx-cu11==11.7.91
packaging @ file:///croot/packaging_1678965309396/work
pandas==1.5.3
pandocfilters @ file:///opt/conda/conda-bld/pandocfilters_1643405455980/work
parso @ file:///opt/conda/conda-bld/parso_1641458642106/work
pendulum==2.1.2
pexpect @ file:///tmp/build/80754af9/pexpect_1605563209008/work
pickleshare @ file:///tmp/build/80754af9/pickleshare_1606932040724/work
Pillow==9.4.0
pkginfo==1.9.6
platformdirs @ file:///work/ci_py311/platformdirs_1676823055788/work
ply==3.11
poetry==1.4.0
poetry-core==1.5.1
poetry-plugin-export==1.3.0
pooch @ file:///tmp/build/80754af9/pooch_1623324770023/work
prometheus-client @ file:///work/ci_py311_2/prometheus_client_1679340520168/work
prompt-toolkit @ file:///work/ci_py311/prompt-toolkit_1676824857154/work
psutil @ file:///work/ci_py311_2/psutil_1679337388738/work
ptyprocess @ file:///tmp/build/80754af9/ptyprocess_1609355006118/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl
pure-eval @ file:///opt/conda/conda-bld/pure_eval_1646925070566/work
pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/work
Pygments @ file:///opt/conda/conda-bld/pygments_1644249106324/work
pyOpenSSL @ file:///croot/pyopenssl_1678965284384/work
pyparsing @ file:///work/ci_py311/pyparsing_1677811559502/work
pyproject_hooks==1.0.0
PyQt5-sip==12.11.0
pyrsistent @ file:///work/ci_py311/pyrsistent_1676823088752/work
PySocks @ file:///work/ci_py311/pysocks_1676822712504/work
python-dateutil @ file:///tmp/build/80754af9/python-dateutil_1626374649649/work
python-decouple==3.8
python-telegram-bot==20.0
pytz @ file:///work/ci_py311/pytz_1676825004346/work
pytzdata==2020.1
pyzmq @ file:///work/ci_py311/pyzmq_1676918885689/work
qtconsole @ file:///work/ci_py311/qtconsole_1677705027985/work
QtPy @ file:///work/ci_py311/qtpy_1676827467989/work
rapidfuzz==2.13.7
requests @ file:///work/ci_py311_2/requests_1679337615230/work
rfc3986==1.5.0
scikit-learn @ file:///croot/scikit-learn_1680198742267/work
scikit-learn-intelex==20230301.154621
scipy==1.10.0
Send2Trash @ file:///tmp/build/80754af9/send2trash_1632406701022/work
shellingham==1.5.0.post1
sip @ file:///work/ci_py311/sip_1676825117084/work
six @ file:///tmp/build/80754af9/six_1644875935023/work
sniffio==1.3.0
soupsieve @ file:///work/ci_py311/soupsieve_1676823539587/work
stack-data @ file:///opt/conda/conda-bld/stack_data_1646927590127/work
terminado @ file:///work/ci_py311/terminado_1677696151350/work
threadpoolctl @ file:///Users/ktietz/demo/mc3/conda-bld/threadpoolctl_1629802263681/work
tinycss2 @ file:///work/ci_py311/tinycss2_1676823757641/work
tokenizers==0.13.2
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
tomli @ file:///work/ci_py311/tomli_1676822660974/work
torch==2.0.0
tornado @ file:///work/ci_py311/tornado_1676823418767/work
traitlets @ file:///work/ci_py311/traitlets_1676823305040/work
transformers==4.27.3
triton==2.0.0
trove-classifiers==2023.3.9
typing_extensions==4.5.0
urllib3 @ file:///croot/urllib3_1680254681959/work
wcwidth @ file:///Users/ktietz/demo/mc3/conda-bld/wcwidth_1629357192024/work
webencodings==0.5.1
websocket-client @ file:///work/ci_py311/websocket-client_1676824890004/work
widgetsnbextension @ file:///croot/widgetsnbextension_1679313860248/work

0 comments on commit 29fe5db

Please sign in to comment.