Test / app.py
UKielz's picture
Upload 14 files
0bbe8e9 verified
import streamlit as st
import requests
API_URL = "http://localhost:8000/chat" # FastAPI server của bạn
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"] = []
# Hiển thị lịch sử chat
for msg in st.session_state["messages"]:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Input từ người dùng
if user_input := st.chat_input("Nhập tin nhắn..."):
# Hiển thị ngay trên UI
st.session_state["messages"].append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
# Gửi request đến FastAPI
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}"
# Hiển thị phản hồi bot
st.session_state["messages"].append({"role": "assistant", "content": bot_reply})
with st.chat_message("assistant"):
st.markdown(bot_reply)