|
import gradio as gr |
|
from google.cloud import translate_v2 as translate |
|
from google.oauth2 import service_account |
|
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): |
|
|
|
credentials_info = {'type': 'service_account', 'project_id': 'your-project-id', 'private_key_id': 'your-private-key-id', 'private_key': api_key, 'client_email': 'your-service-account-email', 'client_id': 'your-client-id', 'auth_uri': 'https://accounts.google.com/o/oauth2/auth', 'token_uri': 'https://oauth2.googleapis.com/token', 'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs', 'client_x509_cert_url': 'your-cert-url'} |
|
credentials = service_account.Credentials.from_service_account_info(credentials_info) |
|
translate_client = translate.Client(credentials=credentials) |
|
|
|
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) |
|
|