File size: 7,483 Bytes
f9bd583
 
 
c32213b
e713c1e
c32213b
e713c1e
 
 
 
3a164ca
 
 
 
e713c1e
3a164ca
 
 
e713c1e
 
 
 
c32213b
 
 
e713c1e
eda394d
 
 
 
 
e713c1e
 
 
 
3a164ca
e713c1e
 
 
 
 
 
 
 
 
3a164ca
eda394d
 
 
 
25240f0
eda394d
 
3a164ca
eda394d
 
 
 
e713c1e
 
3a164ca
eda394d
3a164ca
c32213b
41e34df
c32213b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e34df
c32213b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a164ca
d7ffcb9
 
3a164ca
 
e713c1e
3a164ca
e713c1e
 
 
 
c32213b
 
 
 
3a164ca
e713c1e
 
 
 
 
 
 
 
c32213b
d7ffcb9
 
c6108ea
 
 
12ee5aa
c6108ea
 
 
 
 
 
 
 
c32213b
 
c6108ea
c32213b
 
c6108ea
 
 
 
773e819
 
c6108ea
 
 
 
 
 
 
773e819
 
 
 
 
 
 
 
 
 
 
 
c32213b
41e34df
3a164ca
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import streamlit as st
import requests

# -------------------------------
# 1. ๋ฌธ์„œ ํŒŒ์‹ฑ ํ•จ์ˆ˜ (HTML ์ถ”์ถœ)
# -------------------------------
def parse_document_to_html(file, api_key):
    url = "https://api.upstage.ai/v1/document-ai/document-parse"
    headers = {'Authorization': f'Bearer {api_key}'}
    files = {"document": file}
    data = {
        "base64_encoding": "['table']",
        "model": "document-parse"
    }

    response = requests.post(url, headers=headers, files=files, data=data)
    if response.status_code != 200:
        return None, f"๋ฌธ์„œ ํŒŒ์‹ฑ ์‹คํŒจ (status code: {response.status_code})"
    
    html_text = response.json().get("content", {}).get("html", "")
    return html_text, None

# -------------------------------
# 2. Solar-Pro: ์งˆ๋ฌธ ์‘๋‹ต
# -------------------------------
def chat_with_solar_document(html_text, user_question, history, api_key):
    url = "https://api.upstage.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    system_prompt = f"""
    ๋‹ค์Œ์€ HTML ํ˜•์‹์œผ๋กœ ์ถ”์ถœ๋œ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.
    HTML ์•ˆ์˜ ๋‚ด์šฉ๋งŒ ๊ทผ๊ฑฐ๋กœ ์‚ผ์•„ ํ•™์ƒ์˜ ์งˆ๋ฌธ์— ์ •ํ™•ํ•˜๊ณ  ์‰ฝ๊ฒŒ ๋‹ตํ•ด์ฃผ์„ธ์š”.

    ๋ฌธ์„œ:
    {html_text}
    """

    messages = [{"role": "system", "content": system_prompt}]
    for user, bot in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})
    messages.append({"role": "user", "content": user_question})

    payload = {
        "model": "solar-pro",
        "messages": messages,
        "temperature": 0,
        "max_tokens": 2048
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        if response.status_code != 200:
            return None, history, f"์ƒํƒœ ์ฝ”๋“œ: {response.status_code}"
        bot_reply = response.json()["choices"][0]["message"]["content"]
        history.append((user_question, bot_reply))
        return bot_reply, history, None
    except Exception as e:
        return None, history, f"์˜ˆ์™ธ ๋ฐœ์ƒ: {str(e)}"

# -------------------------------
# 3. ์ถ”์ฒœ ํ‚ค์›Œ๋“œ ์ƒ์„ฑ
# -------------------------------
def get_recommended_keywords(html_text, user_question, api_key):
    url = "https://api.upstage.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    prompt = f"""
    ๋‹ค์Œ์€ HTML๋กœ ์ถ”์ถœ๋œ ๋ฌธ์ œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
    ๋ฌธ์ œ์™€ ์งˆ๋ฌธ์„ ๋ฐ”ํƒ•์œผ๋กœ ํ•™์ƒ์—๊ฒŒ ๋„์›€์ด ๋  ๋งŒํ•œ ๊ณต๋ถ€ ํ‚ค์›Œ๋“œ 3๊ฐ€์ง€๋ฅผ ์ œ์•ˆํ•ด์ฃผ์„ธ์š”. ํ‚ค์›Œ๋“œ๋งŒ ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ์„ธ์š”.

    ๋ฌธ์„œ: {html_text}

    ํ•™์ƒ ์งˆ๋ฌธ: {user_question}
    """

    payload = {
        "model": "solar-pro",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.5,
        "max_tokens": 100
    }

    try:
        res = requests.post(url, headers=headers, json=payload)
        if res.status_code != 200:
            return []
        raw_text = res.json()["choices"][0]["message"]["content"]
        keywords = [kw.strip() for kw in raw_text.split(",") if kw.strip()]
        return keywords[:3]
    except:
        return []

# -------------------------------
# 4. ํ‚ค์›Œ๋“œ๋ณ„ ๊ณต๋ถ€ ํŒ ์ƒ์„ฑ
# -------------------------------
def get_study_tip_for_keyword(keyword, api_key):
    url = "https://api.upstage.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    prompt = f"""
    ํ•™์ƒ์ด '{keyword}'์„(๋ฅผ) ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์–ดํ•ฉ๋‹ˆ๋‹ค.
    ๊ฐœ๋… ์ดํ•ด, ๊ณต๋ถ€ ๋ฐฉ๋ฒ•, ์ถ”์ฒœ ์ˆœ์„œ ๋“ฑ์„ ํฌํ•จํ•ด ์นœ์ ˆํ•˜๊ฒŒ ํ•™์Šต ํŒ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.
    """

    payload = {
        "model": "solar-pro",
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": 300
    }

    try:
        res = requests.post(url, headers=headers, json=payload)
        return res.json()["choices"][0]["message"]["content"]
    except:
        return "โš ๏ธ ํ•™์Šต ์ถ”์ฒœ ์ƒ์„ฑ์— ์‹คํŒจํ–ˆ์–ด์š”."

# -------------------------------
# 5. Streamlit UI
# -------------------------------
def main():
    st.set_page_config(page_title="Solar ๊ณต๋ถ€ ๋ฉ˜ํ† ", layout="wide")
    st.title("๐Ÿ‘ฉโ€๐Ÿซ Solar ๊ณต๋ถ€ ๋ฉ˜ํ† ")

    api_key = st.text_input("๐Ÿ” Upstage API Key", type="password")
    uploaded_file = st.file_uploader("๐Ÿ“Ž ๋ฌธ์ œ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ", type=["pdf", "png", "jpg", "jpeg"])

    if "html_text" not in st.session_state:
        st.session_state.html_text = ""
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = []
    if "keywords" not in st.session_state:
        st.session_state.keywords = []
    if "study_tips" not in st.session_state:
        st.session_state.study_tips = {}

    if uploaded_file and api_key and st.button("๐Ÿ“„ ๋ฌธ์„œ ๋ถ„์„ ์‹œ์ž‘"):
        with st.spinner("๋ฌธ์„œ๋ฅผ ํŒŒ์‹ฑํ•˜๊ณ  ์žˆ์–ด์š”..."):
            html_text, err = parse_document_to_html(uploaded_file, api_key)
            if err:
                st.error(err)
                return
            st.session_state.html_text = html_text
            st.success("๋ฌธ์„œ ํŒŒ์‹ฑ ์„ฑ๊ณต โœ…")

    # โœจ ์งˆ์˜์‘๋‹ต: ์ด๋ฏธ์ง€์™€ ๋ถ„๋ฆฌํ•ด์„œ ๋ณ„๋„ ์˜์—ญ์— ์œ„์น˜
    if st.session_state.html_text:
        left_col, right_col = st.columns([1, 2])

        with left_col:
            st.image(uploaded_file, caption="๐Ÿ–ผ๏ธ ์—…๋กœ๋“œํ•œ ๋ฌธ์ œ ์ด๋ฏธ์ง€", use_container_width=True)

        with right_col:
            st.subheader("โ“ ๊ถ๊ธˆํ•œ ๋‚ด์šฉ์„ ์งˆ๋ฌธํ•ด๋ณด์„ธ์š”")
            user_question = st.text_input("์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜๊ณ  ์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”", key="question_input")

            if st.button("๐Ÿ’ฌ Solar์—๊ฒŒ ๋ฌผ์–ด๋ณด๊ธฐ") and user_question:
                with st.spinner("Solar ์„ ์ƒ๋‹˜์ด ๋‹ต๋ณ€ ์ค‘์ž…๋‹ˆ๋‹ค..."):
                    answer, history, err = chat_with_solar_document(
                        st.session_state.html_text,
                        user_question,
                        st.session_state.chat_history,
                        api_key
                    )
                    if err:
                        st.error(f"โŒ Solar ์‘๋‹ต ์˜ค๋ฅ˜: {err}")
                    else:
                        st.session_state.chat_history = history
                        st.chat_message("user").write(user_question)
                        st.chat_message("assistant").write(answer)

                        # ํ‚ค์›Œ๋“œ ์ถ”์ฒœ ์ƒ์„ฑ
                        st.session_state.keywords = get_recommended_keywords(
                            st.session_state.html_text,
                            user_question,
                            api_key
                        )

    # โœจ ์ด๋ฏธ์ง€ ์•„๋ž˜์— ์ถ”์ฒœ ํ‚ค์›Œ๋“œ ํ‘œ์‹œ
    if st.session_state.keywords:
        st.markdown("### ๐Ÿ”Ž ์ถ”์ฒœ ๊ณต๋ถ€ ํ‚ค์›Œ๋“œ")
        for keyword in st.session_state.keywords:
            if st.button(f"๐Ÿ“˜ {keyword} ๊ณต๋ถ€ ์ถ”์ฒœ๋ฐ›๊ธฐ", key=f"btn_{keyword}"):
                tip = get_study_tip_for_keyword(keyword, api_key)
                st.session_state.study_tips[keyword] = tip

        for keyword, tip in st.session_state.study_tips.items():
            with st.expander(f"๐Ÿ“˜ {keyword} ๊ณต๋ถ€๋ฒ• ๋ณด๊ธฐ"):
                st.write(tip)


if __name__ == "__main__":
    main()