openfree's picture
Update app.py
74b6cd5 verified
raw
history blame
2.06 kB
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
@spaces.GPU(enable_queue=True, duration=50)
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)