Using Nostril to detect Gibberisness or nonsense text in a Google cloud function

26 Views Asked by At

I am trying to validate a text for gibberishness, length and language. Upon research i discovered a python library called nostril. I added the necessary library to my requirements.txt file and tried to import it and use it in a function. But upon deployment it keeps giving an error message.

Error message:

"<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/workspace/main.py", line 6, in from nostril import nonsense_detector as nd ImportError: cannot import name 'nonsense_detector' from 'nostril' (/layers/google.python.pip/pip/lib/python3.9/site-packages/nostril/init.py). Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

Code:

import openai
import json
import requests
from google.cloud import secretmanager
import re
from nostril import nonsense_detector as nd
from lingua import Language, LanguageDetectorBuilder
from flask import Response

# Initialize the Secret Manager client
client = secretmanager.SecretManagerServiceClient()

#Detect Gibberishness or nonsense
def is_nonsense_text(text):
    """Determine if the given text is likely to be nonsense."""
    if nd.nonsense(text):
        return True, "This is a nonsense text"
    else:
        return False, "This is a great text"

I also tried:

from nostril import nonsense 

But it doesnt seem to work. As it keeps the same error message.

code for validation:

#Validate the input received from dialogflow cx
def validate_query(query):
    """Perform input validation checks on the query."""
    # Minimum Length Check
    if len(query) < 10:
        return False, "Query too short."    
    # Repetition Check
    if re.search(r'(.)\1{3,}', query): 
        return False, "Query contains excessive repetitive characters."
    
    # Gibberish (Nonsense) Detection
   # Nonsense detection
    is_nonsense, message = is_nonsense_text(query)
    if is_nonsense:
        return False, message
    
    # Language Detection
    detected_language = Language_detector.detect_language_of(query)
    if detected_language != Language.ENGLISH:
        return False, "Query not in English."
    
return True, ""

Please is there a way to solve this issue?

0

There are 0 best solutions below