File size: 2,426 Bytes
ba9fb68
37836da
ba9fb68
 
 
 
 
37836da
 
 
 
 
 
 
 
 
 
 
 
 
 
ba9fb68
 
37836da
 
 
 
 
 
 
 
ba9fb68
37836da
 
ba9fb68
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# 加载模型和tokenizer
model_name = "StevenChen16/llama3-8b-Lawyer"
tokenizer = AutoTokenizer.from_pretrained(model_name)

# 检查是否有可用的GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)

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 generate_response(input_text):
    # 将输入文本与背景提示词结合
    prompt = f"{background_prompt}\n\nUser: {input_text}\nAssistant: "
    
    # Tokenize 输入文本
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    # 生成输出
    outputs = model.generate(**inputs, max_new_tokens=300)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # 返回生成的响应
    return response

# 创建 Gradio 接口
iface = gr.Interface(
    fn=generate_response,
    inputs="text",
    outputs="text",
    title="Legal Assistant",
    description="Enter your legal question and get a response from the model.",
)

# 运行应用
if __name__ == "__main__":
    iface.launch()