AI-Lawyer / app.py
StevenChen16's picture
share=True
d7c1a74 verified
raw
history blame
4.06 kB
import gradio as gr
import os
from threading import Thread
from llamafactory.chat import ChatModel
from llamafactory.extras.misc import torch_gc
# Set an environment variable
HF_TOKEN = os.environ.get("HF_TOKEN", None)
DESCRIPTION = '''
<div>
<h1 style="text-align: center;">StevenChen16 Llama3 8B Lawyer</h1>
<p>This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/StevenChen16/llama3-8b-Lawyer"><b>StevenChen16 Llama3 8B Lawyer</b></a>. This model is fine-tuned to provide legal advice based on US and Canada law.</p>
<p>Feel free to play with it, or duplicate to run privately!</p>
</div>
'''
LICENSE = """
<p/>
---
Built with StevenChen16 Llama3 8B Lawyer
"""
PLACEHOLDER = """
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">StevenChen16 Llama3 8B Lawyer</h1>
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything about US and Canada law...</p>
</div>
"""
css = """
h1 {
text-align: center;
display: block;
}
#duplicate-button {
margin: auto;
color: white;
background: #1565c0;
border-radius: 100vh;
}
"""
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, history, temperature, max_new_tokens):
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
yield response
# Gradio block
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
with gr.Blocks(css=css) as demo:
gr.Markdown(DESCRIPTION)
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
gr.ChatInterface(
fn=query_model,
chatbot=chatbot,
additional_inputs=[
gr.Slider(minimum=0, maximum=1, step=0.1, value=0.95, label="Temperature"),
gr.Slider(minimum=128, maximum=4096, step=1, value=512, label="Max new tokens"),
],
examples=[
['How to setup a human base on Mars? Give short answer.'],
['Explain theory of relativity to me like I’m 8 years old.'],
['What is 9,000 * 9,000?'],
['Write a pun-filled happy birthday message to my friend Alex.'],
['Justify why a penguin might make a good king of the jungle.']
],
cache_examples=False,
)
gr.Markdown(LICENSE)
if __name__ == "__main__":
demo.launch(share=True)