StevenChen16 commited on
Commit
bf7460e
1 Parent(s): f973ae6

update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -34
app.py CHANGED
@@ -1,17 +1,23 @@
1
- import gradio as gr
2
- import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
4
 
5
- # 加载模型和tokenizer
6
- model_name = "StevenChen16/llama3-8b-Lawyer"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
- # 检查是否有可用的GPU
10
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
- model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
 
 
 
 
 
12
 
13
  background_prompt = """
14
  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:
 
15
  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.
16
  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.
17
  3. **Contextual Relevance**: Tailor your responses to the specific context of the question asked. Provide examples or analogies where appropriate to illustrate legal concepts.
@@ -19,29 +25,20 @@ You are an advanced AI legal assistant trained to assist with a wide range of le
19
  5. **Professional Tone**: Maintain a professional and respectful tone in all responses. Ensure that your advice is legally sound and adheres to ethical standards.
20
  """
21
 
22
- def generate_response(input_text):
23
- # 将输入文本与背景提示词结合
24
- prompt = f"{background_prompt}\n\nUser: {input_text}\nAssistant: "
25
-
26
- # Tokenize 输入文本
27
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
28
-
29
- # 生成输出
30
- outputs = model.generate(**inputs, max_new_tokens=300)
31
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
-
33
- # 返回生成的响应
34
- return response
35
-
36
- # 创建 Gradio 接口
37
- iface = gr.Interface(
38
- fn=generate_response,
39
- inputs="text",
40
- outputs="text",
41
- title="Legal Assistant",
42
- description="Enter your legal question and get a response from the model.",
43
- )
44
 
45
- # 运行应用
46
- if __name__ == "__main__":
47
- iface.launch()
 
1
+ from flask import Flask, request, jsonify, Response
2
+ from flask_cors import CORS
3
+ from llamafactory.chat import ChatModel
4
+ from llamafactory.extras.misc import torch_gc
5
 
6
+ app = Flask(__name__)
7
+ CORS(app)
 
8
 
9
+ args = dict(
10
+ model_name_or_path="StevenChen16/llama3-8b-Lawyer",
11
+ template="llama3",
12
+ finetuning_type="lora",
13
+ quantization_bit=8,
14
+ use_unsloth=True,
15
+ )
16
+ chat_model = ChatModel(args)
17
 
18
  background_prompt = """
19
  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:
20
+
21
  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.
22
  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.
23
  3. **Contextual Relevance**: Tailor your responses to the specific context of the question asked. Provide examples or analogies where appropriate to illustrate legal concepts.
 
25
  5. **Professional Tone**: Maintain a professional and respectful tone in all responses. Ensure that your advice is legally sound and adheres to ethical standards.
26
  """
27
 
28
+ @app.route('/query', methods=['POST'])
29
+ def query_model():
30
+ user_input = request.json.get('prompt', '')
31
+ combined_query = background_prompt + user_input
32
+ messages = [{"role": "user", "content": combined_query}]
33
+
34
+ def generate():
35
+ response = ""
36
+ for new_text in chat_model.stream_chat(messages):
37
+ response += new_text
38
+ yield f"data:{new_text}\n\n"
39
+ messages.append({"role": "assistant", "content": response})
40
+
41
+ return Response(generate(), mimetype='text/event-stream')
 
 
 
 
 
 
 
 
42
 
43
+ if __name__ == '__main__':
44
+ app.run(host='0.0.0.0', port=5000)