Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
"""
gcloud functions call translate-wikipedia --data '{"entity":"facebook", "sentences": "20", "langua
ge":"ru"}'
"""
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,
)
print(f"You passed in this language {language}")
# Display the translation for each input text provided
for translation in response.translations:
print("Translated text: {}".format(translation.translated_text))
return "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"]
sentences = request_json["sentences"]
print(entity)
res = wikipedia.summary(entity, sentences=sentences)
trans = sample_translate_text(
text=res, project_id="cloudai-194723", language=language
)
return trans
else:
return f"No Payload"