Spaces:
Running
Running
File size: 18,117 Bytes
2bc0211 a75f557 2bc0211 9fba415 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
import os
import gradio as gr
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_core.vectorstores import InMemoryVectorStore
from langchain.tools.retriever import create_retriever_tool
from langgraph.graph import MessagesState, StateGraph, START, END
from langchain.chat_models import init_chat_model
from langgraph.prebuilt import ToolNode, tools_condition
from pydantic import BaseModel, Field
from typing import Literal
from langchain_core.messages import HumanMessage, AIMessage
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Get API key from Hugging Face Spaces secrets
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if GOOGLE_API_KEY:
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
else:
logger.warning("GOOGLE_API_KEY not found in environment variables")
class LegalConsultingBot:
def __init__(self):
self.graph = None
self.retriever_tool = None
self.response_model = None
self.grader_model = None
self.initialize_bot()
def initialize_bot(self):
"""Initialize the bot with error handling."""
try:
if not GOOGLE_API_KEY:
logger.error("Google API key not available")
return
self.setup_models()
self.setup_retriever()
self.setup_workflow()
logger.info("Bot initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize bot: {e}")
def setup_models(self):
"""Initialize the language models."""
try:
self.response_model = init_chat_model(
"gemini-2.0-flash",
model_provider="google_genai",
temperature=0
)
self.grader_model = init_chat_model(
"gemini-2.0-flash",
model_provider="google_genai",
temperature=0
)
logger.info("Models initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize models: {e}")
raise
def setup_retriever(self):
"""Initialize the document retriever with legal consulting URLs."""
urls = [
"https://firststepslegal.co.uk/",
"https://sprintlaw.co.uk/",
"https://www.smbs.solutions/legal-and-compliance-resources-legal-assistance-for-businesses",
"https://ignition.law/lawyers-for-smes/",
"https://www.cocredo.co.uk/news/free-legal-advice-small-business-owners",
"https://www.gannons.co.uk/sectors/smes/",
"https://kkbservices.com/who-we-work-with/small-businesses/",
"https://farringfordlegal.co.uk/",
"https://stanislawlegal.com/en/legal-solutions/for-sme-companies/",
"https://www.lawhive.co.uk/small-business/",
"https://www.catalystlaw.co.uk/business-legal-advice.html",
"https://medium.com/@kmitsme123/legal-consulting-tips-for-your-small-business-9075005eb574",
"https://smecomply.co.uk/",
"https://dojobusiness.com/blogs/news/legal-consultant-complete-guide",
"https://englishlegaladvice.com/",
]
try:
# Load documents with error handling
docs = []
successful_loads = 0
for url in urls:
try:
loader = WebBaseLoader(url)
docs.extend(loader.load())
successful_loads += 1
logger.info(f"Successfully loaded: {url}")
except Exception as e:
logger.warning(f"Failed to load {url}: {e}")
continue
logger.info(f"Successfully loaded {successful_loads}/{len(urls)} URLs")
if not docs:
logger.warning("No documents could be loaded, using fallback mode")
return
# Split documents
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=300, chunk_overlap=50
)
doc_splits = text_splitter.split_documents(docs)
logger.info(f"Created {len(doc_splits)} document chunks")
# Create embeddings and vectorstore
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vectorstore = InMemoryVectorStore.from_documents(
documents=doc_splits,
embedding=embeddings
)
# Create retriever tool
retriever = vectorstore.as_retriever()
self.retriever_tool = create_retriever_tool(
retriever,
name="legal_consulting_retriever",
description="Search and return relevant legal information and consulting resources for small and medium-sized businesses."
)
logger.info("Retriever tool created successfully")
except Exception as e:
logger.error(f"Error setting up retriever: {e}")
self.retriever_tool = None
def setup_workflow(self):
"""Set up the LangGraph workflow."""
try:
if not self.response_model:
logger.error("Response model not available")
return
# Create workflow
workflow = StateGraph(MessagesState)
# Add nodes
workflow.add_node("generate_query_or_respond", self.generate_query_or_respond)
if self.retriever_tool:
workflow.add_node("retrieve", ToolNode([self.retriever_tool]))
workflow.add_node("grade_documents", self.grade_documents_node)
workflow.add_node("rewrite_question", self.rewrite_question)
workflow.add_node("generate_answer", self.generate_answer)
# Add edges
workflow.add_edge(START, "generate_query_or_respond")
if self.retriever_tool:
workflow.add_conditional_edges(
"generate_query_or_respond",
tools_condition,
{
"tools": "retrieve",
END: END,
},
)
workflow.add_edge("retrieve", "grade_documents")
workflow.add_conditional_edges(
"grade_documents",
lambda x: x.get("grade_result", "generate_answer"),
{
"generate_answer": "generate_answer",
"rewrite_question": "rewrite_question"
}
)
else:
workflow.add_edge("generate_query_or_respond", END)
workflow.add_edge("generate_answer", END)
workflow.add_edge("rewrite_question", "generate_query_or_respond")
self.graph = workflow.compile()
logger.info("Workflow compiled successfully")
except Exception as e:
logger.error(f"Error setting up workflow: {e}")
self.graph = None
def generate_query_or_respond(self, state: MessagesState):
"""Generate query or respond directly."""
try:
if not self.retriever_tool:
# Fallback response when retriever is not available
fallback_response = """I'm a legal consulting assistant for small and medium enterprises. While my document retriever is currently unavailable, I can still help answer general questions about:
- Business formation and structure
- Contract basics and employment law
- Intellectual property fundamentals
- Compliance and regulatory matters
- General legal considerations for SMEs
Please note: This is general information only, not legal advice. Always consult qualified legal professionals for specific matters."""
return {"messages": [AIMessage(content=fallback_response)]}
response = (
self.response_model
.bind_tools([self.retriever_tool])
.invoke(state["messages"])
)
return {"messages": [response]}
except Exception as e:
logger.error(f"Error in generate_query_or_respond: {e}")
return {"messages": [AIMessage(content="I'm sorry, I encountered an error. Please try again.")]}
class GradeDocuments(BaseModel):
"""Grade documents using a binary score for relevance check."""
binary_score: str = Field(
description="Relevance score: 'yes' if relevant, or 'no' if not relevant"
)
def grade_documents_node(self, state: MessagesState):
"""Node wrapper for document grading."""
try:
question = state["messages"][0].content
context = state["messages"][-1].content if state["messages"] else ""
GRADE_PROMPT = (
"You are a grader assessing relevance of a retrieved document to a user question. \n "
"Here is the retrieved document: \n\n {context} \n\n"
"Here is the user question: {question} \n"
"If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant. \n"
"Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question."
)
prompt = GRADE_PROMPT.format(question=question, context=context)
response = (
self.grader_model
.with_structured_output(self.GradeDocuments)
.invoke([{"role": "user", "content": prompt}])
)
grade_result = "generate_answer" if response.binary_score == "yes" else "rewrite_question"
return {"grade_result": grade_result}
except Exception as e:
logger.error(f"Error in grade_documents_node: {e}")
return {"grade_result": "generate_answer"} # Default to generating answer
def rewrite_question(self, state: MessagesState):
"""Rewrite the original user question."""
try:
messages = state["messages"]
question = messages[0].content if messages else ""
REWRITE_PROMPT = (
"Look at the input and try to reason about the underlying semantic intent / meaning.\n"
"Here is the initial question:"
"\n ------- \n"
"{question}"
"\n ------- \n"
"Formulate an improved question that would be better for searching legal consulting information:"
)
prompt = REWRITE_PROMPT.format(question=question)
response = self.response_model.invoke([{"role": "user", "content": prompt}])
return {"messages": [HumanMessage(content=response.content)]}
except Exception as e:
logger.error(f"Error in rewrite_question: {e}")
# Return original message if rewriting fails
return {"messages": state["messages"][:1] if state["messages"] else []}
def generate_answer(self, state: MessagesState):
"""Generate an answer based on retrieved context."""
try:
question = state["messages"][0].content if state["messages"] else ""
context = state["messages"][-1].content if len(state["messages"]) > 1 else ""
GENERATE_PROMPT = (
"You are an assistant for question-answering tasks about legal consulting for small and medium businesses. "
"Use the following pieces of retrieved context to answer the question. "
"If you don't know the answer, just say that you don't know. "
"Keep the answer concise but informative. Always remind users that this is general information and not legal advice.\n"
"Question: {question} \n"
"Context: {context}"
)
prompt = GENERATE_PROMPT.format(question=question, context=context)
response = self.response_model.invoke([{"role": "user", "content": prompt}])
return {"messages": [response]}
except Exception as e:
logger.error(f"Error in generate_answer: {e}")
return {"messages": [AIMessage(content="I'm sorry, I encountered an error while generating the answer. Please try again.")]}
def chat(self, message: str, history: list) -> str:
"""Main chat function for Gradio interface."""
try:
if not message or not message.strip():
return "Please enter a question."
if not self.response_model:
return "Sorry, the system is not properly initialized. Please check if the API key is configured correctly."
# For simple cases without retriever, provide direct response
if not self.graph:
prompt = f"""You are a helpful assistant specializing in legal consulting for small and medium enterprises.
Answer this question: {message}
Always remind users that this is general information only and not professional legal advice."""
response = self.response_model.invoke([{"role": "user", "content": prompt}])
return response.content if hasattr(response, 'content') else str(response)
# Create initial state
initial_state = {"messages": [HumanMessage(content=message)]}
# Run the graph
result = self.graph.invoke(initial_state)
# Extract the final response
if result and "messages" in result and result["messages"]:
final_message = result["messages"][-1]
if hasattr(final_message, 'content'):
return final_message.content
else:
return str(final_message)
else:
return "I'm sorry, I couldn't generate a response. Please try again."
except Exception as e:
logger.error(f"Error in chat function: {e}")
return f"An error occurred while processing your request. Please try again."
# Initialize the bot
logger.info("Initializing Legal Consulting Bot...")
bot = LegalConsultingBot()
# Create Gradio interface
def create_interface():
with gr.Blocks(
title="Legal Consulting Assistant for SMEs",
theme=gr.themes.Soft(),
css="""
.container {
max-width: 800px;
margin: auto;
}
"""
) as demo:
gr.Markdown("""
# π’ Legal Consulting Assistant for Small & Medium Enterprises
Get informed answers about legal consulting, compliance, and business law for SMEs.
This assistant uses information from various legal consulting websites to provide relevant guidance.
**β οΈ Important**: This provides general information only and does not constitute professional legal advice.
""")
with gr.Row():
with gr.Column(scale=4):
chatbot = gr.Chatbot(
height=500,
placeholder="π¬ Ask me about legal consulting for your business...",
avatar_images=("π€", "π€"),
bubble_full_width=False
)
with gr.Row():
msg = gr.Textbox(
placeholder="e.g., What legal structure should I choose for my startup?",
label="Your Question",
scale=4
)
submit_btn = gr.Button("Send", variant="primary", scale=1)
clear = gr.Button("ποΈ Clear Chat", variant="secondary")
def respond(message, chat_history):
if not message.strip():
return "", chat_history
# Show typing indicator
chat_history.append((message, "π€ Thinking..."))
yield "", chat_history
# Get bot response
bot_message = bot.chat(message, chat_history)
chat_history[-1] = (message, bot_message)
yield "", chat_history
def clear_chat():
return []
# Event handlers
msg.submit(respond, [msg, chatbot], [msg, chatbot])
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
clear.click(clear_chat, outputs=[chatbot])
gr.Markdown("""
### π Example Questions
- *What legal structure should I choose for my small business?*
- *What compliance requirements do SMEs need to consider?*
- *How can I protect my business's intellectual property?*
- *What should be included in employment contracts?*
### βοΈ Disclaimer
This chatbot provides general information about legal consulting for SMEs based on publicly available resources.
**This information should not be considered as professional legal advice.**
Always consult with qualified legal professionals for specific legal matters and before making important business decisions.
""")
return demo
# Create and launch the interface
if __name__ == "__main__":
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |