tudeplom commited on
Commit
1f42217
·
verified ·
1 Parent(s): 1d55e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,25 +1,24 @@
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():
@@ -28,9 +27,9 @@ def index():
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)
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import requests
3
  from flask_cors import CORS
 
 
4
 
5
  app = Flask(__name__)
6
  CORS(app)
7
 
8
+ # Model Hugging Face (Không cần tải về)
9
+ HF_MODEL = "google/gemma-2b"
10
+ HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
11
+
12
+ # Gửi request API để chat
13
+ def chat_with_gemma(prompt):
14
+ response = requests.post(
15
+ HF_API_URL,
16
+ headers={"Authorization": f"Bearer YOUR_HF_API_KEY"}, # Thay YOUR_HF_API_KEY bằng API Key của bạn
17
+ json={"inputs": prompt}
18
+ )
19
+ if response.status_code == 200:
20
+ return response.json()[0]['generated_text']
21
+ return "Lỗi: Không thể lấy phản hồi từ API."
22
 
23
  @app.route('/')
24
  def index():
 
27
  @app.route('/chat', methods=['POST'])
28
  def chat():
29
  data = request.json
30
+ user_message = data.get("message", "")
31
+ bot_reply = chat_with_gemma(user_message)
32
+ return jsonify({'FROM': 'Gemma-2B', 'MESSAGE': bot_reply})
33
 
34
  if __name__ == '__main__':
35
+ app.run(host="0.0.0.0", port=7860)