Spaces:
Running
Running
File size: 2,060 Bytes
cfb9fa3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # 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})
|