Spaces:
Runtime error
Runtime error
from flask import Flask, request, jsonify, Response | |
from flask_cors import CORS | |
from llamafactory.chat import ChatModel | |
from llamafactory.extras.misc import torch_gc | |
app = Flask(__name__) | |
CORS(app) | |
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 = request.json.get('prompt', '') | |
combined_query = background_prompt + user_input | |
messages = [{"role": "user", "content": combined_query}] | |
def generate(): | |
response = "" | |
for new_text in chat_model.stream_chat(messages): | |
response += new_text | |
yield f"data:{new_text}\n\n" | |
messages.append({"role": "assistant", "content": response}) | |
return Response(generate(), mimetype='text/event-stream') | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=5000) | |