Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import logging
|
|
@@ -10,8 +10,8 @@ logging.basicConfig(level=logging.INFO)
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
-
# Model Hugging Face
|
| 14 |
-
HF_MODEL = "
|
| 15 |
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 16 |
|
| 17 |
# Lấy API Key từ biến môi trường
|
|
@@ -20,37 +20,41 @@ if not HF_API_KEY:
|
|
| 20 |
raise ValueError("Lỗi: API Key không tồn tại. Hãy thêm vào Hugging Face Secrets.")
|
| 21 |
|
| 22 |
# Hàm gửi request đến API Hugging Face
|
| 23 |
-
def
|
| 24 |
logging.info(f"Gửi request đến API: {HF_API_URL}")
|
| 25 |
logging.info(f"Nội dung gửi: {prompt}")
|
| 26 |
|
| 27 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 28 |
-
payload = {"inputs": prompt}
|
| 29 |
-
|
| 30 |
try:
|
| 31 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
| 32 |
logging.info(f"Trạng thái API: {response.status_code}")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
if response.status_code == 200:
|
| 36 |
result = response.json()
|
| 37 |
-
return result[0].get("generated_text", "Không có phản hồi.")
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except requests.exceptions.RequestException as e:
|
| 41 |
-
|
| 42 |
-
return "Lỗi: Không thể kết nối đến API."
|
| 43 |
-
|
| 44 |
-
@app.route('/')
|
| 45 |
-
def index():
|
| 46 |
-
return render_template('index.html')
|
| 47 |
|
|
|
|
| 48 |
@app.route('/chat', methods=['POST'])
|
| 49 |
def chat():
|
| 50 |
data = request.json
|
| 51 |
user_message = data.get("message", "")
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
| 55 |
if __name__ == '__main__':
|
| 56 |
-
app.run(host="0.0.0.0", port=
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import logging
|
|
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
+
# Model TinyLlama trên Hugging Face
|
| 14 |
+
HF_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 15 |
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
|
| 16 |
|
| 17 |
# Lấy API Key từ biến môi trường
|
|
|
|
| 20 |
raise ValueError("Lỗi: API Key không tồn tại. Hãy thêm vào Hugging Face Secrets.")
|
| 21 |
|
| 22 |
# Hàm gửi request đến API Hugging Face
|
| 23 |
+
def chat_with_tinyllama(prompt):
|
| 24 |
logging.info(f"Gửi request đến API: {HF_API_URL}")
|
| 25 |
logging.info(f"Nội dung gửi: {prompt}")
|
| 26 |
|
| 27 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 28 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 150}}
|
| 29 |
+
|
| 30 |
try:
|
| 31 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60)
|
| 32 |
logging.info(f"Trạng thái API: {response.status_code}")
|
| 33 |
+
|
|
|
|
| 34 |
if response.status_code == 200:
|
| 35 |
result = response.json()
|
| 36 |
+
return result[0].get("generated_text", "Không có phản hồi từ mô hình.")
|
| 37 |
+
elif response.status_code == 503:
|
| 38 |
+
return "Mô hình đang quá tải, vui lòng thử lại sau."
|
| 39 |
+
else:
|
| 40 |
+
return f"Lỗi API: {response.status_code} - {response.text}"
|
| 41 |
+
|
| 42 |
+
except requests.exceptions.Timeout:
|
| 43 |
+
return "Lỗi: API phản hồi quá lâu. Hãy thử lại sau."
|
| 44 |
except requests.exceptions.RequestException as e:
|
| 45 |
+
return f"Lỗi mạng: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Route API chat
|
| 48 |
@app.route('/chat', methods=['POST'])
|
| 49 |
def chat():
|
| 50 |
data = request.json
|
| 51 |
user_message = data.get("message", "")
|
| 52 |
+
if not user_message:
|
| 53 |
+
return jsonify({'FROM': 'TinyLlama', 'MESSAGE': 'Lỗi: Tin nhắn không hợp lệ.'})
|
| 54 |
+
|
| 55 |
+
bot_reply = chat_with_tinyllama(user_message)
|
| 56 |
+
return jsonify({'FROM': 'TinyLlama', 'MESSAGE': bot_reply})
|
| 57 |
|
| 58 |
+
# Chạy Flask server
|
| 59 |
if __name__ == '__main__':
|
| 60 |
+
app.run(host="0.0.0.0", port=7860)
|