Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +25 -16
src/streamlit_app.py
CHANGED
|
@@ -318,44 +318,53 @@ def generate_wordcloud(keywords_dict):
|
|
| 318 |
st.warning("워드클라우드 설치안되어 있습니다.")
|
| 319 |
return None
|
| 320 |
try:
|
| 321 |
-
|
|
|
|
| 322 |
width=800,
|
| 323 |
height=400,
|
| 324 |
-
background_color
|
| 325 |
-
colormap
|
| 326 |
max_font_size=150,
|
| 327 |
random_state=42
|
| 328 |
-
)
|
| 329 |
|
| 330 |
try:
|
| 331 |
import os
|
| 332 |
-
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 333 |
-
|
|
|
|
|
|
|
| 334 |
|
| 335 |
font_path = None
|
| 336 |
-
for
|
| 337 |
-
candidate = os.path.join(script_dir,
|
| 338 |
if os.path.exists(candidate):
|
| 339 |
font_path = candidate
|
| 340 |
break
|
| 341 |
-
|
|
|
|
| 342 |
if font_path:
|
| 343 |
-
wc= WordCloud(
|
| 344 |
font_path=font_path,
|
| 345 |
width=800,
|
| 346 |
height=400,
|
| 347 |
-
background_color
|
| 348 |
-
colormap
|
| 349 |
max_font_size=150,
|
| 350 |
random_state=42
|
| 351 |
).generate_from_frequencies(keywords_dict)
|
| 352 |
-
|
| 353 |
-
|
| 354 |
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
except Exception as e:
|
| 358 |
-
st.error(f"
|
| 359 |
return None
|
| 360 |
|
| 361 |
# 뉴스 분석 함수
|
|
|
|
| 318 |
st.warning("워드클라우드 설치안되어 있습니다.")
|
| 319 |
return None
|
| 320 |
try:
|
| 321 |
+
# 기본 WordCloud 객체 (폰트 경로 없이)
|
| 322 |
+
wc = WordCloud(
|
| 323 |
width=800,
|
| 324 |
height=400,
|
| 325 |
+
background_color='white',
|
| 326 |
+
colormap='viridis',
|
| 327 |
max_font_size=150,
|
| 328 |
random_state=42
|
| 329 |
+
)
|
| 330 |
|
| 331 |
try:
|
| 332 |
import os
|
| 333 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 334 |
+
# 사용자가 루트에 넣은 폰트 파일 이름을 지정합니다.
|
| 335 |
+
# 만약 다른 이름의 폰트를 사용했다면 이 부분을 수정해주세요. (예: "YourFontName.ttf")
|
| 336 |
+
possible_font_paths = ["NanumGothic.ttf"]
|
| 337 |
|
| 338 |
font_path = None
|
| 339 |
+
for path_segment in possible_font_paths:
|
| 340 |
+
candidate = os.path.join(script_dir, path_segment)
|
| 341 |
if os.path.exists(candidate):
|
| 342 |
font_path = candidate
|
| 343 |
break
|
| 344 |
+
|
| 345 |
+
# font_path가 성공적으로 찾아진 경우에만 폰트 경로를 포함하여 WordCloud 재생성
|
| 346 |
if font_path:
|
| 347 |
+
wc = WordCloud(
|
| 348 |
font_path=font_path,
|
| 349 |
width=800,
|
| 350 |
height=400,
|
| 351 |
+
background_color='white',
|
| 352 |
+
colormap='viridis',
|
| 353 |
max_font_size=150,
|
| 354 |
random_state=42
|
| 355 |
).generate_from_frequencies(keywords_dict)
|
| 356 |
+
else:
|
| 357 |
+
st.warning(f"지정된 한국어 글꼴 파일({', '.join(possible_font_paths)})을 스크립트 디렉터리에서 찾을 수 없습니다. 워드클라우드가 깨질 수 있습니다.")
|
| 358 |
|
| 359 |
+
except Exception as e:
|
| 360 |
+
print(f"글꼴 로딩 중 오류 발생: {str(e)}")
|
| 361 |
+
st.warning(f"글꼴 로딩 중 예상치 못한 오류가 발생했습니다: {str(e)}") # 사용자에게도 경고 표시
|
| 362 |
+
|
| 363 |
+
# 최종적으로 wc 객체 반환 (폰트가 적용되었거나, 기본 객체이거나)
|
| 364 |
+
return wc.generate_from_frequencies(keywords_dict) if isinstance(wc, WordCloud) else None
|
| 365 |
|
| 366 |
except Exception as e:
|
| 367 |
+
st.error(f"워드클라우드 생성 중 오류발생: {str(e)}")
|
| 368 |
return None
|
| 369 |
|
| 370 |
# 뉴스 분석 함수
|