Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,16 @@ from langchain.llms import HuggingFacePipeline
|
|
| 16 |
import gradio as gr
|
| 17 |
import re
|
| 18 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
import inflection
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
"""
|
|
@@ -119,10 +128,76 @@ def get_confidence_score(question):
|
|
| 119 |
return min(1.0, round(max_score, 2)) # Normalize to 0-1 scale
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
"""### π Step 10: Integrate with Gradio UI"""
|
| 123 |
|
| 124 |
# Define Chatbot Function
|
| 125 |
def chat_with_rag(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
response = conversation_chain.invoke(message)
|
| 128 |
confidence_score = get_confidence_score(message)
|
|
@@ -155,19 +230,18 @@ print(f"π **Answer:** {to_camel_case(output)}\n\n**Confidence Score:** {confi
|
|
| 155 |
# Create Gradio Chatbot UI with Auto-Clearing Input
|
| 156 |
demo = gr.ChatInterface(
|
| 157 |
fn=chat_with_rag, # Function to generate responses
|
| 158 |
-
title="π Financial RAG Chatbot",
|
| 159 |
-
description="Ask questions about financial reports and get AI-powered answers!",
|
| 160 |
-
theme="soft",
|
| 161 |
examples=[
|
| 162 |
["What are the biggest challenges for Apple?"],
|
| 163 |
["What was Apple's total revenue in 2024?"],
|
| 164 |
["What are the biggest financial risks for Apple?"],
|
| 165 |
-
["What is the capital of France?"]
|
| 166 |
],
|
| 167 |
-
submit_btn="Ask",
|
| 168 |
-
stop_btn=None,
|
| 169 |
)
|
| 170 |
|
| 171 |
-
|
| 172 |
if __name__ == "__main__":
|
| 173 |
demo.launch()
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
import re
|
| 18 |
from bs4 import BeautifulSoup
|
| 19 |
+
from guardrails.validators import Validator, register_validator, ValidationResult, FailResult, PassResult
|
| 20 |
+
from presidio_analyzer import AnalyzerEngine
|
| 21 |
+
from presidio_analyzer.nlp_engine import SpacyNlpEngine, NlpEngineProvider
|
| 22 |
+
from better_profanity import profanity
|
| 23 |
+
from presidio_analyzer import PatternRecognizer, Pattern
|
| 24 |
import inflection
|
| 25 |
+
from guardrails import Guard
|
| 26 |
+
import warnings
|
| 27 |
+
# Suppress all warnings
|
| 28 |
+
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
|
| 31 |
"""
|
|
|
|
| 128 |
return min(1.0, round(max_score, 2)) # Normalize to 0-1 scale
|
| 129 |
|
| 130 |
|
| 131 |
+
## GuardRail validators
|
| 132 |
+
|
| 133 |
+
# Define NLP Configuration with lang_code
|
| 134 |
+
nlp_configuration = {
|
| 135 |
+
"nlp_engine_name": "spacy",
|
| 136 |
+
"models": [{"lang_code": "en", "model_name": "en_core_web_lg"}], # Specify language
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
# Define SSN Pattern
|
| 140 |
+
ssn_regex = r"\b\d{3}-\d{2}-\d{4}\b" # Matches US SSN format (123-45-6789)
|
| 141 |
+
ssn_pattern = Pattern(name="SSN Pattern", regex=ssn_regex, score=0.85) # Score between 0-1
|
| 142 |
+
|
| 143 |
+
# Create Custom SSN Recognizer
|
| 144 |
+
ssn_recognizer = PatternRecognizer(supported_entity="SSN", patterns=[ssn_pattern])
|
| 145 |
+
|
| 146 |
+
analyzer = AnalyzerEngine()
|
| 147 |
+
analyzer.registry.add_recognizer(ssn_recognizer)
|
| 148 |
+
|
| 149 |
+
@register_validator(name="custom_pii_detector", data_type="string")
|
| 150 |
+
class CustomPIIDetector(Validator):
|
| 151 |
+
|
| 152 |
+
def validate(self, value, metadata={}) -> ValidationResult:
|
| 153 |
+
# Analyze text for PII
|
| 154 |
+
results = analyzer.analyze(text=value, entities=["PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD", "SSN"], language="en")
|
| 155 |
+
|
| 156 |
+
if results:
|
| 157 |
+
detected_entities = ", ".join(set([res.entity_type for res in results]))
|
| 158 |
+
return FailResult(
|
| 159 |
+
error_message=f"Query contains PII: {detected_entities}."
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
return PassResult()
|
| 163 |
+
|
| 164 |
+
# Custom Profanity Detector using better-profanity
|
| 165 |
+
@register_validator(name="custom_profanity_detector", data_type="string")
|
| 166 |
+
class CustomProfanityDetector(Validator):
|
| 167 |
+
def validate(self, value, metadata={}) -> ValidationResult:
|
| 168 |
+
if profanity.contains_profanity(value):
|
| 169 |
+
return FailResult(
|
| 170 |
+
error_message="Query contains profanity."
|
| 171 |
+
)
|
| 172 |
+
return PassResult()
|
| 173 |
+
|
| 174 |
+
# Custom Relevance Validator for Finance and Apple-related Queries
|
| 175 |
+
@register_validator(name="custom_relevance_detector", data_type="string")
|
| 176 |
+
class CustomRelevanceDetector(Validator):
|
| 177 |
+
def validate(self, value, metadata={}) -> ValidationResult:
|
| 178 |
+
finance_keywords = {"revenue", "profit", "expenses", "balance sheet", "earnings", "financial", "investment", "dividends", "assets", "liabilities", "cash flow", "loss","turnover"}
|
| 179 |
+
apple_keywords = {"apple", "iphone", "macbook", "tim cook", "apple inc", "ios", "mac", "ipad"}
|
| 180 |
+
|
| 181 |
+
text_lower = value.lower()
|
| 182 |
+
|
| 183 |
+
# Check if any finance-related or Apple-related keyword appears in the query
|
| 184 |
+
if not any(keyword in text_lower for keyword in (finance_keywords | apple_keywords)): # Use set union
|
| 185 |
+
return FailResult(
|
| 186 |
+
error_message="Query is not related to finance or Apple."
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
return PassResult()
|
| 190 |
+
|
| 191 |
+
guard = Guard().use(CustomPIIDetector).use(CustomProfanityDetector).use(CustomRelevanceDetector)
|
| 192 |
+
|
| 193 |
"""### π Step 10: Integrate with Gradio UI"""
|
| 194 |
|
| 195 |
# Define Chatbot Function
|
| 196 |
def chat_with_rag(message, history):
|
| 197 |
+
try:
|
| 198 |
+
res = guard.validate(message)
|
| 199 |
+
except Exception as e:
|
| 200 |
+
return f"β Guardrail {str(e)}"
|
| 201 |
try:
|
| 202 |
response = conversation_chain.invoke(message)
|
| 203 |
confidence_score = get_confidence_score(message)
|
|
|
|
| 230 |
# Create Gradio Chatbot UI with Auto-Clearing Input
|
| 231 |
demo = gr.ChatInterface(
|
| 232 |
fn=chat_with_rag, # Function to generate responses
|
| 233 |
+
title="π Financial Basic RAG Chatbot",
|
| 234 |
+
description="Ask questions about Apple's financial reports and get AI-powered answers!",
|
| 235 |
+
theme="soft",
|
| 236 |
examples=[
|
| 237 |
["What are the biggest challenges for Apple?"],
|
| 238 |
["What was Apple's total revenue in 2024?"],
|
| 239 |
["What are the biggest financial risks for Apple?"],
|
| 240 |
+
["What is the capital of France?"],
|
| 241 |
],
|
| 242 |
+
submit_btn="Ask",
|
| 243 |
+
stop_btn=None,
|
| 244 |
)
|
| 245 |
|
|
|
|
| 246 |
if __name__ == "__main__":
|
| 247 |
demo.launch()
|