StevenChen16 commited on
Commit
6226cf7
1 Parent(s): 065d368

Update app_backup.py

Browse files
Files changed (1) hide show
  1. app_backup.py +89 -20
app_backup.py CHANGED
@@ -1,7 +1,42 @@
1
  import gradio as gr
 
 
2
  from llamafactory.chat import ChatModel
3
  from llamafactory.extras.misc import torch_gc
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  args = dict(
6
  model_name_or_path="StevenChen16/llama3-8b-Lawyer",
7
  template="llama3",
@@ -12,33 +47,67 @@ args = dict(
12
  chat_model = ChatModel(args)
13
 
14
  background_prompt = """
15
- 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:
16
 
17
- 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.
18
- 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.
19
- 3. **Contextual Relevance**: Tailor your responses to the specific context of the question asked. Provide examples or analogies where appropriate to illustrate legal concepts.
20
- 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.
21
- 5. **Professional Tone**: Maintain a professional and respectful tone in all responses. Ensure that your advice is legally sound and adheres to ethical standards.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  """
23
 
24
- def query_model(user_input):
25
  combined_query = background_prompt + user_input
26
  messages = [{"role": "user", "content": combined_query}]
27
 
28
  response = ""
29
- for new_text in chat_model.stream_chat(messages):
30
  response += new_text
 
31
 
32
- return response
33
-
34
- # Gradio interface
35
- interface = gr.Interface(
36
- fn=query_model,
37
- inputs="text",
38
- outputs="text",
39
- title="Legal AI Assistant",
40
- description="Ask me any legal question related to US and Canada law.",
41
- )
 
 
 
 
 
 
 
 
42
 
43
- if __name__ == '__main__':
44
- interface.launch()
 
1
  import gradio as gr
2
+ import os
3
+ from threading import Thread
4
  from llamafactory.chat import ChatModel
5
  from llamafactory.extras.misc import torch_gc
6
 
7
+
8
+ DESCRIPTION = '''
9
+ <div>
10
+ <h1 style="text-align: center;">AI Lawyer</h1>
11
+ </div>
12
+ '''
13
+
14
+ LICENSE = """
15
+ <p/>
16
+ ---
17
+ Built with model "StevenChen16/Llama3-8B-Lawyer", based on "meta-llama/Meta-Llama-3-8B"
18
+ """
19
+
20
+ PLACEHOLDER = """
21
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
22
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">AI Lawyer</h1>
23
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything about US and Canada law...</p>
24
+ </div>
25
+ """
26
+
27
+ css = """
28
+ h1 {
29
+ text-align: center;
30
+ display: block;
31
+ }
32
+ #duplicate-button {
33
+ margin: auto;
34
+ color: white;
35
+ background: #1565c0;
36
+ border-radius: 100vh;
37
+ }
38
+ """
39
+
40
  args = dict(
41
  model_name_or_path="StevenChen16/llama3-8b-Lawyer",
42
  template="llama3",
 
47
  chat_model = ChatModel(args)
48
 
49
  background_prompt = """
50
+ As an AI legal assistant, you are a highly trained expert in U.S. and Canadian law. Your purpose is to provide accurate, comprehensive, and professional legal information to assist users with a wide range of legal questions. When answering questions, you should actively ask questions to obtain more information, analyze from different perspectives, and explain your reasoning process to the user. Please adhere to the following guidelines:
51
 
52
+ 1. Clarify the question:
53
+ - Ask questions to clarify the user's specific situation and needs to provide the most relevant and targeted advice.
54
+ - However, if the user has already provided sufficient background information, avoid excessively pressing for details. Focus on understanding the core of the issue, rather than unnecessary minutiae.
55
+
56
+ 2. Gather information:
57
+ - Identify the key information needed to answer the question and proactively ask the user for these details.
58
+ - When gathering information, be sure to identify which details are directly relevant to the legal analysis of the case. For information that is not relevant, you don't need to ask too many follow-up questions.
59
+ - If the user indicates that they have provided all relevant information, accept this and do not continue to demand more details.
60
+
61
+ 3. Multi-perspective analysis:
62
+ - Evaluate legal issues from different viewpoints, considering various possible interpretations and applications.
63
+ - Present arguments supporting and opposing specific perspectives to comprehensively clarify complex issues.
64
+ - In your analysis, strive to balance comprehensiveness and conciseness. Provide thorough analysis, but also ensure that the user can easily understand and absorb the information.
65
+
66
+ 4. Explain reasoning:
67
+ - Explain the main legal principles, regulations, and case law you consider when analyzing the issue.
68
+ - Clarify how you apply legal knowledge to the user's specific situation and the logic behind your conclusions.
69
+ - When explaining your reasoning, use clear and concise language, avoiding excessive length or repetition.
70
+
71
+ 5. Interactive dialogue:
72
+ - Encourage the user to participate in the discussion, ask follow-up questions, and share their thoughts and concerns.
73
+ - Dynamically adjust your analysis and recommendations based on new information obtained in the conversation.
74
+ - In your interactions, be attentive to the user's needs and concerns. If they express satisfaction or indicate that they don't require more information, respect their wishes.
75
+
76
+ 6. Professional advice:
77
+ - Provide clear, actionable legal advice, but also emphasize the necessity of consulting a professional lawyer before making a final decision.
78
+ - If clients wish to speak with a lawyer, you can introduce our team (WealthWizards), which consists of lawyers with different specializations and orientations.
79
+ - When providing advice, use language that is easy to understand and communicate with a tone of empathy and care. Let them feel that you understand their situation and sincerely want to help them.
80
+
81
+ Please remember that your role is to provide general legal information and analysis, but also to actively guide and interact with the user during the conversation in a personalized and professional manner. If you feel that necessary information is missing to provide targeted analysis and advice, take the initiative to ask until you believe you have sufficient details. However, also be mindful to avoid over-inquiring or disregarding the user's needs and concerns. Now, please guide me step by step to describe the legal issues I am facing, according to the above requirements.
82
  """
83
 
84
+ def query_model(user_input, history):
85
  combined_query = background_prompt + user_input
86
  messages = [{"role": "user", "content": combined_query}]
87
 
88
  response = ""
89
+ for new_text in chat_model.stream_chat(messages, temperature=0.9):
90
  response += new_text
91
+ yield response
92
 
93
+ # Gradio block
94
+ chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
95
+
96
+ with gr.Blocks(css=css) as demo:
97
+ gr.Markdown(DESCRIPTION)
98
+ gr.ChatInterface(
99
+ fn=query_model,
100
+ chatbot=chatbot,
101
+ examples=[
102
+ ['What are the key differences between a sole proprietorship and a partnership?'],
103
+ ['What legal steps should I take if I want to start a business in the US?'],
104
+ ['Can you explain the concept of "duty of care" in negligence law?'],
105
+ ['What are the legal requirements for obtaining a patent in Canada?'],
106
+ ['How can I protect my intellectual property when sharing my idea with potential investors?']
107
+ ],
108
+ cache_examples=False,
109
+ )
110
+ gr.Markdown(LICENSE)
111
 
112
+ if __name__ == "__main__":
113
+ demo.launch(share=True)