Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,55 @@ url_api_retrieval_model = f"{retrieval_module}/search"
|
|
| 28 |
url_api_reranker_model = f"{reranker_module}/rerank"
|
| 29 |
url_api_generation_model = f"{abs_QA_module}/answer"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
with open("./static/styles.css") as f:
|
| 32 |
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 33 |
|
|
|
|
| 28 |
url_api_reranker_model = f"{reranker_module}/rerank"
|
| 29 |
url_api_generation_model = f"{abs_QA_module}/answer"
|
| 30 |
|
| 31 |
+
# ========= FLASK APP ===============
|
| 32 |
+
flask_app = Flask(__name__)
|
| 33 |
+
|
| 34 |
+
# Dùng để lưu tin nhắn nhận được trong session
|
| 35 |
+
if "messages" not in st.session_state:
|
| 36 |
+
st.session_state.messages = []
|
| 37 |
+
|
| 38 |
+
# Gửi tin nhắn tới người dùng
|
| 39 |
+
def send_message(recipient_id, message):
|
| 40 |
+
url = f"{FB_API_URL}/me/messages?access_token={FB_PAGE_ACCESS_TOKEN}"
|
| 41 |
+
payload = {
|
| 42 |
+
"recipient": {"id": recipient_id},
|
| 43 |
+
"message": {"text": message}
|
| 44 |
+
}
|
| 45 |
+
response = requests.post(url, json=payload)
|
| 46 |
+
return response.ok
|
| 47 |
+
|
| 48 |
+
# Xử lý GET và POST từ Facebook Webhook
|
| 49 |
+
@flask_app.route("/webhook", methods=["GET", "POST"])
|
| 50 |
+
def webhook():
|
| 51 |
+
if request.method == "GET":
|
| 52 |
+
if request.args.get("hub.verify_token") == FB_VERIFY_TOKEN:
|
| 53 |
+
return request.args.get("hub.challenge")
|
| 54 |
+
return "Verification token mismatch", 403
|
| 55 |
+
|
| 56 |
+
if request.method == "POST":
|
| 57 |
+
data = request.get_json()
|
| 58 |
+
if "entry" in data:
|
| 59 |
+
for entry in data["entry"]:
|
| 60 |
+
for event in entry["messaging"]:
|
| 61 |
+
sender_id = event["sender"]["id"]
|
| 62 |
+
message_text = event.get("message", {}).get("text", "")
|
| 63 |
+
if message_text:
|
| 64 |
+
# Lưu vào session_state
|
| 65 |
+
st.session_state.messages.append(
|
| 66 |
+
{"sender_id": sender_id, "text": message_text}
|
| 67 |
+
)
|
| 68 |
+
# Gửi trả lời mặc định
|
| 69 |
+
send_message(sender_id, "Cảm ơn bạn đã nhắn tin!")
|
| 70 |
+
return "OK", 200
|
| 71 |
+
|
| 72 |
+
# Chạy Flask trong luồng riêng
|
| 73 |
+
def run_flask():
|
| 74 |
+
flask_app.run(host="0.0.0.0", port=5000)
|
| 75 |
+
|
| 76 |
+
threading.Thread(target=run_flask, daemon=True).start()
|
| 77 |
+
|
| 78 |
+
# ========== STREAMLIT UI ==========
|
| 79 |
+
|
| 80 |
with open("./static/styles.css") as f:
|
| 81 |
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 82 |
|