Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Module 'google.cloud.language_v1' has no attribute 'ClassificationModelOptions' #391

Closed
dharam-ml opened this issue Oct 28, 2022 · 3 comments · Fixed by #388
Closed

Module 'google.cloud.language_v1' has no attribute 'ClassificationModelOptions' #391

dharam-ml opened this issue Oct 28, 2022 · 3 comments · Fixed by #388
Assignees
Labels
api: language Issues related to the googleapis/python-language API. samples Issues that are directly related to samples. type: docs Improvement to the documentation for an API.

Comments

@dharam-ml
Copy link

Context: Error while making the API call to get categories from V2 Model. Script is running fine for old code with default version.

Problem: I have a script that uses the google.cloud.language_v1 module to perform text classification on a set of records every day. It was working fine until I made modifications to use the V2Model content categories.

I changed the content_categories_version value to explicitly specify the version for the model. I used the code from language_classify_text.py sample by @wizeng23 in the github repo. However, on running the script I receive the below error:

module 'google.cloud.language_v1' has no attribute 'ClassificationModelOptions'

Environment details

  • OS type and version: macOS Monterey Version 12.6
  • Python version: 3.7.13
  • pip version: 22.0.4
  • google-cloud-language version: 2.5.1

Steps to reproduce

  1. Initialize document and content_categories_version as below.
  2. Run the script with a text string

Code example

## Providing the code I'm using for testing the API call:

import os
from google.cloud import language_v1

## Set Google API Key
os.environ.get("GOOGLE_APPLICATION_CREDENTAILS")

client = language_v1.LanguageServiceClient()

def categorize(txt):
    """
    Takes a string as an argument and returns the category with highest confidence received from the Google NLP API.
    """
    result = {}

    document = language_v1.Document(content = txt, type_=language_v1.types.Document.Type.PLAIN_TEXT)
    content_categories_version = (language_v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion.V2)
    request_json = {
        "document": document,
        "classification_model_options": {
            "v2_model": {
                "content_categories_version": content_categories_version
                }
            }
        }

    response = client.classify_text(request=request_json)
    print(response)

    for category in response.categories:
        result[category.name] = category.confidence
    
    print(result)

    best_response = max(result, key = result.get)

    lst = best_response.split(sep = '/')
    out_category = lst[-1]
          
    return out_category

txt = 'Dungeons and Dragons'

categorize(txt)

Stack trace

Traceback (most recent call last):
  File "nlp_api_test.py", line 43, in <module>
    categorize(txt)
  File "nlp_api_test.py", line 16, in categorize
    content_categories_version = (language_v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion.V2)
AttributeError: module 'google.cloud.language_v1' has no attribute 'ClassificationModelOptions'

Please let me know if I'm doing anything incorrectly.

Thanks!

@product-auto-label product-auto-label bot added the api: language Issues related to the googleapis/python-language API. label Oct 28, 2022
@luelhagos
Copy link

@dharam-ml Did you solve the issue? I also got the same error.

@dharam-ml
Copy link
Author

@luelhagos Yes, you need to set the google-cloud-language version to >=2.6.0 in the requirements file. I checked and realized that there is a pull request pending from google for this implementation.

@parthea parthea added type: docs Improvement to the documentation for an API. samples Issues that are directly related to samples. labels Nov 4, 2022
@parthea
Copy link
Contributor

parthea commented Nov 4, 2022

Hi @dharam-ml,

Thanks for the error report! I'm glad that the issue is resolved. We'll merge #388 in case it helps others. The release notes for 2.6.0 mention Add support for V1 and V2 classification models but perhaps we should explicitly list new classes added. I'll think about improving the release notes.
https://github.com/googleapis/python-language/releases

@nicain nicain self-assigned this Nov 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: language Issues related to the googleapis/python-language API. samples Issues that are directly related to samples. type: docs Improvement to the documentation for an API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants