Add application file
Browse files
app.py
CHANGED
|
@@ -1,188 +1,75 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
-
import tempfile
|
| 4 |
import os
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
# ํ๊ฒฝ ๋ณ์
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
st.
|
| 16 |
-
page_title="RAG ์ฑ๋ด",
|
| 17 |
-
page_icon="๐ค",
|
| 18 |
-
layout="wide"
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
if 'messages' not in st.session_state:
|
| 29 |
-
st.session_state.messages = []
|
| 30 |
-
if 'sources' not in st.session_state:
|
| 31 |
-
st.session_state.sources = []
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"
|
| 41 |
-
|
| 42 |
-
index=0
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
top_k = st.slider(
|
| 46 |
-
"์ฐธ๊ณ ๋ฌธ์ ์ (Top-K)",
|
| 47 |
-
min_value=1,
|
| 48 |
-
max_value=10,
|
| 49 |
-
value=3
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
temperature = st.slider(
|
| 53 |
-
"๋ค์์ฑ (Temperature)",
|
| 54 |
-
min_value=0.0,
|
| 55 |
-
max_value=1.0,
|
| 56 |
-
value=0.7,
|
| 57 |
-
step=0.1
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
st.divider()
|
| 61 |
-
|
| 62 |
-
st.subheader("๐ ์ฐธ๊ณ ๋ฌธ์")
|
| 63 |
-
if st.session_state.sources:
|
| 64 |
-
for i, source in enumerate(st.session_state.sources):
|
| 65 |
-
with st.expander(f"{i+1}. {source.get('metadata', {}).get('category', '์ผ๋ฐ')}"):
|
| 66 |
-
st.write(source.get('content', ''))
|
| 67 |
-
else:
|
| 68 |
-
st.info("์ฐธ๊ณ ๋ฌธ์๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค")
|
| 69 |
-
|
| 70 |
-
# ๋ฉ์ธ ์์ญ
|
| 71 |
-
st.title("๐ฌ RAG ์ฑ๋ด")
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if query:
|
| 85 |
-
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 86 |
-
st.session_state.messages.append({"role": "user", "content": query})
|
| 87 |
-
with st.chat_message("user"):
|
| 88 |
-
st.write(query)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"temperature": temperature
|
| 102 |
-
},
|
| 103 |
-
timeout=30
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
if response.status_code == 200:
|
| 107 |
-
result = response.json()
|
| 108 |
-
answer = result.get("answer", "")
|
| 109 |
-
context_docs = result.get("context_docs", [])
|
| 110 |
-
|
| 111 |
-
# ์๋ต ํ์
|
| 112 |
-
st.markdown(answer)
|
| 113 |
-
|
| 114 |
-
# ์ธ์
์ํ ์
๋ฐ์ดํธ
|
| 115 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 116 |
-
st.session_state.sources = context_docs
|
| 117 |
-
else:
|
| 118 |
-
st.error(f"API ์ค๋ฅ: {response.status_code} - {response.text}")
|
| 119 |
-
except Exception as e:
|
| 120 |
-
st.error(f"์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 121 |
-
|
| 122 |
-
# ์์ฑ ์
๋ ฅ ์ฒ๋ฆฌ
|
| 123 |
-
if audio_input:
|
| 124 |
-
with st.spinner("์์ฑ ์ฒ๋ฆฌ ์ค..."):
|
| 125 |
-
try:
|
| 126 |
-
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 127 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp:
|
| 128 |
-
tmp.write(audio_input.getvalue())
|
| 129 |
-
tmp_path = tmp.name
|
| 130 |
-
|
| 131 |
-
# ํ์ผ ์ฝ๊ธฐ
|
| 132 |
-
with open(tmp_path, 'rb') as f:
|
| 133 |
-
audio_bytes = f.read()
|
| 134 |
-
|
| 135 |
-
# ์์ ํ์ผ ์ญ์
|
| 136 |
-
os.unlink(tmp_path)
|
| 137 |
-
|
| 138 |
-
# VITO STT ์ฒ๋ฆฌ
|
| 139 |
-
stt_result = stt_client.transcribe_audio(audio_bytes)
|
| 140 |
|
| 141 |
-
if
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 147 |
-
st.session_state.messages.append({"role": "user", "content": transcription})
|
| 148 |
-
with st.chat_message("user"):
|
| 149 |
-
st.write(transcription)
|
| 150 |
-
|
| 151 |
-
# ์ฑ๋ด ์๋ต ์์ญ
|
| 152 |
-
with st.chat_message("assistant"):
|
| 153 |
-
with st.spinner("๋ต๋ณ ์์ฑ ์ค..."):
|
| 154 |
-
try:
|
| 155 |
-
# RAG API ์์ฒญ
|
| 156 |
-
response = requests.post(
|
| 157 |
-
f"{API_BASE_URL}/rag",
|
| 158 |
-
json={
|
| 159 |
-
"query": transcription,
|
| 160 |
-
"retriever_type": retriever_type,
|
| 161 |
-
"top_k": top_k,
|
| 162 |
-
"temperature": temperature
|
| 163 |
-
},
|
| 164 |
-
timeout=30
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
if response.status_code == 200:
|
| 168 |
-
result = response.json()
|
| 169 |
-
answer = result.get("answer", "")
|
| 170 |
-
context_docs = result.get("context_docs", [])
|
| 171 |
-
|
| 172 |
-
# ์๋ต ํ์
|
| 173 |
-
st.markdown(answer)
|
| 174 |
-
|
| 175 |
-
# ์ธ์
์ํ ์
๋ฐ์ดํธ
|
| 176 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 177 |
-
st.session_state.sources = context_docs
|
| 178 |
-
else:
|
| 179 |
-
st.error(f"API ์ค๋ฅ: {response.status_code} - {response.text}")
|
| 180 |
-
except Exception as e:
|
| 181 |
-
st.error(f"์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 182 |
-
else:
|
| 183 |
-
st.warning("์์ฑ์์ ํ
์คํธ๋ฅผ ์ธ์ํ์ง ๋ชปํ์ต๋๋ค.")
|
| 184 |
else:
|
| 185 |
-
st.error(f"
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
|
|
|
| 3 |
import os
|
|
|
|
| 4 |
import json
|
| 5 |
+
import sys
|
| 6 |
|
| 7 |
+
# ์์คํ
์ ๋ณด ํ์ธ
|
| 8 |
+
st.write("Python version:", sys.version)
|
| 9 |
+
st.write("ํ์ฌ ์์
๋๋ ํ ๋ฆฌ:", os.getcwd())
|
| 10 |
|
| 11 |
+
# ํ๊ฒฝ ๋ณ์ ํ์ธ (๋ณด์์ ๋ฏผ๊ฐํ ์ ๋ณด๋ ๊ฐ๋ ค์ ํ์)
|
| 12 |
+
env_vars = {}
|
| 13 |
+
for key in os.environ:
|
| 14 |
+
value = os.environ[key]
|
| 15 |
+
if key in ['API_BASE_URL']:
|
| 16 |
+
env_vars[key] = value
|
| 17 |
+
elif 'SECRET' in key or 'KEY' in key or 'TOKEN' in key or 'PASSWORD' in key:
|
| 18 |
+
env_vars[key] = value[:4] + '****' if len(value) > 4 else '****'
|
| 19 |
|
| 20 |
+
st.subheader("ํ๊ฒฝ ๋ณ์")
|
| 21 |
+
st.json(env_vars)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# API ์ฐ๊ฒฐ ํ
์คํธ
|
| 24 |
+
st.subheader("API ์ฐ๊ฒฐ ํ
์คํธ")
|
| 25 |
|
| 26 |
+
api_url = os.environ.get('API_BASE_URL', 'https://enter-your-api-url.com')
|
| 27 |
+
st.write(f"API URL: {api_url}")
|
| 28 |
|
| 29 |
+
test_url = st.text_input("ํ
์คํธํ API URL", api_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if st.button("API ์ฐ๊ฒฐ ํ
์คํธ"):
|
| 32 |
+
try:
|
| 33 |
+
with st.spinner("API ์ฐ๊ฒฐ ํ
์คํธ ์ค..."):
|
| 34 |
+
response = requests.get(f"{test_url}/docs", timeout=10)
|
| 35 |
+
st.success(f"API ์ฐ๊ฒฐ ์ฑ๊ณต! ์ํ ์ฝ๋: {response.status_code}")
|
| 36 |
+
st.text(f"์๋ต ๋ด์ฉ (์ฒ์ 500์): {response.text[:500]}")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"API ์ฐ๊ฒฐ ์คํจ: {str(e)}")
|
| 39 |
+
st.info("API ์๋ฒ๊ฐ ์คํ ์ค์ธ์ง, URL์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ์ธ์.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# ํ์ผ ์์คํ
ํ์ธ
|
| 42 |
+
st.subheader("ํ์ผ ์์คํ
ํ์ธ")
|
| 43 |
+
files = os.listdir('.')
|
| 44 |
+
st.write("ํ์ฌ ๋๋ ํ ๋ฆฌ ํ์ผ:", files)
|
| 45 |
|
| 46 |
+
# ๊ฐ๋จํ ์ฑํ
UI
|
| 47 |
+
st.subheader("๊ฐ๋จํ ์ฑํ
ํ
์คํธ")
|
| 48 |
+
user_input = st.text_input("์ง๋ฌธ ์
๋ ฅ")
|
| 49 |
|
| 50 |
+
if st.button("์ ์ก") and user_input:
|
| 51 |
+
st.write(f"์ฌ์ฉ์: {user_input}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
try:
|
| 54 |
+
with st.spinner("์๋ต ์์ฑ ์ค..."):
|
| 55 |
+
response = requests.post(
|
| 56 |
+
f"{api_url}/rag",
|
| 57 |
+
json={
|
| 58 |
+
"query": user_input,
|
| 59 |
+
"retriever_type": "reranker",
|
| 60 |
+
"top_k": 3
|
| 61 |
+
},
|
| 62 |
+
timeout=30
|
| 63 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
result = response.json()
|
| 67 |
+
st.write(f"์ฑ๋ด: {result.get('answer', '์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค.')}")
|
| 68 |
+
st.write("์๋ต ์ธ๋ถ ์ ๋ณด:")
|
| 69 |
+
st.json(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
+
st.error(f"API ์ค๋ฅ: {response.status_code} - {response.text}")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 74 |
+
st.info("์์ธํ ์ค๋ฅ ์ ๋ณด:")
|
| 75 |
+
st.exception(e)
|