Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,24 +2,49 @@ import json
|
|
2 |
import gradio as gr
|
3 |
import spaces
|
4 |
import wbgtopic
|
5 |
-
|
|
|
6 |
|
7 |
SAMPLE_TEXT = """A growing literature attributes gender inequality in labor market outcomes in part to the reduction in female labor supply after childbirth, the child penalty..."""
|
8 |
|
9 |
clf = wbgtopic.WBGDocTopic()
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def process_results(results):
|
12 |
-
# 결과를 보기 좋게 가공
|
13 |
if not results or not results[0]:
|
14 |
-
return []
|
15 |
|
16 |
-
# 첫 번째 문서의 결과만 사용 (단일 문서 기준)
|
17 |
topics = results[0]
|
18 |
-
|
19 |
-
# 상위 5개 주제만 선택
|
20 |
top_topics = sorted(topics, key=lambda x: x['score_mean'], reverse=True)[:5]
|
21 |
|
22 |
-
# 각 주제의 점수를 퍼센트로 변환하고 한글로 번역
|
23 |
formatted_topics = []
|
24 |
for topic in top_topics:
|
25 |
formatted_topic = {
|
@@ -29,7 +54,10 @@ def process_results(results):
|
|
29 |
}
|
30 |
formatted_topics.append(formatted_topic)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
@spaces.GPU(enable_queue=True, duration=50)
|
35 |
def fn(inputs):
|
@@ -53,13 +81,17 @@ with gr.Blocks(title="문서 주제 분석기") as demo:
|
|
53 |
submit_btn = gr.Button("분석 시작", variant="primary")
|
54 |
|
55 |
with gr.Row():
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# 이벤트 연결
|
59 |
submit_btn.click(
|
60 |
fn=fn,
|
61 |
inputs=[text],
|
62 |
-
outputs=output
|
63 |
)
|
64 |
|
65 |
demo.launch(debug=True)
|
|
|
2 |
import gradio as gr
|
3 |
import spaces
|
4 |
import wbgtopic
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
from topic_translator import translate_topics
|
7 |
|
8 |
SAMPLE_TEXT = """A growing literature attributes gender inequality in labor market outcomes in part to the reduction in female labor supply after childbirth, the child penalty..."""
|
9 |
|
10 |
clf = wbgtopic.WBGDocTopic()
|
11 |
|
12 |
+
def create_chart(topics):
|
13 |
+
# 데이터 준비
|
14 |
+
labels = [t['label'] for t in topics]
|
15 |
+
scores = [t['score'] for t in topics]
|
16 |
+
confidence = [t['confidence'] for t in topics]
|
17 |
+
|
18 |
+
# 막대 차트 생성
|
19 |
+
fig = go.Figure()
|
20 |
+
|
21 |
+
# 주요 막대 추가
|
22 |
+
fig.add_trace(go.Bar(
|
23 |
+
x=labels,
|
24 |
+
y=scores,
|
25 |
+
name='관련도',
|
26 |
+
marker_color='rgb(55, 83, 109)'
|
27 |
+
))
|
28 |
+
|
29 |
+
# 차트 레이아웃 설정
|
30 |
+
fig.update_layout(
|
31 |
+
title='문서 주제 분석 결과',
|
32 |
+
xaxis_title='주제',
|
33 |
+
yaxis_title='관련도 (%)',
|
34 |
+
yaxis_range=[0, 100],
|
35 |
+
height=500,
|
36 |
+
font=dict(size=14)
|
37 |
+
)
|
38 |
+
|
39 |
+
return fig
|
40 |
+
|
41 |
def process_results(results):
|
|
|
42 |
if not results or not results[0]:
|
43 |
+
return [], None
|
44 |
|
|
|
45 |
topics = results[0]
|
|
|
|
|
46 |
top_topics = sorted(topics, key=lambda x: x['score_mean'], reverse=True)[:5]
|
47 |
|
|
|
48 |
formatted_topics = []
|
49 |
for topic in top_topics:
|
50 |
formatted_topic = {
|
|
|
54 |
}
|
55 |
formatted_topics.append(formatted_topic)
|
56 |
|
57 |
+
# 차트 생성
|
58 |
+
chart = create_chart(formatted_topics)
|
59 |
+
|
60 |
+
return formatted_topics, chart
|
61 |
|
62 |
@spaces.GPU(enable_queue=True, duration=50)
|
63 |
def fn(inputs):
|
|
|
81 |
submit_btn = gr.Button("분석 시작", variant="primary")
|
82 |
|
83 |
with gr.Row():
|
84 |
+
# 차트를 보여줄 Plot 컴포넌트 추가
|
85 |
+
plot = gr.Plot(label="주제 분석 차트")
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
output = gr.JSON(label="상세 분석 결과")
|
89 |
|
90 |
# 이벤트 연결
|
91 |
submit_btn.click(
|
92 |
fn=fn,
|
93 |
inputs=[text],
|
94 |
+
outputs=[output, plot]
|
95 |
)
|
96 |
|
97 |
demo.launch(debug=True)
|