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})