Created
March 28, 2020 17:10
-
-
Save noahgift/de40ac37b3d51b22835c9260d41599bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wikipedia | |
from google.cloud import translate | |
def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", | |
project_id="YOUR_PROJECT_ID", language="fr"): | |
"""Translating Text.""" | |
client = translate.TranslationServiceClient() | |
parent = client.location_path(project_id, "global") | |
# Detail on supported types can be found here: | |
# https://cloud.google.com/translate/docs/supported-formats | |
response = client.translate_text( | |
parent=parent, | |
contents=[text], | |
mime_type="text/plain", # mime types: text/plain, text/html | |
source_language_code="en-US", | |
target_language_code=language, | |
) | |
# Display the translation for each input text provided | |
for translation in response.translations: | |
print(u"Translated text: {}".format(translation.translated_text)) | |
return u"Translated text: {}".format(translation.translated_text) | |
def translate_test(request): | |
"""Takes JSON Payload {"entity": "google"} | |
""" | |
request_json = request.get_json() | |
print(f"This is my payload {request_json}") | |
if request_json and 'entity' in request_json: | |
entity = request_json['entity'] | |
language = request_json['language'] | |
print(f"This is the entity {entity}") | |
print(f"This is the language {language}") | |
res = wikipedia.summary(entity, sentences=1) | |
trans=sample_translate_text(text=res, | |
project_id="cloudai-194723", language=language) | |
return trans | |
else: | |
return f'No Payload' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment