Spaces:
Runtime error
Runtime error
Download engine.py from Rajakumari/AI-Security-Assistant: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Rajakumari/AI-Security-Assistant/resolve/main/engine.py
- Command line
-
hf download hf://spaces/Rajakumari/AI-Security-Assistant/engine.py
-
curl -L -o engine.py https://huggingface.co/spaces/Rajakumari/AI-Security-Assistant/resolve/main/engine.py
1.63 kB
| import os | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain_huggingface import HuggingFaceEndpoint | |
| from langchain_core.messages import HumanMessage, AIMessage | |
| class AIAssistant: | |
| def __init__(self, model_type="frontier"): | |
| self.history = [] | |
| self.model_type = model_type | |
| if model_type == "frontier": | |
| # Model name changed to gemini-1.5-pro for better stability | |
| self.llm = ChatGoogleGenerativeAI( | |
| model="gemini-1.5-pro", | |
| google_api_key=os.getenv("GOOGLE_API_KEY") | |
| ) | |
| else: | |
| self.llm = HuggingFaceEndpoint( | |
| repo_id="Qwen/Qwen2.5-0.5B-Instruct", | |
| huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN"), | |
| temperature=0.7 | |
| ) | |
| def get_answer(self, user_input): | |
| harmful_keywords = ["bomb", "hack", "steal", "kill"] | |
| if any(word in user_input.lower() for word in harmful_keywords): | |
| return "🛡️ Safety Refusal: I cannot assist with harmful or illegal requests." | |
| try: | |
| self.history.append(HumanMessage(content=user_input)) | |
| response = self.llm.invoke(self.history) | |
| res_text = response.content if hasattr(response, 'content') else str(response) | |
| self.history.append(AIMessage(content=res_text)) | |
| return res_text | |
| except Exception as e: | |
| # ఇక్కడ వస్తున్న ఎర్రర్ ని క్లియర్ గా చూపిస్తుంది | |
| return f"❌ Error: {str(e)}" |