Spaces:
Running
Running
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()
|