|
import os |
|
import gradio as gr |
|
from google.cloud import translate_v2 as translate |
|
from nltk import download |
|
from nltk.tokenize import word_tokenize |
|
from nltk.corpus import stopwords |
|
from nltk import pos_tag |
|
|
|
|
|
download('punkt') |
|
download('averaged_perceptron_tagger') |
|
download('stopwords') |
|
|
|
def extract_keywords(text): |
|
|
|
tokens = word_tokenize(text) |
|
|
|
tokens = [word for word in tokens if word.isalnum() and word.lower() not in stopwords.words('english')] |
|
|
|
tagged = pos_tag(tokens) |
|
|
|
keywords = [word for word, tag in tagged if tag in ['NN', 'NNP', 'NNS', 'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ']] |
|
return ' '.join(keywords) |
|
|
|
def translate_and_extract_keywords(text, api_key): |
|
|
|
translate_client = translate.Client(credentials=translate.Credentials(api_key)) |
|
|
|
result = translate_client.translate(text, target_language='en') |
|
translated_text = result['translatedText'] |
|
|
|
return extract_keywords(translated_text) |
|
|
|
|
|
interface = gr.Interface( |
|
fn=translate_and_extract_keywords, |
|
inputs=[ |
|
gr.Textbox(lines=2, placeholder="한글 문장을 입력하세요..."), |
|
gr.Textbox(label="Google Cloud API 키") |
|
], |
|
outputs="text", |
|
title="한글 문장을 영어 키워드로 번역 및 추출", |
|
description="한글 문장과 Google Cloud API 키를 입력하면, 그 의미가 포함된 영어 키워드를 추출하여 출력합니다." |
|
) |
|
|
|
|
|
interface.launch(share=True) |
|
|