Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the nlp bug #317

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion applications/rag/frontend/container/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import google.cloud.logging as logging
import sqlalchemy
import pymysql
import traceback
import json

from flask import Flask, render_template, request, jsonify
Expand Down Expand Up @@ -173,7 +174,7 @@ def handlePrompt():
"user_prompt": user_prompt
})
if 'nlpFilterLevel' in data:
if nlp_filter.is_content_inappropriate(response['text'], data.get['nlpFilterLevel']):
if nlp_filter.is_content_inappropriate(response['text'], data['nlpFilterLevel']):
response['text'] = 'The response is deemed inappropriate for display.'
elif 'inspectTemplate' in data and 'deidentifyTemplate' in data:
inspect_template_path = data['inspectTemplate']
Expand All @@ -185,6 +186,7 @@ def handlePrompt():
return {'response': response}
except Exception as err:
log.info(f"exception from llm: {err}")
traceback.print_exc()
return str(err), 500


Expand Down
13 changes: 6 additions & 7 deletions applications/rag/frontend/container/rai/nlp_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os
import google.cloud.language as language
import google.cloud.language_v1 as language
from . import retry

# Convert the project id into a full resource id.
Expand Down Expand Up @@ -45,14 +45,13 @@ def sum_moderation_confidences(text):
response = nature_language_client.moderate_text(
request=request, retry=retry.retry_policy
)

print(f'get response: {response}')
# Parse response and sum the confidences of moderation, the categories are from https://cloud.google.com/natural-language/docs/moderating-text
total_confidence = 0.0
categories = []
for category in response.moderation_categories:
total_confidence += category.confidence
categories.append(category.name)
return total_confidence, categories
print(f'total confidence is: {total_confidence}')
return total_confidence

def is_content_inappropriate(text, nlp_filter_level):
# Define thresholds
Expand All @@ -63,9 +62,9 @@ def is_content_inappropriate(text, nlp_filter_level):
}

# Map the filter level to a threshold
if nlp_filter_level <= 20:
if int(nlp_filter_level) <= 20:
threshold = thresholds['high']
elif nlp_filter_level <= 50:
elif int(nlp_filter_level) <= 50:
threshold = thresholds['mid']
else:
threshold = thresholds['low']
Expand Down
11 changes: 6 additions & 5 deletions applications/rag/frontend/container/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ function onReady() {

// Handle templates
document.getElementById("toggle-filters").addEventListener("change", function() {
var filterOptions = document.getElementById("template-section");
filterOptions.style.display = this.checked ? "block" : "none";
fetchNLPEnabled();
fetchDLPEnabled();
});
}
if (document.readyState != "loading") onReady();
Expand Down Expand Up @@ -113,8 +113,9 @@ function autoResizeTextarea() {
// Function to handle the visibility of filter section
function toggleNlpFilterSection(nlpEnabled) {
var filterOptions = document.getElementById("nlp-filter-section");
var checkbox = document.getElementById('toggle-filters');

if (nlpEnabled) {
if (nlpEnabled && checkbox.checked) {
filterOptions.style.display = "block";
} else {
filterOptions.style.display = "none";
Expand All @@ -136,8 +137,8 @@ function fetchNLPEnabled() {
// Function to handle the visibility of filter section
function toggleFilterSection(dlpEnabled) {
var filterOptions = document.getElementById("template-section");

if (dlpEnabled) {
var checkbox = document.getElementById('toggle-filters');
if (dlpEnabled && checkbox.checked) {
filterOptions.style.display = "block";
} else {
filterOptions.style.display = "none";
Expand Down
4 changes: 2 additions & 2 deletions applications/rag/frontend/container/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<label for="toggle-filters">Enable Filters:</label>
<input type="checkbox" id="toggle-filters">

<!-- Dropdown for inspect_templates and deidentify_templates -->
<!-- Dropdown for inspect_templates and deidentify_templates for dlp filter -->
<div id="template-section" style="display: none;">
<select id="inspect-template-dropdown">
<!-- Options will be dynamically added here -->
Expand All @@ -49,7 +49,7 @@
</div>

<!-- NLP Filter Slider -->
<div id="nlp-filter-section">
<div id="nlp-filter-section" style="display: none;">
<label for="nlp-range">NLP Filter Level:</label>
<input type="range" id="nlp-range" min="0" max="100" value="50">
</div>
Expand Down