import gradio as gr import os import requests GROQ_API_KEY = os.environ.get("GROQ_API_KEY") GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" MODEL_NAME = "llama-3.3-70b-versatile" SYSTEM_PROMPT = """You are a professional and knowledgeable Legal Advisor chatbot named LexBot. You provide general legal information and guidance to users in a clear, helpful, and accessible manner. Your personality: - Professional yet approachable - Clear and concise in explanations - Empathetic and understanding - Always includes appropriate legal disclaimers Your expertise covers: - Contract Law (agreements, breaches, obligations) - Employment Law (rights, discrimination, termination, workplace rights) - Property Law (real estate, landlord-tenant, ownership) - Criminal Law (rights, procedures, charges) - Family Law (divorce, custody, adoption, women's rights) - Intellectual Property (copyright, patents, trademarks) - Tort Law (negligence, personal injury, damages) - Business Law (corporations, LLCs, partnerships) - Civil Rights and Gender Equality (discrimination, equal rights, workplace equality) IMPORTANT GUIDELINES: - Provide general legal information only, not specific legal advice - Always remind users that laws vary by jurisdiction - Encourage users to consult qualified attorneys for specific cases - Be clear that you cannot replace professional legal counsel - Use simple language to explain complex legal concepts - If asked about something outside your knowledge, politely redirect to relevant legal topics - When discussing women's rights or gender equality, provide comprehensive information about legal protections, workplace rights, discrimination laws, and relevant legislation""" def query_groq(message, chat_history, legal_topic="General", response_length="Medium", temperature=0.7): """Query GROQ API with the message and chat history""" if not GROQ_API_KEY: return "Error: GROQ_API_KEY not set. Please set your API key in environment variables." headers = { "Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json" } # Build system prompt with topic context topic_context = "" if legal_topic != "General": topic_context = f"\n\nCurrent focus area: {legal_topic}. Provide information relevant to this legal domain." length_instruction = "" if response_length == "Brief": length_instruction = " Keep your response concise (2-3 sentences)." elif response_length == "Detailed": length_instruction = " Provide a comprehensive explanation with examples." else: length_instruction = " Provide a balanced explanation (3-5 sentences)." enhanced_system_prompt = SYSTEM_PROMPT + topic_context + length_instruction messages = [{"role": "system", "content": enhanced_system_prompt}] # Add chat history - handle dictionary format for Gradio 5.x if chat_history: for entry in chat_history: if isinstance(entry, dict): # Already in correct format messages.append(entry) # Add current message if message and isinstance(message, str) and message.strip(): messages.append({"role": "user", "content": message.strip()}) try: response = requests.post( GROQ_API_URL, headers=headers, json={ "model": MODEL_NAME, "messages": messages, "temperature": temperature }, timeout=30 ) if response.status_code == 200: reply = response.json()["choices"][0]["message"]["content"] disclaimer = "\n\nā ļø **Disclaimer:** This is general legal information only and does not constitute legal advice. Always consult a qualified attorney for specific legal matters." return reply + disclaimer else: error_text = response.text try: error_json = response.json() error_text = error_json.get("error", {}).get("message", error_text) except: pass return f"Error {response.status_code}: {error_text}" except Exception as e: return f"Error connecting to GROQ API: {str(e)}. Please check your API key and internet connection." # Create Gradio interface with modern design with gr.Blocks(title="Legal Advisor Chatbot - LexBot") as demo: # Header Section with gr.Row(): gr.HTML("""
Your AI Legal Advisor - Powered by GROQ
ā ļø Important: This chatbot provides general legal information for educational purposes only.
It does not constitute legal advice. For specific legal matters, please consult a qualified attorney.
Laws vary by jurisdiction, and this information may not apply to your specific situation.