Spaces:
Running
Running
| import json | |
| import gradio as gr | |
| import spaces | |
| import wbgtopic | |
| from topic_translator import translate_topics # ์๋ก ๋ง๋ค ํ์ผ์ ๋๋ค | |
| 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...""" | |
| clf = wbgtopic.WBGDocTopic() | |
| def process_results(results): | |
| # ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ๊ฐ๊ณต | |
| if not results or not results[0]: | |
| return [] | |
| # ์ฒซ ๋ฒ์งธ ๋ฌธ์์ ๊ฒฐ๊ณผ๋ง ์ฌ์ฉ (๋จ์ผ ๋ฌธ์ ๊ธฐ์ค) | |
| topics = results[0] | |
| # ์์ 5๊ฐ ์ฃผ์ ๋ง ์ ํ | |
| top_topics = sorted(topics, key=lambda x: x['score_mean'], reverse=True)[:5] | |
| # ๊ฐ ์ฃผ์ ์ ์ ์๋ฅผ ํผ์ผํธ๋ก ๋ณํํ๊ณ ํ๊ธ๋ก ๋ฒ์ญ | |
| formatted_topics = [] | |
| for topic in top_topics: | |
| formatted_topic = { | |
| 'label': translate_topics.get(topic['label'], topic['label']), | |
| 'score': round(topic['score_mean'] * 100, 1), | |
| 'confidence': round((1 - topic['score_std']) * 100, 1) | |
| } | |
| formatted_topics.append(formatted_topic) | |
| return formatted_topics | |
| def fn(inputs): | |
| raw_results = clf.suggest_topics(inputs) | |
| return process_results(raw_results) | |
| # Gradio ์ธํฐํ์ด์ค ์์ฑ | |
| with gr.Blocks(title="๋ฌธ์ ์ฃผ์ ๋ถ์๊ธฐ") as demo: | |
| gr.Markdown("## ๐ ๋ฌธ์ ์ฃผ์ ๋ถ์๊ธฐ") | |
| gr.Markdown("๋ฌธ์๋ฅผ ์ ๋ ฅํ๋ฉด ๊ด๋ จ๋ ์ฃผ์ ๋ค์ ๋ถ์ํ์ฌ ๋ณด์ฌ์ค๋๋ค.") | |
| with gr.Row(): | |
| text = gr.Textbox( | |
| value=SAMPLE_TEXT, | |
| label="๋ถ์ํ ํ ์คํธ", | |
| placeholder="์ฌ๊ธฐ์ ๋ถ์ํ ํ ์คํธ๋ฅผ ์ ๋ ฅํ์ธ์", | |
| lines=5 | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("๋ถ์ ์์", variant="primary") | |
| with gr.Row(): | |
| output = gr.JSON(label="๋ถ์ ๊ฒฐ๊ณผ") | |
| # ์ด๋ฒคํธ ์ฐ๊ฒฐ | |
| submit_btn.click( | |
| fn=fn, | |
| inputs=[text], | |
| outputs=output | |
| ) | |
| demo.launch(debug=True) |