|
|
import streamlit as st
|
|
|
import requests
|
|
|
|
|
|
API_URL = "http://localhost:8000/chat"
|
|
|
|
|
|
st.set_page_config(page_title="Coca-Cola Chatbot", page_icon="🥤", layout="wide")
|
|
|
st.title("🥤 Coca-Cola Vietnam Chatbot")
|
|
|
|
|
|
if "messages" not in st.session_state:
|
|
|
st.session_state["messages"] = []
|
|
|
|
|
|
|
|
|
for msg in st.session_state["messages"]:
|
|
|
with st.chat_message(msg["role"]):
|
|
|
st.markdown(msg["content"])
|
|
|
|
|
|
|
|
|
if user_input := st.chat_input("Nhập tin nhắn..."):
|
|
|
|
|
|
st.session_state["messages"].append({"role": "user", "content": user_input})
|
|
|
with st.chat_message("user"):
|
|
|
st.markdown(user_input)
|
|
|
|
|
|
|
|
|
try:
|
|
|
response = requests.post(API_URL, json={"message": user_input}, params={"session_id": "default"}, timeout=60)
|
|
|
if response.status_code == 200:
|
|
|
data = response.json()
|
|
|
bot_reply = data.get("response", "⚠️ Không có phản hồi")
|
|
|
else:
|
|
|
bot_reply = f"❌ Lỗi API: {response.status_code}"
|
|
|
|
|
|
except Exception as e:
|
|
|
bot_reply = f"⚠️ Không kết nối được API: {e}"
|
|
|
|
|
|
|
|
|
st.session_state["messages"].append({"role": "assistant", "content": bot_reply})
|
|
|
with st.chat_message("assistant"):
|
|
|
st.markdown(bot_reply)
|
|
|
|