knukdt_chatbot / src /app.py
joonion's picture
Initial Chatbot
16f0ee0
Raw
History Blame Contribute Delete
2.06 kB
# app.py
import streamlit as st
import requests
API_URL = "http://localhost:8000"
st.title("RAG ์ฑ—๋ด‡ ์„œ๋น„์Šค")
if "messages" not in st.session_state:
st.session_state.messages = []
with st.sidebar:
st.header("์ง€์‹ ๊ด€๋ฆฌ")
upload_file = st.file_uploader("PDF ์—…๋กœ๋“œ", type=['pdf'])
if st.button("๋ฒกํ„ฐ DB์— ์ถ”๊ฐ€"):
if upload_file is None:
st.warning("๋จผ์ € PDF ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”.")
else:
with st.spinner("PDF๋ฅผ ๋ฒกํ„ฐ DB์— ์ถ”๊ฐ€ ์ค‘์ž…๋‹ˆ๋‹ค..."):
files = {"file": (upload_file.name, upload_file.getvalue(), "application/pdf")}
response = requests.post(f"{API_URL}/upload", files=files)
if response.status_code == 200:
result = response.json()
st.success("PDF๊ฐ€ ๋ฒกํ„ฐ DB์— ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
st.write(f"ํŒŒ์ผ๋ช…: {result['filename']}")
else:
st.error("PDF ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.")
st.subheader("๋ฌป๊ณ  ๋‹ตํ•˜๊ธฐ")
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
if question := st.chat_input("์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”."):
with st.chat_message("user"):
st.markdown(question)
st.session_state.messages.append({"role": "user", "content": question})
response = requests.post(f"{API_URL}/ask", data={"question": question})
if response.status_code == 200:
answer = response.json()['answer']
context = response.json()['context']
else:
answer = "๋‹ต๋ณ€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค."
with st.chat_message("assistant"):
st.markdown(answer)
if response.status_code == 200:
with st.expander("์ปจํ…์ŠคํŠธ ์ •๋ณด ํ™•์ธ"):
st.markdown(f"**์ปจํ…์ŠคํŠธ ์ •๋ณด**")
st.markdown(context)
st.session_state.messages.append({"role": "assistant", "content": answer})