tudeplom commited on
Commit
937cd3c
·
verified ·
1 Parent(s): f6cc1d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -1,17 +1,36 @@
1
- import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # Load chatbot pipeline từ Hugging Face
5
- chatbot = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
6
 
7
- # Hàm xử chat
8
- def chat_with_ai(user_message):
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
- # Giao diện Gradio
14
- iface = gr.Interface(fn=chat_with_ai, inputs="text", outputs="text", title="TinyLlama Chatbot")
 
15
 
16
- # Chạy ứng dụng
17
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)