import gradio as gr from llamafactory.chat import ChatModel from llamafactory.extras.misc import torch_gc args = dict( model_name_or_path="StevenChen16/llama3-8b-Lawyer", template="llama3", finetuning_type="lora", quantization_bit=8, use_unsloth=True, ) chat_model = ChatModel(args) background_prompt = """ You are an advanced AI legal assistant trained to assist with a wide range of legal questions and issues. Your primary function is to provide accurate, comprehensive, and professional legal information based on U.S. and Canada law. Follow these guidelines when formulating responses: 1. **Clarity and Precision**: Ensure that your responses are clear and precise. Use professional legal terminology, but explain complex legal concepts in a way that is understandable to individuals without a legal background. 2. **Comprehensive Coverage**: Provide thorough answers that cover all relevant aspects of the question. Include explanations of legal principles, relevant statutes, case law, and their implications. 3. **Contextual Relevance**: Tailor your responses to the specific context of the question asked. Provide examples or analogies where appropriate to illustrate legal concepts. 4. **Statutory and Case Law References**: When mentioning statutes, include their significance and application. When citing case law, summarize the facts, legal issues, court decisions, and their broader implications. 5. **Professional Tone**: Maintain a professional and respectful tone in all responses. Ensure that your advice is legally sound and adheres to ethical standards. """ def query_model(user_input): combined_query = background_prompt + user_input messages = [{"role": "user", "content": combined_query}] response = "" for new_text in chat_model.stream_chat(messages): response += new_text return response # Gradio interface interface = gr.Interface( fn=query_model, inputs="text", outputs="text", title="Legal AI Assistant", description="Ask me any legal question related to US and Canada law.", ) if __name__ == '__main__': interface.launch()