Skip to content

Instantly share code, notes, and snippets.

@michaelbernstein
Created July 14, 2025 19:43
Show Gist options
  • Select an option

  • Save michaelbernstein/8bd1b373e48073a81dcf9b38651dcef3 to your computer and use it in GitHub Desktop.

Select an option

Save michaelbernstein/8bd1b373e48073a81dcf9b38651dcef3 to your computer and use it in GitHub Desktop.
AcrolinxNextGenDemo.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/michaelbernstein/8bd1b373e48073a81dcf9b38651dcef3/acrolinxnextgendemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Acrolinx Python SDK Demo\n",
"\n",
"This notebooks demonstrates how to use the official **Acrolinx NextGen API** python SDK.\n",
"\n",
"## Setup\n",
"\n",
"We'll install the official package and import the necessary module\n",
"\n"
],
"metadata": {
"id": "Pu0byugIi3eE"
}
},
{
"cell_type": "code",
"source": [
"%%capture\n",
"# Install the official Acrolinx NextGen API package\n",
"!pip install acrolinx-nextgen-api"
],
"metadata": {
"id": "Envx0PBe149t"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.112540Z",
"start_time": "2025-07-09T15:28:47.278503Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uvufyJYwzll4",
"outputId": "04d92182-63fa-41e9-e4ca-853ebb372803"
},
"source": [
"from acrolinx import Acrolinx\n",
"import json\n",
"import time\n",
"import tempfile\n",
"import os\n",
"\n",
"print(\"✅ Acrolinx SDK imported successfully!\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"✅ Acrolinx SDK imported successfully!\n"
]
}
],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"**❗FOR YOU: Put your API key here.**"
],
"metadata": {
"id": "gakm2k49iCwD"
}
},
{
"cell_type": "code",
"source": [
"ACROLINX_API_KEY = \"INSERT YOUR KEY HERE\""
],
"metadata": {
"id": "uU_euoAAiAZw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.283384Z",
"start_time": "2025-07-09T15:28:48.230705Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iPqGnTQTzll5",
"outputId": "9469b091-6639-4d3f-d642-4bed04bfe7e1"
},
"source": [
"# Initialize the Acrolinx client\n",
"client = Acrolinx(\n",
" token=ACROLINX_API_KEY,\n",
")\n",
"\n",
"print(\"🚀 Acrolinx SDK client initialized!\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"🚀 Acrolinx SDK client initialized!\n"
]
}
],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"**❗FOR YOU: Put the text you want to check here.**"
],
"metadata": {
"id": "Az8mobv0igzo"
}
},
{
"cell_type": "code",
"source": [
"# Sample text to analyze - a business communication about remote work\n",
"sample_text = \"\"\"\n",
"Remote work has fundamentally transformed how organiztions operate in the modern busness landscape. Companies worldwide have discovered that distributed teams can maintain high levels of productivity while offering employees greater flexibility and work-life balance. This shift has necessitated the adoption of new technologies, communication protocols, and management strategies to ensure seamless collaboration across different time zones and locations.\n",
"\n",
"However, the transition to remote work also presents unique challenges that require careful consideration. Issues such as maintaining team cohesion, ensuring effective knowledge transfer, and preserving company culture become more complex when employees are physically separated. Organizations that successfully navigate these challenges typically invest in robust digital infrastructure, establish clear communication guidelines, and prioritize regular virtual team-building activities to foster meaningful connections among team members.\n",
"\"\"\".strip()"
],
"metadata": {
"id": "zzwCCEYjikYd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.299600Z",
"start_time": "2025-07-09T15:28:48.296207Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VfH0BBgCzll6",
"outputId": "d87b7640-0167-4f86-e71a-fdc9810d1b22"
},
"source": [
"print(\"📝 Sample text prepared:\")\n",
"print(\"-\" * 60)\n",
"print(sample_text)\n",
"print(\"-\" * 60)\n",
"print(f\"Text length: {len(sample_text)} characters\")\n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"📝 Sample text prepared:\n",
"------------------------------------------------------------\n",
"Remote work has fundamentally transformed how organiztions operate in the modern busness landscape. Companies worldwide have discovered that distributed teams can maintain high levels of productivity while offering employees greater flexibility and work-life balance. This shift has necessitated the adoption of new technologies, communication protocols, and management strategies to ensure seamless collaboration across different time zones and locations.\n",
"\n",
"However, the transition to remote work also presents unique challenges that require careful consideration. Issues such as maintaining team cohesion, ensuring effective knowledge transfer, and preserving company culture become more complex when employees are physically separated. Organizations that successfully navigate these challenges typically invest in robust digital infrastructure, establish clear communication guidelines, and prioritize regular virtual team-building activities to foster meaningful connections among team members.\n",
"------------------------------------------------------------\n",
"Text length: 997 characters\n"
]
}
],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"**❗FOR YOU: Choose your parameters for the check and set them here.**\n",
"\n",
"See [the docs](https://docs.acrolinx.com/api-reference/style-checks/create-style-check) for allowed values."
],
"metadata": {
"id": "zMFUJ_KLj3Kw"
}
},
{
"cell_type": "code",
"source": [
"style_guide = \"microsoft\"\n",
"tone = \"business\"\n",
"dialect = \"american_english\""
],
"metadata": {
"id": "LIJsdoDij2T_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"This cell starts the style check using the SDK. You won't see results output from here."
],
"metadata": {
"id": "2UezpHipjPvB"
}
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.789915Z",
"start_time": "2025-07-09T15:28:48.327564Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "E7IFFftpzll6",
"outputId": "32d60d3c-2392-443c-cfd5-51df0e3d97c1"
},
"source": [
"# Create a style check using the SDK\n",
"print(\"🔍 Creating style check with Acrolinx SDK...\")\n",
"\n",
"try:\n",
" # Create temporary file for upload (API requires file upload)\n",
" with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as tmp_file:\n",
" tmp_file.write(sample_text)\n",
" temp_file_path = tmp_file.name\n",
"\n",
" print(f\"📁 Created temporary file: {temp_file_path}\")\n",
"\n",
" # Submit the document for analysis\n",
" with (open(temp_file_path, 'rb') as file_obj):\n",
" check_response = client.style_suggestions.create_style_suggestion(\n",
" file_upload=file_obj,\n",
" style_guide=style_guide,\n",
" tone=tone,\n",
" dialect=dialect\n",
" )\n",
"\n",
" # Clean up temporary file\n",
" os.unlink(temp_file_path)\n",
" print(\"🗑️ Temporary file cleaned up\")\n",
"\n",
" # Display initial response\n",
" print(\"\\n📊 Style Check Submitted:\")\n",
" print(f\"Status: {check_response.status}\")\n",
" print(f\"Workflow ID: {check_response.workflow_id}\")\n",
"\n",
" # Store workflow ID for polling\n",
" workflow_id = check_response.workflow_id\n",
"\n",
"except Exception as e:\n",
" print(f\"❌ Error creating style check and suggestions: {e}\")\n",
" # Clean up if error occurs\n",
" if 'temp_file_path' in locals() and os.path.exists(temp_file_path):\n",
" os.unlink(temp_file_path)\n",
" print(\"🗑️ Temporary file cleaned up after error\")\n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"🔍 Creating style check with Acrolinx SDK...\n",
"📁 Created temporary file: /tmp/tmp9_fft8dy.txt\n",
"🗑️ Temporary file cleaned up\n",
"\n",
"📊 Style Check Submitted:\n",
"Status: running\n",
"Workflow ID: suggestions-3c9491d4-edcf-4064-b6fe-41e47678071b-20250709211749-646aa4e1\n"
]
}
],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"This cell validates the status of the check and will confirm when it is completed."
],
"metadata": {
"id": "wgxsvqa-jilu"
}
},
{
"cell_type": "code",
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.805852Z",
"start_time": "2025-07-09T15:28:48.801053Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VA_MmBYuzll6",
"outputId": "b08d2954-f0f5-4cba-84f6-3d95016dabaf"
},
"source": [
"# Poll for completion and get results\n",
"if 'workflow_id' in locals():\n",
" print(f\"⏳ Polling for completion of workflow: {workflow_id}\")\n",
"\n",
" max_attempts = 15\n",
" for attempt in range(max_attempts):\n",
" try:\n",
" # Check status\n",
" result = client.style_suggestions.get_style_suggestion(workflow_id=workflow_id)\n",
"\n",
" print(f\" Attempt {attempt + 1}: Status = {result.status}\")\n",
"\n",
" if result.status == 'completed':\n",
" print(\"\\n✅ Analysis completed!\")\n",
" final_result = result\n",
" break\n",
" elif result.status == 'failed':\n",
" print(\"\\n❌ Analysis failed!\")\n",
" break\n",
"\n",
" # Wait before next attempt\n",
" time.sleep(3)\n",
"\n",
" except Exception as e:\n",
" print(f\" Error on attempt {attempt + 1}: {e}\")\n",
" # If we get validation errors, try to continue polling\n",
" if \"validation error\" in str(e).lower():\n",
" print(\" (Continuing despite validation error...)\")\n",
" time.sleep(3)\n",
" continue\n",
" else:\n",
" break\n",
"\n",
" if attempt >= max_attempts - 1:\n",
" print(\"⏰ Timeout waiting for completion\")\n",
"else:\n",
" print(\"❌ No workflow_id available\")\n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"⏳ Polling for completion of workflow: suggestions-3c9491d4-edcf-4064-b6fe-41e47678071b-20250709211749-646aa4e1\n",
" Attempt 1: Status = running\n",
" Attempt 2: Status = completed\n",
"\n",
"✅ Analysis completed!\n"
]
}
],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"Finally, have a look at your results!"
],
"metadata": {
"id": "6k-PsoiljvIY"
}
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:48.996512Z",
"start_time": "2025-07-09T15:28:48.829283Z"
},
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "DejmLbVCzll7",
"outputId": "1da0bf04-228d-4a07-be12-708728bcda7a"
},
"cell_type": "code",
"source": [
"print(\"\\n\" + \"=\" * 60)\n",
"print(\"📋 ACROLINX ANALYSIS RESULTS\")\n",
"print(\"=\" * 60)\n",
"\n",
"scores = final_result.scores\n",
"print(\"📊 Quality Scores:\")\n",
"print(f\" 🏆 Quality: {scores.quality.score}/100\")\n",
"print(f\" 📝 Grammar: {scores.grammar.score}/100\")\n",
"print(f\" 📏 Style Guide: {scores.style_guide.score}/100\")\n",
"print(f\" 🎯 Tone: {scores.tone.score}/100\")\n",
"print(f\" 📚 Terminology: {scores.terminology.score}/100\")\n",
"\n",
"if final_result.issues:\n",
" issues = final_result.issues\n",
" print(f\"\\n🔧 Issues Found: {len(issues)}\")\n",
"\n",
" if len(issues) > 0:\n",
" print(\"\\nTop 10 Issues: \\nissue - suggestion - category\")\n",
" print(\"=\" * 60)\n",
" for i, issue in enumerate(issues[:10]):\n",
" print(f\"{i+1}. \\\"{issue.original}\\\" - \\\"{issue.suggestion}\\\" - {issue.category}\")\n",
" print(f\"\\n📊 Analysis Status: {final_result.status}\")\n",
" print(f\"📊 Workflow ID: {workflow_id}\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"============================================================\n",
"📋 ACROLINX ANALYSIS RESULTS\n",
"============================================================\n",
"📊 Quality Scores:\n",
" 🏆 Quality: 61/100\n",
" 📝 Grammar: 48/100\n",
" 📏 Style Guide: 48/100\n",
" 🎯 Tone: 86/100\n",
" 📚 Terminology: 100/100\n",
"\n",
"🔧 Issues Found: 13\n",
"\n",
"Top 10 Issues: \n",
"issue - suggestion - category\n",
"============================================================\n",
"1. \"organiztions\" - \"organizations\" - grammar\n",
"2. \"busness\" - \"business\" - grammar\n",
"3. \"organiztions\" - \"organizations\" - simple_vocab\n",
"4. \"necessitated\" - \"required\" - simple_vocab\n",
"5. \"has necessitated\" - \"requires\" - sentence_structure\n",
"6. \"require careful consideration\" - \"need to be carefully considered\" - sentence_structure\n",
"7. \"Organizations that successfully navigate these challenges typically invest in robust digital infrastructure, establish clear communication guidelines, and prioritize regular virtual team-building activities to foster meaningful connections among team members.\" - \"Successful organizations invest in digital infrastructure. They set clear guidelines and prioritize virtual team-building.\" - sentence_length\n",
"8. \"organiztions\" - \"organizations\" - tone\n",
"9. \"busness\" - \"business\" - tone\n",
"10. \"However, the\" - \"The\" - tone\n",
"\n",
"📊 Analysis Status: completed\n",
"📊 Workflow ID: suggestions-3c9491d4-edcf-4064-b6fe-41e47678071b-20250709211749-646aa4e1\n"
]
}
],
"execution_count": null
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-07-09T15:28:49.001609Z",
"start_time": "2025-07-09T15:28:16.222383Z"
},
"id": "kX_8TKoOzll7"
},
"cell_type": "code",
"source": [],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"kernelspec": {
"display_name": "michael",
"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.12.11"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment