Spaces:
Sleeping
Sleeping
| # 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}) | |