Spaces:
Build error
Build error
Update pipeline.py
Browse files12345675 unmodified lines1314151617181920212223242526272829303132333435363711 unmodified lines4950515253545556575859606162636465666768692 unmodified lines72737475767778import osimport getpassfrom pydantic_ai import Agent # Import the Agent from pydantic_aifrom pydantic_ai.models.mistral import MistralModel # Import the Mistral modelimport spacy # Import spaCy for NER functionalityimport pandas as pdfrom typing import Optional5 unmodified linesimport subprocess # Import subprocess to run shell commandsfrom langchain.llms.base import LLM # Import LLMfrom classification_chain import get_classification_chainfrom refusal_chain import get_refusal_chainfrom tailor_chain import get_tailor_chainfrom cleaner_chain import get_cleaner_chain, CleanerChain# 1) Environment: set up keys if missingif not os.environ.get("GEMINI_API_KEY"):os.environ["GEMINI_API_KEY"] = getpass.getpass("Enter your Gemini API Key: ")if not os.environ.get("GROQ_API_KEY"):os.environ["GROQ_API_KEY"] = getpass.getpass("Enter your GROQ API Key: ")if not os.environ.get("MISTRAL_API_KEY"):os.environ["MISTRAL_API_KEY"] = getpass.getpass("Enter your Mistral API Key: ")# Initialize Mistral agent using Pydantic AImistral_api_key = os.environ.get("MISTRAL_API_KEY") # Ensure your Mistral API key is setmistral_model = MistralModel("mistral-moderation-latest", api_key=mistral_api_key) # Use a Mistral modelmistral_agent = Agent(mistral_model)# Load spaCy model for NER and download the spaCy model if not already installeddef install_spacy_model():11 unmodified lines# Load the spaCy model globallynlp = spacy.load("en_core_web_sm")# Function to moderate text using Pydantic AI's Mistral moderation modeldef moderate_text(query: str) -> str:"""Classifies the query as harmful or not using Mistral Moderation via Pydantic AI.Returns "OutOfScope" if harmful, otherwise returns the original query."""# Use the moderation API to evaluate if the query is harmfulresponse = mistral_agent.model.classifiers.moderate_chat(model="mistral-moderation-latest",inputs=[{"role": "user", "content": query},],)# Assuming the response contains 'results' with category scorescategories = response['results'][0]['categories']# Check if harmful content is flagged in moderation categories2 unmodified linescategories.get("dangerous_and_criminal_content", False) or \categories.get("selfharm", False):return "OutOfScope"return query# 3) build_or_load_vectorstore (no changes)def build_or_load_vectorstore(csv_path: str, store_dir: str) -> FAISS:if os.path.exists(store_dir):123455 unmodified lines11121314151617181920212211 unmodified lines3435363738394041424344454647484950515253542 unmodified lines575859606162636465import osimport getpassimport spacy # Import spaCy for NER functionalityimport pandas as pdfrom typing import Optional5 unmodified linesimport subprocess # Import subprocess to run shell commandsfrom langchain.llms.base import LLM # Import LLM# Mistral Client Setupfrom mistralai import Mistral # Import the Mistral client# Initialize Mistral API clientmistral_api_key = os.environ.get("MISTRAL_API_KEY") # Ensure your Mistral API key is setclient = Mistral(api_key=mistral_api_key)# Load spaCy model for NER and download the spaCy model if not already installeddef install_spacy_model():11 unmodified lines# Load the spaCy model globallynlp = spacy.load("en_core_web_sm")# Function to moderate text using Mistral moderation APIdef moderate_text(query: str) -> str:"""Classifies the query as harmful or not using Mistral Moderation via Mistral API.Returns "OutOfScope" if harmful, otherwise returns the original query."""# Use the moderation API to evaluate if the query is harmfulresponse = client.classifiers.moderate_chat(model="mistral-moderation-latest",inputs=[{"role": "user", "content": query},],)# Extracting category scores from responsecategories = response['results'][0]['categories']# Check if harmful content is flagged in moderation categories2 unmodified linescategories.get("dangerous_and_criminal_content", False) or \categories.get("selfharm", False):return "OutOfScope"return query# 3) build_or_load_vectorstore (no changes)def build_or_load_vectorstore(csv_path: str, store_dir: str) -> FAISS:if os.path.exists(store_dir):
pipeline.py+11-24