Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
prompt = f"User: {user_message}\nAssistant:"
|
| 10 |
-
response = chatbot(prompt, max_length=200, do_sample=True, temperature=0.7)
|
| 11 |
-
return response[0]["generated_text"].split("Assistant:")[-1].strip()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
CORS(app)
|
| 8 |
|
| 9 |
+
# Đường dẫn tới model GGUF
|
| 10 |
+
MODEL_PATH = "models/mistral-7b-instruct-v0.2.Q4_K_M.gguf"
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Kiểm tra model có tồn tại không
|
| 13 |
+
if not os.path.exists(MODEL_PATH):
|
| 14 |
+
raise FileNotFoundError(f"Không tìm thấy model tại {MODEL_PATH}")
|
| 15 |
|
| 16 |
+
# Load model GGUF
|
| 17 |
+
llm = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=8, n_batch=512)
|
| 18 |
+
|
| 19 |
+
# Xử lý chat
|
| 20 |
+
def get_answer(user_text):
|
| 21 |
+
response = llm(f"User: {user_text}\nAssistant:", max_tokens=150, temperature=0.7)
|
| 22 |
+
return response["choices"][0]["text"]
|
| 23 |
+
|
| 24 |
+
@app.route('/')
|
| 25 |
+
def index():
|
| 26 |
+
return render_template('index.html')
|
| 27 |
+
|
| 28 |
+
@app.route('/chat', methods=['POST'])
|
| 29 |
+
def chat():
|
| 30 |
+
data = request.json
|
| 31 |
+
user_message = data.get('message', '')
|
| 32 |
+
bot_reply = get_answer(user_message)
|
| 33 |
+
return jsonify({'FROM': 'AI Assistant', 'MESSAGE': bot_reply})
|
| 34 |
+
|
| 35 |
+
if __name__ == '__main__':
|
| 36 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|