Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +55 -55
src/streamlit_app.py
CHANGED
@@ -36,7 +36,7 @@ st.sidebar.header("๐๏ธ ์นดํผ ์์ฑ ์ค์ ")
|
|
36 |
default_api_key = os.getenv("GEMINI_API_KEY", "")
|
37 |
|
38 |
api_key = st.sidebar.text_input(
|
39 |
-
"๐ Gemini API ํค",
|
40 |
value=default_api_key,
|
41 |
type="password",
|
42 |
help="ํ๊ฒฝ๋ณ์์ GEMINI_API_KEY๋ก ์ค์ ํ๋ฉด ์๋ ์
๋ ฅ๋ฉ๋๋ค"
|
@@ -51,14 +51,14 @@ if not api_key:
|
|
51 |
@st.cache_resource(show_spinner=False)
|
52 |
def load_system():
|
53 |
"""์์คํ
์ปดํฌ๋ํธ ๋ก๋ฉ - ์๋ฒ ๋ฉ ๊ธฐ๋ฐ RAG ์์คํ
"""
|
54 |
-
|
55 |
progress_container = st.container()
|
56 |
-
|
57 |
with progress_container:
|
58 |
# ์ ์ฒด ์งํ๋ฅ
|
59 |
total_progress = st.progress(0)
|
60 |
status_text = st.empty()
|
61 |
-
|
62 |
# 1๋จ๊ณ: API ์ค์ (10%)
|
63 |
status_text.text("๐ Gemini API ์ด๊ธฐํ ์ค...")
|
64 |
try:
|
@@ -69,42 +69,42 @@ def load_system():
|
|
69 |
except Exception as e:
|
70 |
st.error(f"โ Gemini API ์ค์ ์คํจ: {e}")
|
71 |
return None, None, None, None
|
72 |
-
|
73 |
# 2๋จ๊ณ: ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ (40%)
|
74 |
status_text.text("๐ค ํ๊ตญ์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์ค... (1-2๋ถ ์์)")
|
75 |
embedding_model = None
|
76 |
-
|
77 |
# ์์ ์ ์ธ ๋ชจ๋ธ ๋ก๋ฉ ์ ๋ต
|
78 |
try:
|
79 |
# ๋จผ์ ์บ์ ๋๋ ํ ๋ฆฌ ์์ฑ
|
80 |
os.makedirs('/tmp/sentence_transformers', exist_ok=True)
|
81 |
os.makedirs('/tmp/transformers', exist_ok=True)
|
82 |
-
|
83 |
# sentence-transformers ์ํฌํธ๋ฅผ ํจ์ ๋ด์์
|
84 |
from sentence_transformers import SentenceTransformer
|
85 |
from sklearn.metrics.pairwise import cosine_similarity
|
86 |
-
|
87 |
# ํ๊ตญ์ด ๋ชจ๋ธ ๋ก๋ฉ ์๋
|
88 |
-
embedding_model = SentenceTransformer('jhgan/ko-sbert-nli',
|
89 |
cache_folder='/tmp/sentence_transformers')
|
90 |
total_progress.progress(40)
|
91 |
st.success("โ
ํ๊ตญ์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
92 |
-
|
93 |
except Exception as e:
|
94 |
st.error(f"โ ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
95 |
st.error("๐จ ์๋ฒ ๋ฉ ๋ชจ๋ธ ์์ด๋ RAG ์์คํ
์ด ์๋ํ ์ ์์ต๋๋ค!")
|
96 |
return None, None, None, None
|
97 |
-
|
98 |
# 3๋จ๊ณ: ๋ฐ์ดํฐ ๋ก๋ (60%)
|
99 |
status_text.text("๐ ์นดํผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ก๋ฉ ์ค...")
|
100 |
try:
|
101 |
-
df = pd.read_excel('
|
102 |
total_progress.progress(60)
|
103 |
st.success(f"โ
๋ฐ์ดํฐ ๋ก๋ฉ ์๋ฃ: {len(df):,}๊ฐ ์นดํผ")
|
104 |
except Exception as e:
|
105 |
st.error(f"โ ๋ฐ์ดํฐ ๋ก๋ฉ ์คํจ: {e}")
|
106 |
return None, None, None, None
|
107 |
-
|
108 |
# 4๋จ๊ณ: ์๋ฒ ๋ฉ ๋ฐ์ดํฐ ๋ก๋ (90%) - ์ด๊ฒ ํต์ฌ!
|
109 |
status_text.text("๐ ๋ฒกํฐ ์๋ฒ ๋ฉ ๋ก๋ฉ ์ค... (RAG ์์คํ
ํต์ฌ)")
|
110 |
try:
|
@@ -117,13 +117,13 @@ def load_system():
|
|
117 |
st.error(f"โ ์๋ฒ ๋ฉ ๋ก๋ฉ ์คํจ: {e}")
|
118 |
st.error("๐จ ์๋ฒ ๋ฉ ์์ด๋ ์๋ฏธ์ ๊ฒ์์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค!")
|
119 |
return None, None, None, None
|
120 |
-
|
121 |
# 5๋จ๊ณ: ์ต์ข
๊ฒ์ฆ (100%)
|
122 |
status_text.text("โจ ์์คํ
๊ฒ์ฆ ์ค...")
|
123 |
if model and embedding_model and df is not None and embeddings is not None:
|
124 |
total_progress.progress(100)
|
125 |
status_text.text("๐ RAG ์์คํ
๋ก๋ฉ ์๋ฃ!")
|
126 |
-
|
127 |
# ์ฑ๊ณต ๋ฉ์์ง
|
128 |
success_col1, success_col2, success_col3 = st.columns(3)
|
129 |
with success_col1:
|
@@ -132,12 +132,12 @@ def load_system():
|
|
132 |
st.metric("์๋ฒ ๋ฉ ์ฐจ์", f"{embeddings.shape[1]}D")
|
133 |
with success_col3:
|
134 |
st.metric("๊ฒ์ ์์ง", "Korean SBERT")
|
135 |
-
|
136 |
# ์งํ๋ฅ ๋ฐ ์ ๊ฑฐ
|
137 |
time.sleep(1)
|
138 |
total_progress.empty()
|
139 |
status_text.empty()
|
140 |
-
|
141 |
return model, embedding_model, df, embeddings
|
142 |
else:
|
143 |
st.error("โ ์์คํ
๋ก๋ฉ ์คํจ: ํ์ ๊ตฌ์ฑ์์ ๋๋ฝ")
|
@@ -204,14 +204,14 @@ else:
|
|
204 |
# ํ
ํ๋ฆฟ ์ ํ
|
205 |
templates = {
|
206 |
"์ ์ ํ ๋ฐ์นญ": "๋์ {์นดํ
๊ณ ๋ฆฌ} ์ ์ ํ ๋ฐ์นญ ์นดํผ",
|
207 |
-
"ํ ์ธ ์ด๋ฒคํธ": "{์นดํ
๊ณ ๋ฆฌ} ํ ์ธ ์ด๋ฒคํธ ํ๋ก๋ชจ์
์นดํผ",
|
208 |
"๋ธ๋๋ ์ฌ๋ก๊ฑด": "{์นดํ
๊ณ ๋ฆฌ} ๋ธ๋๋์ ๋ํ ์ฌ๋ก๊ฑด",
|
209 |
"์ฑ/์๋น์ค ๋ฆฌ๋ด์ผ": "{์๋น์ค๋ช
} ์ ๋ฒ์ ์ถ์ ์นดํผ",
|
210 |
"์์ฆ ํ์ ": "{์์ฆ} ํ์ {์นดํ
๊ณ ๋ฆฌ} ํน๋ณ ์๋์
์นดํผ"
|
211 |
}
|
212 |
-
|
213 |
selected_template = st.selectbox("ํ
ํ๋ฆฟ ์ ํ:", list(templates.keys()))
|
214 |
-
|
215 |
col1, col2 = st.columns(2)
|
216 |
with col1:
|
217 |
template_category = st.text_input("์ ํ/์๋น์ค:", value="")
|
@@ -224,7 +224,7 @@ else:
|
|
224 |
user_request = templates[selected_template].format(์์ฆ=season, ์นดํ
๊ณ ๋ฆฌ=template_category)
|
225 |
else:
|
226 |
user_request = templates[selected_template].format(์นดํ
๊ณ ๋ฆฌ=template_category)
|
227 |
-
|
228 |
st.text_area("์์ฑ๋ ์์ฒญ:", value=user_request, height=80, disabled=True)
|
229 |
|
230 |
# ๊ณ ๊ธ ์ต์
|
@@ -240,41 +240,41 @@ with st.expander("๐ง ๊ณ ๊ธ ์ต์
"):
|
|
240 |
# RAG ์นดํผ ์์ฑ ํจ์ (์๋ฒ ๋ฉ ๊ธฐ๋ฐ ํ์!)
|
241 |
def generate_copy_with_rag(user_request, category, target, tone, creative, num_concepts):
|
242 |
"""RAG ๊ธฐ๋ฐ ์นดํผ ์์ฑ - ์๋ฒ ๋ฉ ํ์ ์ฌ์ฉ"""
|
243 |
-
|
244 |
if not user_request.strip():
|
245 |
st.error("โ ์นดํผ ์์ฒญ์ ์
๋ ฅํด์ฃผ์ธ์")
|
246 |
return None
|
247 |
-
|
248 |
# ์งํ ์ํฉ ํ์
|
249 |
progress_bar = st.progress(0)
|
250 |
status_text = st.empty()
|
251 |
-
|
252 |
# 1๋จ๊ณ: ์๋ฏธ์ ๊ฒ์ (์๋ฒ ๋ฉ ๊ธฐ๋ฐ)
|
253 |
status_text.text("๐ ์๋ฏธ์ ๊ฒ์ ์ค... (RAG ํต์ฌ ๊ธฐ๋ฅ)")
|
254 |
progress_bar.progress(20)
|
255 |
-
|
256 |
try:
|
257 |
# ๊ฒ์ ์ฟผ๋ฆฌ ์์ฑ ๋ฐ ์๋ฒ ๋ฉ
|
258 |
search_query = f"{user_request} {target} ๊ด๊ณ ์นดํผ"
|
259 |
from sklearn.metrics.pairwise import cosine_similarity
|
260 |
query_embedding = embedding_model.encode([search_query])
|
261 |
-
|
262 |
# ์นดํ
๊ณ ๋ฆฌ ํํฐ๋ง
|
263 |
if category != '์ ์ฒด':
|
264 |
filtered_df = df[df['์นดํ
๊ณ ๋ฆฌ'] == category]
|
265 |
else:
|
266 |
filtered_df = df
|
267 |
-
|
268 |
progress_bar.progress(40)
|
269 |
-
|
270 |
# ์ ์ฌ๋ ๊ณ์ฐ (์๋ฒ ๋ฉ์ ํต์ฌ!)
|
271 |
filtered_indices = filtered_df.index.tolist()
|
272 |
filtered_embeddings = embeddings[filtered_indices]
|
273 |
similarities = cosine_similarity(query_embedding, filtered_embeddings)[0]
|
274 |
-
|
275 |
# ์์ ์ฐธ๊ณ ์นดํผ ์ ๋ณ
|
276 |
top_indices = np.argsort(similarities)[::-1][:num_references]
|
277 |
-
|
278 |
reference_copies = []
|
279 |
for idx in top_indices:
|
280 |
original_idx = filtered_indices[idx]
|
@@ -285,31 +285,31 @@ def generate_copy_with_rag(user_request, category, target, tone, creative, num_c
|
|
285 |
'brand': row['๋ธ๋๋'],
|
286 |
'similarity': similarities[idx]
|
287 |
})
|
288 |
-
|
289 |
progress_bar.progress(60)
|
290 |
-
|
291 |
if not reference_copies:
|
292 |
st.warning(f"โ ๏ธ ์ ์ฌ๋ {min_similarity} ์ด์์ธ ์ฐธ๊ณ ์นดํผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ ์ฌ๋๋ฅผ ๋ฎ์ถฐ๋ณด์ธ์.")
|
293 |
progress_bar.empty()
|
294 |
status_text.empty()
|
295 |
return None
|
296 |
-
|
297 |
# 2๋จ๊ณ: AI ์นดํผ ์์ฑ
|
298 |
status_text.text("๐ค AI ์นดํผ ์์ฑ ์ค...")
|
299 |
progress_bar.progress(80)
|
300 |
-
|
301 |
# ํ๋กฌํํธ ์์ฑ
|
302 |
references_text = "\n".join([
|
303 |
f"{i}. \"{ref['copy']}\" - {ref['brand']} (์ ์ฌ๋: {ref['similarity']:.3f})"
|
304 |
for i, ref in enumerate(reference_copies, 1)
|
305 |
])
|
306 |
-
|
307 |
creativity_guidance = {
|
308 |
"๋ณด์์ ": "์์ ํ๊ณ ๊ฒ์ฆ๋ ํํ์ ์ฌ์ฉํ์ฌ",
|
309 |
-
"๊ท ํ": "์ฐฝ์์ ์ด๋ฉด์๋ ์ ์ ํ ์์ค์์",
|
310 |
"์ฐฝ์์ ": "๋
์ฐฝ์ ์ด๊ณ ํ์ ์ ์ธ ํํ์ผ๋ก"
|
311 |
}
|
312 |
-
|
313 |
prompt = f"""
|
314 |
๋น์ ์ ํ๊ตญ์ ์ ๋ฌธ ๊ด๊ณ ์นดํผ๋ผ์ดํฐ์
๋๋ค.
|
315 |
|
@@ -332,7 +332,7 @@ def generate_copy_with_rag(user_request, category, target, tone, creative, num_c
|
|
332 |
1. [์นดํผ1]
|
333 |
- ์ค๋ช
: ์ ์ด ์นดํผ๊ฐ ํจ๊ณผ์ ์ธ์ง ๊ฐ๋จํ ์ค๋ช
|
334 |
|
335 |
-
2. [์นดํผ2]
|
336 |
- ์ค๋ช
: ์ ์ด ์นดํผ๊ฐ ํจ๊ณผ์ ์ธ์ง ๊ฐ๋จํ ์ค๋ช
|
337 |
|
338 |
3. [์นดํผ3]
|
@@ -340,15 +340,15 @@ def generate_copy_with_rag(user_request, category, target, tone, creative, num_c
|
|
340 |
|
341 |
**์ถ์ฒ ์นดํผ:** ์ ์ค ๊ฐ์ฅ ์ถ์ฒํ๋ ์นดํผ์ ์ด์
|
342 |
"""
|
343 |
-
|
344 |
response = model.generate_content(prompt)
|
345 |
progress_bar.progress(100)
|
346 |
status_text.text("โ
์๋ฃ!")
|
347 |
-
|
348 |
time.sleep(0.5)
|
349 |
progress_bar.empty()
|
350 |
status_text.empty()
|
351 |
-
|
352 |
return {
|
353 |
'references': reference_copies,
|
354 |
'generated_content': response.text,
|
@@ -364,7 +364,7 @@ def generate_copy_with_rag(user_request, category, target, tone, creative, num_c
|
|
364 |
'creative': creative
|
365 |
}
|
366 |
}
|
367 |
-
|
368 |
except Exception as e:
|
369 |
st.error(f"โ ์นดํผ ์์ฑ ์คํจ: {e}")
|
370 |
progress_bar.empty()
|
@@ -373,7 +373,7 @@ def generate_copy_with_rag(user_request, category, target, tone, creative, num_c
|
|
373 |
|
374 |
# ์์ฑ ๋ฒํผ
|
375 |
if st.button("๐ ์นดํผ ์์ฑํ๊ธฐ", type="primary", use_container_width=True):
|
376 |
-
|
377 |
if not user_request or not user_request.strip():
|
378 |
st.error("โ ์นดํผ ์์ฒญ์ ์
๋ ฅํด์ฃผ์ธ์")
|
379 |
else:
|
@@ -386,29 +386,29 @@ if st.button("๐ ์นดํผ ์์ฑํ๊ธฐ", type="primary", use_container_width=Tru
|
|
386 |
creative=creative_level,
|
387 |
num_concepts=num_concepts
|
388 |
)
|
389 |
-
|
390 |
if result:
|
391 |
# ๊ฒฐ๊ณผ ํ์
|
392 |
st.markdown("## ๐ ์์ฑ๋ ์นด๏ฟฝ๏ฟฝ๏ฟฝ")
|
393 |
st.markdown("---")
|
394 |
-
|
395 |
# ๊ฒ์ ์ ๋ณด ํ์
|
396 |
st.info(f"๐ **๊ฒ์ ์ ๋ณด**: {result['search_info']['total_candidates']:,}๊ฐ ํ๋ณด์์ "
|
397 |
f"{result['search_info']['selected_references']}๊ฐ ์ฐธ๊ณ ์นดํผ ์ ๋ณ")
|
398 |
-
|
399 |
# ์ฐธ๊ณ ์นดํผ ํ์
|
400 |
if show_references and result['references']:
|
401 |
with st.expander("๐ ์ฐธ๊ณ ํ ์นดํผ๋ค (์๋ฏธ์ ์ ์ฌ๋ ๊ธฐ๋ฐ ์ ๋ณ)"):
|
402 |
for i, ref in enumerate(result['references'], 1):
|
403 |
st.markdown(f"**{i}.** \"{ref['copy']}\"")
|
404 |
-
st.markdown(f" - ๋ธ๋๋: {ref['brand']}")
|
405 |
st.markdown(f" - ์ ์ฌ๋: {ref['similarity']:.3f}")
|
406 |
st.markdown("")
|
407 |
-
|
408 |
# ์์ฑ๋ ์นดํผ ํ์
|
409 |
st.markdown("### โจ AI๊ฐ ์์ฑํ ์นดํผ:")
|
410 |
st.markdown(result['generated_content'])
|
411 |
-
|
412 |
# ๊ฒฐ๊ณผ ๋ค์ด๋ก๋
|
413 |
result_json = json.dumps({
|
414 |
'timestamp': datetime.now().isoformat(),
|
@@ -417,7 +417,7 @@ if st.button("๐ ์นดํผ ์์ฑํ๊ธฐ", type="primary", use_container_width=Tru
|
|
417 |
'search_info': result['search_info'],
|
418 |
'generated_content': result['generated_content']
|
419 |
}, ensure_ascii=False, indent=2)
|
420 |
-
|
421 |
st.download_button(
|
422 |
label="๐พ ๊ฒฐ๊ณผ ๋ค์ด๋ก๋ (JSON)",
|
423 |
data=result_json,
|
@@ -440,21 +440,21 @@ if df is not None and embeddings is not None:
|
|
440 |
with st.expander("๐ก RAG ์์คํ
์ฌ์ฉ๋ฒ ๊ฐ์ด๋"):
|
441 |
st.markdown("""
|
442 |
### ๐ฏ ํจ๊ณผ์ ์ธ ์ฌ์ฉ๋ฒ
|
443 |
-
|
444 |
**1. ๊ตฌ์ฒด์ ์ธ ์์ฒญํ๊ธฐ:**
|
445 |
- โ "์นดํผ ์จ์ค"
|
446 |
- โ
"30๋ ์ง์ฅ ์ฌ์ฑ์ฉ ํ๋ฆฌ๋ฏธ์ ์คํจ์ผ์ด ์ ์ ํ ๋ฐ์นญ ์นดํผ"
|
447 |
-
|
448 |
**2. RAG ์์คํ
์ ์ฅ์ :**
|
449 |
- ๐ง **์๋ฏธ์ ๊ฒ์**: ํค์๋๋ฟ๋ง ์๋๋ผ ์๋ฏธ๊น์ง ์ดํด
|
450 |
- ๐ฏ **๋ฌธ๋งฅ ๋งค์นญ**: ํ๊ฒ๊ณผ ์ํฉ์ ๋ง๋ ์นดํผ ์๋ ์ ๋ณ
|
451 |
- ๐ **๋ฐ์ดํฐ ๊ธฐ๋ฐ**: 37,671๊ฐ ์ค์ ์นดํผ์์ ํ์ตํ ํจํด
|
452 |
-
|
453 |
**3. ์ฐฝ์์ฑ ์กฐ์ :**
|
454 |
- **๋ณด์์ **: ์์ ํ ํด๋ผ์ด์ธํธ, ๊ฒ์ฆ๋ ์ ๊ทผ
|
455 |
- **๊ท ํ**: ์ผ๋ฐ์ ์ธ ํ๋ก์ ํธ (์ถ์ฒ!)
|
456 |
- **์ฐฝ์์ **: ํ์ ์ ๋ธ๋๋, ํ๊ฒฉ์ ์บ ํ์ธ
|
457 |
-
|
458 |
**4. ์ฐธ๊ณ ์นดํผ ํ์ฉ:**
|
459 |
- ์์ฑ๋ ์นดํผ์ ์ฐธ๊ณ ์นดํผ๋ฅผ ๋น๊ต ๋ถ์
|
460 |
- ํธ๋ ๋์ ํจํด ํ์
๊ฐ๋ฅ
|
@@ -473,4 +473,4 @@ if os.getenv("DEBUG_MODE"):
|
|
473 |
st.sidebar.markdown("### ๐ง ๋๋ฒ๊ทธ ์ ๋ณด")
|
474 |
if 'embeddings' in locals():
|
475 |
st.sidebar.write(f"์๋ฒ ๋ฉ ๋ฉ๋ชจ๋ฆฌ: {embeddings.nbytes / (1024*1024):.1f}MB")
|
476 |
-
st.sidebar.write(f"Streamlit ๋ฒ์ : {st.__version__}")
|
|
|
36 |
default_api_key = os.getenv("GEMINI_API_KEY", "")
|
37 |
|
38 |
api_key = st.sidebar.text_input(
|
39 |
+
"๐ Gemini API ํค",
|
40 |
value=default_api_key,
|
41 |
type="password",
|
42 |
help="ํ๊ฒฝ๋ณ์์ GEMINI_API_KEY๋ก ์ค์ ํ๋ฉด ์๋ ์
๋ ฅ๋ฉ๋๋ค"
|
|
|
51 |
@st.cache_resource(show_spinner=False)
|
52 |
def load_system():
|
53 |
"""์์คํ
์ปดํฌ๋ํธ ๋ก๋ฉ - ์๋ฒ ๋ฉ ๊ธฐ๋ฐ RAG ์์คํ
"""
|
54 |
+
|
55 |
progress_container = st.container()
|
56 |
+
|
57 |
with progress_container:
|
58 |
# ์ ์ฒด ์งํ๋ฅ
|
59 |
total_progress = st.progress(0)
|
60 |
status_text = st.empty()
|
61 |
+
|
62 |
# 1๋จ๊ณ: API ์ค์ (10%)
|
63 |
status_text.text("๐ Gemini API ์ด๊ธฐํ ์ค...")
|
64 |
try:
|
|
|
69 |
except Exception as e:
|
70 |
st.error(f"โ Gemini API ์ค์ ์คํจ: {e}")
|
71 |
return None, None, None, None
|
72 |
+
|
73 |
# 2๋จ๊ณ: ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ (40%)
|
74 |
status_text.text("๐ค ํ๊ตญ์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์ค... (1-2๋ถ ์์)")
|
75 |
embedding_model = None
|
76 |
+
|
77 |
# ์์ ์ ์ธ ๋ชจ๋ธ ๋ก๋ฉ ์ ๋ต
|
78 |
try:
|
79 |
# ๋จผ์ ์บ์ ๋๋ ํ ๋ฆฌ ์์ฑ
|
80 |
os.makedirs('/tmp/sentence_transformers', exist_ok=True)
|
81 |
os.makedirs('/tmp/transformers', exist_ok=True)
|
82 |
+
|
83 |
# sentence-transformers ์ํฌํธ๋ฅผ ํจ์ ๋ด์์
|
84 |
from sentence_transformers import SentenceTransformer
|
85 |
from sklearn.metrics.pairwise import cosine_similarity
|
86 |
+
|
87 |
# ํ๊ตญ์ด ๋ชจ๋ธ ๋ก๋ฉ ์๋
|
88 |
+
embedding_model = SentenceTransformer('jhgan/ko-sbert-nli',
|
89 |
cache_folder='/tmp/sentence_transformers')
|
90 |
total_progress.progress(40)
|
91 |
st.success("โ
ํ๊ตญ์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
92 |
+
|
93 |
except Exception as e:
|
94 |
st.error(f"โ ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
|
95 |
st.error("๐จ ์๋ฒ ๋ฉ ๋ชจ๋ธ ์์ด๋ RAG ์์คํ
์ด ์๋ํ ์ ์์ต๋๋ค!")
|
96 |
return None, None, None, None
|
97 |
+
|
98 |
# 3๋จ๊ณ: ๋ฐ์ดํฐ ๋ก๋ (60%)
|
99 |
status_text.text("๐ ์นดํผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ก๋ฉ ์ค...")
|
100 |
try:
|
101 |
+
df = pd.read_excel('../๊ด๊ณ ์นดํผ๋ฐ์ดํฐ_๋ธ๋๋์ถ์ถ์๋ฃ.xlsx')
|
102 |
total_progress.progress(60)
|
103 |
st.success(f"โ
๋ฐ์ดํฐ ๋ก๋ฉ ์๋ฃ: {len(df):,}๊ฐ ์นดํผ")
|
104 |
except Exception as e:
|
105 |
st.error(f"โ ๋ฐ์ดํฐ ๋ก๋ฉ ์คํจ: {e}")
|
106 |
return None, None, None, None
|
107 |
+
|
108 |
# 4๋จ๊ณ: ์๋ฒ ๋ฉ ๋ฐ์ดํฐ ๋ก๋ (90%) - ์ด๊ฒ ํต์ฌ!
|
109 |
status_text.text("๐ ๋ฒกํฐ ์๋ฒ ๋ฉ ๋ก๋ฉ ์ค... (RAG ์์คํ
ํต์ฌ)")
|
110 |
try:
|
|
|
117 |
st.error(f"โ ์๋ฒ ๋ฉ ๋ก๋ฉ ์คํจ: {e}")
|
118 |
st.error("๐จ ์๋ฒ ๋ฉ ์์ด๋ ์๋ฏธ์ ๊ฒ์์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค!")
|
119 |
return None, None, None, None
|
120 |
+
|
121 |
# 5๋จ๊ณ: ์ต์ข
๊ฒ์ฆ (100%)
|
122 |
status_text.text("โจ ์์คํ
๊ฒ์ฆ ์ค...")
|
123 |
if model and embedding_model and df is not None and embeddings is not None:
|
124 |
total_progress.progress(100)
|
125 |
status_text.text("๐ RAG ์์คํ
๋ก๋ฉ ์๋ฃ!")
|
126 |
+
|
127 |
# ์ฑ๊ณต ๋ฉ์์ง
|
128 |
success_col1, success_col2, success_col3 = st.columns(3)
|
129 |
with success_col1:
|
|
|
132 |
st.metric("์๋ฒ ๋ฉ ์ฐจ์", f"{embeddings.shape[1]}D")
|
133 |
with success_col3:
|
134 |
st.metric("๊ฒ์ ์์ง", "Korean SBERT")
|
135 |
+
|
136 |
# ์งํ๋ฅ ๋ฐ ์ ๊ฑฐ
|
137 |
time.sleep(1)
|
138 |
total_progress.empty()
|
139 |
status_text.empty()
|
140 |
+
|
141 |
return model, embedding_model, df, embeddings
|
142 |
else:
|
143 |
st.error("โ ์์คํ
๋ก๋ฉ ์คํจ: ํ์ ๊ตฌ์ฑ์์ ๋๋ฝ")
|
|
|
204 |
# ํ
ํ๋ฆฟ ์ ํ
|
205 |
templates = {
|
206 |
"์ ์ ํ ๋ฐ์นญ": "๋์ {์นดํ
๊ณ ๋ฆฌ} ์ ์ ํ ๋ฐ์นญ ์นดํผ",
|
207 |
+
"ํ ์ธ ์ด๋ฒคํธ": "{์นดํ
๊ณ ๋ฆฌ} ํ ์ธ ์ด๋ฒคํธ ํ๋ก๋ชจ์
์นดํผ",
|
208 |
"๋ธ๋๋ ์ฌ๋ก๊ฑด": "{์นดํ
๊ณ ๋ฆฌ} ๋ธ๋๋์ ๋ํ ์ฌ๋ก๊ฑด",
|
209 |
"์ฑ/์๋น์ค ๋ฆฌ๋ด์ผ": "{์๋น์ค๋ช
} ์ ๋ฒ์ ์ถ์ ์นดํผ",
|
210 |
"์์ฆ ํ์ ": "{์์ฆ} ํ์ {์นดํ
๊ณ ๋ฆฌ} ํน๋ณ ์๋์
์นดํผ"
|
211 |
}
|
212 |
+
|
213 |
selected_template = st.selectbox("ํ
ํ๋ฆฟ ์ ํ:", list(templates.keys()))
|
214 |
+
|
215 |
col1, col2 = st.columns(2)
|
216 |
with col1:
|
217 |
template_category = st.text_input("์ ํ/์๋น์ค:", value="")
|
|
|
224 |
user_request = templates[selected_template].format(์์ฆ=season, ์นดํ
๊ณ ๋ฆฌ=template_category)
|
225 |
else:
|
226 |
user_request = templates[selected_template].format(์นดํ
๊ณ ๋ฆฌ=template_category)
|
227 |
+
|
228 |
st.text_area("์์ฑ๋ ์์ฒญ:", value=user_request, height=80, disabled=True)
|
229 |
|
230 |
# ๊ณ ๊ธ ์ต์
|
|
|
240 |
# RAG ์นดํผ ์์ฑ ํจ์ (์๋ฒ ๋ฉ ๊ธฐ๋ฐ ํ์!)
|
241 |
def generate_copy_with_rag(user_request, category, target, tone, creative, num_concepts):
|
242 |
"""RAG ๊ธฐ๋ฐ ์นดํผ ์์ฑ - ์๋ฒ ๋ฉ ํ์ ์ฌ์ฉ"""
|
243 |
+
|
244 |
if not user_request.strip():
|
245 |
st.error("โ ์นดํผ ์์ฒญ์ ์
๋ ฅํด์ฃผ์ธ์")
|
246 |
return None
|
247 |
+
|
248 |
# ์งํ ์ํฉ ํ์
|
249 |
progress_bar = st.progress(0)
|
250 |
status_text = st.empty()
|
251 |
+
|
252 |
# 1๋จ๊ณ: ์๋ฏธ์ ๊ฒ์ (์๋ฒ ๋ฉ ๊ธฐ๋ฐ)
|
253 |
status_text.text("๐ ์๋ฏธ์ ๊ฒ์ ์ค... (RAG ํต์ฌ ๊ธฐ๋ฅ)")
|
254 |
progress_bar.progress(20)
|
255 |
+
|
256 |
try:
|
257 |
# ๊ฒ์ ์ฟผ๋ฆฌ ์์ฑ ๋ฐ ์๋ฒ ๋ฉ
|
258 |
search_query = f"{user_request} {target} ๊ด๊ณ ์นดํผ"
|
259 |
from sklearn.metrics.pairwise import cosine_similarity
|
260 |
query_embedding = embedding_model.encode([search_query])
|
261 |
+
|
262 |
# ์นดํ
๊ณ ๋ฆฌ ํํฐ๋ง
|
263 |
if category != '์ ์ฒด':
|
264 |
filtered_df = df[df['์นดํ
๊ณ ๋ฆฌ'] == category]
|
265 |
else:
|
266 |
filtered_df = df
|
267 |
+
|
268 |
progress_bar.progress(40)
|
269 |
+
|
270 |
# ์ ์ฌ๋ ๊ณ์ฐ (์๋ฒ ๋ฉ์ ํต์ฌ!)
|
271 |
filtered_indices = filtered_df.index.tolist()
|
272 |
filtered_embeddings = embeddings[filtered_indices]
|
273 |
similarities = cosine_similarity(query_embedding, filtered_embeddings)[0]
|
274 |
+
|
275 |
# ์์ ์ฐธ๊ณ ์นดํผ ์ ๋ณ
|
276 |
top_indices = np.argsort(similarities)[::-1][:num_references]
|
277 |
+
|
278 |
reference_copies = []
|
279 |
for idx in top_indices:
|
280 |
original_idx = filtered_indices[idx]
|
|
|
285 |
'brand': row['๋ธ๋๋'],
|
286 |
'similarity': similarities[idx]
|
287 |
})
|
288 |
+
|
289 |
progress_bar.progress(60)
|
290 |
+
|
291 |
if not reference_copies:
|
292 |
st.warning(f"โ ๏ธ ์ ์ฌ๋ {min_similarity} ์ด์์ธ ์ฐธ๊ณ ์นดํผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ ์ฌ๋๋ฅผ ๋ฎ์ถฐ๋ณด์ธ์.")
|
293 |
progress_bar.empty()
|
294 |
status_text.empty()
|
295 |
return None
|
296 |
+
|
297 |
# 2๋จ๊ณ: AI ์นดํผ ์์ฑ
|
298 |
status_text.text("๐ค AI ์นดํผ ์์ฑ ์ค...")
|
299 |
progress_bar.progress(80)
|
300 |
+
|
301 |
# ํ๋กฌํํธ ์์ฑ
|
302 |
references_text = "\n".join([
|
303 |
f"{i}. \"{ref['copy']}\" - {ref['brand']} (์ ์ฌ๋: {ref['similarity']:.3f})"
|
304 |
for i, ref in enumerate(reference_copies, 1)
|
305 |
])
|
306 |
+
|
307 |
creativity_guidance = {
|
308 |
"๋ณด์์ ": "์์ ํ๊ณ ๊ฒ์ฆ๋ ํํ์ ์ฌ์ฉํ์ฌ",
|
309 |
+
"๊ท ํ": "์ฐฝ์์ ์ด๋ฉด์๋ ์ ์ ํ ์์ค์์",
|
310 |
"์ฐฝ์์ ": "๋
์ฐฝ์ ์ด๊ณ ํ์ ์ ์ธ ํํ์ผ๋ก"
|
311 |
}
|
312 |
+
|
313 |
prompt = f"""
|
314 |
๋น์ ์ ํ๊ตญ์ ์ ๋ฌธ ๊ด๊ณ ์นดํผ๋ผ์ดํฐ์
๋๋ค.
|
315 |
|
|
|
332 |
1. [์นดํผ1]
|
333 |
- ์ค๋ช
: ์ ์ด ์นดํผ๊ฐ ํจ๊ณผ์ ์ธ์ง ๊ฐ๋จํ ์ค๋ช
|
334 |
|
335 |
+
2. [์นดํผ2]
|
336 |
- ์ค๋ช
: ์ ์ด ์นดํผ๊ฐ ํจ๊ณผ์ ์ธ์ง ๊ฐ๋จํ ์ค๋ช
|
337 |
|
338 |
3. [์นดํผ3]
|
|
|
340 |
|
341 |
**์ถ์ฒ ์นดํผ:** ์ ์ค ๊ฐ์ฅ ์ถ์ฒํ๋ ์นดํผ์ ์ด์
|
342 |
"""
|
343 |
+
|
344 |
response = model.generate_content(prompt)
|
345 |
progress_bar.progress(100)
|
346 |
status_text.text("โ
์๋ฃ!")
|
347 |
+
|
348 |
time.sleep(0.5)
|
349 |
progress_bar.empty()
|
350 |
status_text.empty()
|
351 |
+
|
352 |
return {
|
353 |
'references': reference_copies,
|
354 |
'generated_content': response.text,
|
|
|
364 |
'creative': creative
|
365 |
}
|
366 |
}
|
367 |
+
|
368 |
except Exception as e:
|
369 |
st.error(f"โ ์นดํผ ์์ฑ ์คํจ: {e}")
|
370 |
progress_bar.empty()
|
|
|
373 |
|
374 |
# ์์ฑ ๋ฒํผ
|
375 |
if st.button("๐ ์นดํผ ์์ฑํ๊ธฐ", type="primary", use_container_width=True):
|
376 |
+
|
377 |
if not user_request or not user_request.strip():
|
378 |
st.error("โ ์นดํผ ์์ฒญ์ ์
๋ ฅํด์ฃผ์ธ์")
|
379 |
else:
|
|
|
386 |
creative=creative_level,
|
387 |
num_concepts=num_concepts
|
388 |
)
|
389 |
+
|
390 |
if result:
|
391 |
# ๊ฒฐ๊ณผ ํ์
|
392 |
st.markdown("## ๐ ์์ฑ๋ ์นด๏ฟฝ๏ฟฝ๏ฟฝ")
|
393 |
st.markdown("---")
|
394 |
+
|
395 |
# ๊ฒ์ ์ ๋ณด ํ์
|
396 |
st.info(f"๐ **๊ฒ์ ์ ๋ณด**: {result['search_info']['total_candidates']:,}๊ฐ ํ๋ณด์์ "
|
397 |
f"{result['search_info']['selected_references']}๊ฐ ์ฐธ๊ณ ์นดํผ ์ ๋ณ")
|
398 |
+
|
399 |
# ์ฐธ๊ณ ์นดํผ ํ์
|
400 |
if show_references and result['references']:
|
401 |
with st.expander("๐ ์ฐธ๊ณ ํ ์นดํผ๋ค (์๋ฏธ์ ์ ์ฌ๋ ๊ธฐ๋ฐ ์ ๋ณ)"):
|
402 |
for i, ref in enumerate(result['references'], 1):
|
403 |
st.markdown(f"**{i}.** \"{ref['copy']}\"")
|
404 |
+
st.markdown(f" - ๋ธ๋๋: {ref['brand']}")
|
405 |
st.markdown(f" - ์ ์ฌ๋: {ref['similarity']:.3f}")
|
406 |
st.markdown("")
|
407 |
+
|
408 |
# ์์ฑ๋ ์นดํผ ํ์
|
409 |
st.markdown("### โจ AI๊ฐ ์์ฑํ ์นดํผ:")
|
410 |
st.markdown(result['generated_content'])
|
411 |
+
|
412 |
# ๊ฒฐ๊ณผ ๋ค์ด๋ก๋
|
413 |
result_json = json.dumps({
|
414 |
'timestamp': datetime.now().isoformat(),
|
|
|
417 |
'search_info': result['search_info'],
|
418 |
'generated_content': result['generated_content']
|
419 |
}, ensure_ascii=False, indent=2)
|
420 |
+
|
421 |
st.download_button(
|
422 |
label="๐พ ๊ฒฐ๊ณผ ๋ค์ด๋ก๋ (JSON)",
|
423 |
data=result_json,
|
|
|
440 |
with st.expander("๐ก RAG ์์คํ
์ฌ์ฉ๋ฒ ๊ฐ์ด๋"):
|
441 |
st.markdown("""
|
442 |
### ๐ฏ ํจ๊ณผ์ ์ธ ์ฌ์ฉ๋ฒ
|
443 |
+
|
444 |
**1. ๊ตฌ์ฒด์ ์ธ ์์ฒญํ๊ธฐ:**
|
445 |
- โ "์นดํผ ์จ์ค"
|
446 |
- โ
"30๋ ์ง์ฅ ์ฌ์ฑ์ฉ ํ๋ฆฌ๋ฏธ์ ์คํจ์ผ์ด ์ ์ ํ ๋ฐ์นญ ์นดํผ"
|
447 |
+
|
448 |
**2. RAG ์์คํ
์ ์ฅ์ :**
|
449 |
- ๐ง **์๋ฏธ์ ๊ฒ์**: ํค์๋๋ฟ๋ง ์๋๋ผ ์๋ฏธ๊น์ง ์ดํด
|
450 |
- ๐ฏ **๋ฌธ๋งฅ ๋งค์นญ**: ํ๊ฒ๊ณผ ์ํฉ์ ๋ง๋ ์นดํผ ์๋ ์ ๋ณ
|
451 |
- ๐ **๋ฐ์ดํฐ ๊ธฐ๋ฐ**: 37,671๊ฐ ์ค์ ์นดํผ์์ ํ์ตํ ํจํด
|
452 |
+
|
453 |
**3. ์ฐฝ์์ฑ ์กฐ์ :**
|
454 |
- **๋ณด์์ **: ์์ ํ ํด๋ผ์ด์ธํธ, ๊ฒ์ฆ๋ ์ ๊ทผ
|
455 |
- **๊ท ํ**: ์ผ๋ฐ์ ์ธ ํ๋ก์ ํธ (์ถ์ฒ!)
|
456 |
- **์ฐฝ์์ **: ํ์ ์ ๋ธ๋๋, ํ๊ฒฉ์ ์บ ํ์ธ
|
457 |
+
|
458 |
**4. ์ฐธ๊ณ ์นดํผ ํ์ฉ:**
|
459 |
- ์์ฑ๋ ์นดํผ์ ์ฐธ๊ณ ์นดํผ๋ฅผ ๋น๊ต ๋ถ์
|
460 |
- ํธ๋ ๋์ ํจํด ํ์
๊ฐ๋ฅ
|
|
|
473 |
st.sidebar.markdown("### ๐ง ๋๋ฒ๊ทธ ์ ๋ณด")
|
474 |
if 'embeddings' in locals():
|
475 |
st.sidebar.write(f"์๋ฒ ๋ฉ ๋ฉ๋ชจ๋ฆฌ: {embeddings.nbytes / (1024*1024):.1f}MB")
|
476 |
+
st.sidebar.write(f"Streamlit ๋ฒ์ : {st.__version__}")
|