Spaces:
Running
on
Zero
Running
on
Zero
StevenChen16
commited on
Commit
•
bf7460e
1
Parent(s):
f973ae6
update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,23 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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 |
-
|
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)
|
|