Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,15 +58,78 @@ def analyze_text_sections(text):
|
|
| 58 |
|
| 59 |
@safe_process
|
| 60 |
def calculate_topic_correlations(topics):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
@safe_process
|
| 72 |
def perform_sentiment_analysis(text):
|
|
@@ -197,50 +260,8 @@ def process_results(results):
|
|
| 197 |
|
| 198 |
return formatted_topics
|
| 199 |
|
| 200 |
-
@spaces.GPU(enable_queue=True, duration=50)
|
| 201 |
-
def process_all_analysis(text):
|
| 202 |
-
try:
|
| 203 |
-
# 기본 주제 분석
|
| 204 |
-
raw_results = clf.suggest_topics(text)
|
| 205 |
-
topics = process_results(raw_results)
|
| 206 |
-
|
| 207 |
-
# 추가 분석
|
| 208 |
-
section_topics = analyze_text_sections(text)
|
| 209 |
-
corr_matrix, labels = calculate_topic_correlations(topics)
|
| 210 |
-
sentiments = perform_sentiment_analysis(text)
|
| 211 |
-
clusters = create_topic_clusters(topics)
|
| 212 |
-
|
| 213 |
-
# 차트 생성
|
| 214 |
-
bar_chart, radar_chart = create_main_charts(topics)
|
| 215 |
-
heatmap = create_correlation_heatmap(corr_matrix, labels)
|
| 216 |
-
evolution_chart = create_topic_evolution(section_topics)
|
| 217 |
-
gauge_chart = create_confidence_gauge(topics)
|
| 218 |
-
|
| 219 |
-
return {
|
| 220 |
-
'topics': topics,
|
| 221 |
-
'bar_chart': bar_chart,
|
| 222 |
-
'radar_chart': radar_chart,
|
| 223 |
-
'heatmap': heatmap,
|
| 224 |
-
'evolution': evolution_chart,
|
| 225 |
-
'gauge': gauge_chart,
|
| 226 |
-
'sentiments': sentiments.to_dict() if sentiments is not None else {},
|
| 227 |
-
'clusters': clusters.tolist() if clusters is not None else []
|
| 228 |
-
}
|
| 229 |
-
except Exception as e:
|
| 230 |
-
print(f"Analysis error: {str(e)}")
|
| 231 |
-
return {
|
| 232 |
-
'error': str(e),
|
| 233 |
-
'topics': [],
|
| 234 |
-
'bar_chart': go.Figure(),
|
| 235 |
-
'radar_chart': go.Figure(),
|
| 236 |
-
'heatmap': go.Figure(),
|
| 237 |
-
'evolution': go.Figure(),
|
| 238 |
-
'gauge': go.Figure(),
|
| 239 |
-
'sentiments': {},
|
| 240 |
-
'clusters': []
|
| 241 |
-
}
|
| 242 |
|
| 243 |
-
|
| 244 |
with gr.Blocks(title="고급 문서 주제 분석기") as demo:
|
| 245 |
gr.Markdown("## 📊 고급 문서 주제 분석기")
|
| 246 |
gr.Markdown("문서를 입력하면 다양한 분석 결과를 시각화하여 보여줍니다.")
|
|
@@ -283,10 +304,10 @@ with gr.Blocks(title="고급 문서 주제 분석기") as demo:
|
|
| 283 |
)
|
| 284 |
|
| 285 |
if __name__ == "__main__":
|
| 286 |
-
demo.queue(max_size=1)
|
| 287 |
demo.launch(
|
| 288 |
server_name="0.0.0.0",
|
| 289 |
server_port=7860,
|
| 290 |
-
share=
|
| 291 |
debug=True
|
| 292 |
)
|
|
|
|
| 58 |
|
| 59 |
@safe_process
|
| 60 |
def calculate_topic_correlations(topics):
|
| 61 |
+
try:
|
| 62 |
+
if not topics or len(topics) < 1:
|
| 63 |
+
return np.array([[1]]), ['No topics']
|
| 64 |
+
|
| 65 |
+
topic_scores = {}
|
| 66 |
+
for topic in topics:
|
| 67 |
+
if 'score_mean' in topic:
|
| 68 |
+
topic_scores[topic['label']] = topic['score_mean']
|
| 69 |
+
elif 'score' in topic: # score로도 확인
|
| 70 |
+
topic_scores[topic['label']] = topic['score'] / 100.0
|
| 71 |
+
|
| 72 |
+
if len(topic_scores) < 1:
|
| 73 |
+
return np.array([[1]]), ['No valid scores']
|
| 74 |
+
|
| 75 |
+
correlation_matrix = np.corrcoef(list(topic_scores.values()))
|
| 76 |
+
return correlation_matrix, list(topic_scores.keys())
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Correlation calculation error: {e}")
|
| 79 |
+
return np.array([[1]]), ['Error']
|
| 80 |
+
|
| 81 |
+
@spaces.GPU()
|
| 82 |
+
def process_all_analysis(text):
|
| 83 |
+
try:
|
| 84 |
+
# 기본 주제 분석
|
| 85 |
+
raw_results = clf.suggest_topics(text)
|
| 86 |
+
topics = process_results(raw_results)
|
| 87 |
+
|
| 88 |
+
# 추가 분석
|
| 89 |
+
section_topics = analyze_text_sections(text)
|
| 90 |
+
corr_matrix, labels = calculate_topic_correlations(topics)
|
| 91 |
+
sentiments = perform_sentiment_analysis(text)
|
| 92 |
+
clusters = create_topic_clusters(topics)
|
| 93 |
+
|
| 94 |
+
# 차트 생성
|
| 95 |
+
bar_chart, radar_chart = create_main_charts(topics)
|
| 96 |
+
heatmap = create_correlation_heatmap(corr_matrix, labels)
|
| 97 |
+
evolution_chart = create_topic_evolution(section_topics)
|
| 98 |
+
gauge_chart = create_confidence_gauge(topics)
|
| 99 |
+
|
| 100 |
+
results = {
|
| 101 |
+
'topics': topics,
|
| 102 |
+
'bar_chart': bar_chart,
|
| 103 |
+
'radar_chart': radar_chart,
|
| 104 |
+
'heatmap': heatmap,
|
| 105 |
+
'evolution': evolution_chart,
|
| 106 |
+
'gauge': gauge_chart,
|
| 107 |
+
'sentiments': sentiments.to_dict() if sentiments is not None else {},
|
| 108 |
+
'clusters': clusters.tolist() if clusters is not None else []
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
# 개별 결과 반환
|
| 112 |
+
return (
|
| 113 |
+
results, # JSON output
|
| 114 |
+
bar_chart, # plot1
|
| 115 |
+
radar_chart, # plot2
|
| 116 |
+
heatmap, # plot3
|
| 117 |
+
evolution_chart, # plot4
|
| 118 |
+
gauge_chart, # plot5
|
| 119 |
+
go.Figure() # plot6 (빈 감성 분석 차트)
|
| 120 |
+
)
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Analysis error: {str(e)}")
|
| 123 |
+
empty_fig = go.Figure()
|
| 124 |
+
return (
|
| 125 |
+
{'error': str(e), 'topics': []},
|
| 126 |
+
empty_fig,
|
| 127 |
+
empty_fig,
|
| 128 |
+
empty_fig,
|
| 129 |
+
empty_fig,
|
| 130 |
+
empty_fig,
|
| 131 |
+
empty_fig
|
| 132 |
+
)
|
| 133 |
|
| 134 |
@safe_process
|
| 135 |
def perform_sentiment_analysis(text):
|
|
|
|
| 260 |
|
| 261 |
return formatted_topics
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
|
| 265 |
with gr.Blocks(title="고급 문서 주제 분석기") as demo:
|
| 266 |
gr.Markdown("## 📊 고급 문서 주제 분석기")
|
| 267 |
gr.Markdown("문서를 입력하면 다양한 분석 결과를 시각화하여 보여줍니다.")
|
|
|
|
| 304 |
)
|
| 305 |
|
| 306 |
if __name__ == "__main__":
|
| 307 |
+
demo.queue(max_size=1)
|
| 308 |
demo.launch(
|
| 309 |
server_name="0.0.0.0",
|
| 310 |
server_port=7860,
|
| 311 |
+
share=True, # 공개 링크 생성을 위해 True로 설정
|
| 312 |
debug=True
|
| 313 |
)
|