Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. APIKEY="key" # Replace with your API key
  2. !pip install --upgrade google-api-python-client
  3.  
  4. # running Translate API
  5. from googleapiclient.discovery import build
  6. service = build('translate', 'v2', developerKey=APIKEY)
  7.  
  8. # use the service
  9. inputs = ['is it really this easy?', 'amazing technology', 'wow']
  10. outputs = service.translations().list(source='en', target='fr', q=inputs).execute()
  11. # print outputs
  12. for input, output in zip(inputs, outputs['translations']):
  13. print("{0} -> {1}".format(input, output['translatedText']))
  14.  
  15.  
  16. # Running Vision API
  17. import base64
  18. IMAGE="gs://cloud-training-demos/vision/sign2.jpg"
  19. vservice = build('vision', 'v1', developerKey=APIKEY)
  20. request = vservice.images().annotate(body={
  21. 'requests': [{
  22. 'image': {
  23. 'source': {
  24. 'gcs_image_uri': IMAGE
  25. }
  26. },
  27. 'features': [{
  28. 'type': 'TEXT_DETECTION',
  29. 'maxResults': 3,
  30. }]
  31. }],
  32. })
  33. responses = request.execute(num_retries=3)
  34. print(responses)
  35. inputs=[foreigntext]
  36. outputs = service.translations().list(source=foreignlang, target='en', q=inputs).execute()
  37. # print(outputs)
  38. for input, output in zip(inputs, outputs['translations']):
  39. print("{0} -> {1}".format(input, output['translatedText']))
  40.  
  41.  
  42. lservice = build('language', 'v1beta1', developerKey=APIKEY)
  43. quotes = [
  44. 'To succeed, you must have tremendous perseverance, tremendous will.',
  45. 'It’s not that I’m so smart, it’s just that I stay with problems longer.',
  46. 'Love is quivering happiness.',
  47. 'Love is of all passions the strongest, for it attacks simultaneously the head, the heart, and the senses.',
  48. 'Nice enrichable place, good coffe and baked goods, overall good atmosphere.',
  49. 'What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or in the holy name of liberty or democracy?',
  50. 'When someone you love dies, and you’re not expecting it, you don’t lose her all at once; you lose her in pieces over a long time — the way the mail stops coming, and her scent fades from the pillows and even from the clothes in her closet and drawers. '
  51. ]
  52. for quote in quotes:
  53. response = lservice.documents().analyzeSentiment(
  54. body={
  55. 'document': {
  56. 'type': 'PLAIN_TEXT',
  57. 'content': quote
  58. }
  59. }).execute()
  60. polarity = response['documentSentiment']['polarity']
  61. magnitude = response['documentSentiment']['magnitude']
  62. print('POLARITY=%s MAGNITUDE=%s for %s' % (polarity, magnitude, quote))
  63.  
  64. # Running Speech API
  65. sservice = build('speech', 'v1', developerKey=APIKEY)
  66. response = sservice.speech().recognize(
  67. body={
  68. 'config': {
  69. 'languageCode' : 'en-US',
  70. 'encoding': 'LINEAR16',
  71. 'sampleRateHertz': 16000
  72. },
  73. 'audio': {
  74. 'uri': 'gs://cloud-training-demos/vision/audio.raw'
  75. }
  76. }).execute()
  77. print(response)
  78. print(response['results'][0]['alternatives'][0]['transcript'])
  79. print('Confidence=%f' % response['results'][0]['alternatives'][0]['confidence'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement