-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, 3 October 2021

How to get started with the Google Translate API

Even if most of us can’t travel as we once did, the world is a more accessible place, at least online. Business people may not be attending international conferences or flying around the world for meetings so much. But in many respects, businesses are finding that they can access and develop foreign markets by localizing their websites and apps to speak the language of locals and adapt to their standards. Developers can play an integral role in the localization process.

How localization tools are driving innovation in AI-driven APIs

Developers can hop on this trend, increase the value of their software, and expand online on capabilities to their apps. Google has emerged as a leader in translation algorithms in the past decade, leveraging advances in AI-driven neural network tech. This powers Google Translate, well-known to businesses for their companies or clients. They can do so readily by adding auto-translators Google Translate, well-known to businesses and consumers, an app that has made an impact on the translation profession as we know it. We’ll explore here how developers can add Google-powered translations to their apps by leveraging the powers of the company’s Translate API. Localization tools, which drive greater accuracy in translation mechanics, is also necessary for the translator to work more efficiently.

Before diving into the software weeds, it’s worth noting alternatives to the Google Translate API route. Third-party conversion tools like Zapier and IFTTT let you link your software workflow to auto-translation modules via webhooks and web services, with a minimum of coding. Even a tech-savvy non-programmer should be able to implement these solutions. The main drawback, however, is that you are likely to settle for a translation engine inferior to the one offered by Google.

What does Google offer for translation?

Google is a pioneer in both machine language and machine learning—the two L-words representing two sides of the same coin. Language needs to be learned, and that learning is achievable by mastery of a natural language. AI-driven mastery these days is driven by neural machines or NMT in 2016, bringing a “paradigm shift” in translation tech. From that year forward, NMT has been the preferred method of translating.

Back in 2006, Google started training its translation algorithm by digesting tens of millions of words extracted from translated documents of the European Union parliament and the United Nations. Today Google confronts competition from Facebook, which is leveraging the learnings from comments and posts by its 2 billion users to translate more casual conversations, including rendering LOL and WTF in scores of languages. NMT continues to be a way to go.

Happily, the language learning process didn’t stop with bureaucratese and Emoji. Google Translate today supports over 100 languages, several dozen with voice support. You can talk in one language and get the translation vocalized in another, usually with a choice of voice. And, as we will see, machine learning has been productized so that you can effectively translate a domain-specific language of your own.

Getting started with Google Translate API

Google promotes its API as fast and dynamic, adaptable to diverse content needs. The company markets not just to professional coders but to a broader spectrum of users, including those with “limited machine learning expertise” who can quickly “create high-quality, production-ready models.”

For the latter, you can just upload translated language pairs (a structured list of words/phrases with their translations) and AutoML Translation will train a custom translation model. The workflow allows either customized-by-the-client or pre-trained (by Google) inputs. To translate an English product description into French, Korean, and Portuguese, for example, you could customize a Google AutoML model for French and rely on an off-the-shelf pre-trained model for Korean and Portuguese. Then you simply upload your English HTML file to Google Cloud Storage and send a batch request to the Translation API pointing to your AutoML and the pre-trained models. Google’s AutoML Translation will then output your HTML in three separate language files to your Cloud Storage.

Training is key, but the initial model is pre-trained to render 100+ languages. And if you have a domain-specific lexicon (medical or legal terms, for example) these require just a little more training and tweaking of the basic API, if they don’t already exist. A Glossary lets users “wrap” proprietary terminology not to be translated (like brand and product names) to ensure they stay intact during translation. There is also built-in support Media Translation API, which handles real-time, low latency streaming of audio translations. 

The process is essentially three steps: Upload a language pair. Train AutoML. Evaluate.

This translation power is not free but the pricing is fair. Typically, you’ll be using Google’s Translate API and its Media Translation API (if you need voice support). You’ll need the AutoML service only if you need to train more language pairs. 

The fee for the Translate API is $20 per million characters. The Media Translation API will set you back $0.068 to $0.084 per minute. AutoML is a bit pricier, costing $45 per hour for training a language pair, to a max of $300/pair. Pay only for what you use, as you use it. (Google is patient: it wants to get you hooked, so it throws in free processing as you get up to speed, with a full year to practice before needing to pay up.)

Setting up for Your First Translation

The RESTful Translate API is the easiest way to get started. Google offers a basic and advanced setup. You can do this with localization tools, but you can also do it manually. If you’ve set up any Google API service, you’re probably comfortable with the drill and may already have a Cloud Console Account. Assuming this is true, the next things you need to do, if you haven’t already, are: 

  • Create or select your project.
  • Enable the Cloud Translation API.
  • Create a service account.
  • Download your private key in JSON format. Keep the full path to this file for the next step. 

Go to the shell prompt on your Mac OS X, Linux, or Windows (Powershell) system and set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your JSON service account key using the following commands. This variable only applies to the current shell session. If you open a new session, you’ll need to reset this variable, replacing [PATH] with the path of the JSON file with your key.

If you’re using Linux or macOS:

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

For Windows, in PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

Or, from a command prompt: 

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

Then install and initialize Google’s Cloud SDK. Depending on which operating system you’re using, the Cloud SDK may have a dependency on a version of Python that isn’t installed on your system. So be sure to double-check the Cloud SDK documentation to ensure the appropriate version of Python is installed.

Executing Your First Translation

Make a Translation API Request with a REST call using the v2 translate method.

Use curl to make your request to the https://translation.googleapis.com/language/translate/v2 endpoint.

The command includes JSON with (1) the text to be translated (q), (2) the language to translate from (source), and (3) the language to translate to (target).

Source and target languages are identified with ISO-639-1 codes. In this example, the source language is English (en), the target is French (fr). The query format is plain “text”.

The sample curl command uses the gcloud auth application-default print-access-token command to get an authentication token.

curl -s -X POST -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    --data "{
  'q': 'The quick brown fox jumps over the lazy dog',
  'source': 'en',
  'target': 'fr',
  'format': 'text'
}" "https://translation.googleapis.com/language/translate/v2"

The response should resemble the following:

{
  "data": {
    "translations": [
      {
        "translatedText": "Le renard brun rapide saute par-dessus le chien paresseux"
      }
    ]
  }
}

Congratulations! You’ve sent your first request to the Cloud Translation API!

Next steps in the translation process

For most apps, you can rely on one of the over 100 language pairs already trained and tested.  (If the pair you require is not available, or you need a custom translation with the AutoML training module.) The full process is as follows:

  1. Create a file containing the desired language pairs, using the CURL example above. Choose source and target languages from the list here (e.g., “en” or “fr”).
  2. Write code that reads the content of your website and makes a REST call to the Cloud Translation API (including a parameter pointing to your model and then producing a translated version of that text). 
  3. Create a new page in your content management system to contain and then display the translated text. Even better, if your CMS is programmable (either directly or by way of API), improve the code by automating this step.
  4. Configure your CMS and website to display the appropriate pages when a specific language is selected by your site’s end users.

Client libraries are currently available for seven popular programming languages – C#, Go, Java, Node.js, PHP, Python, and Ruby. Just install the library of your choice. Go to Translation Client Libraries for installation instructions.


Monday, 24 June 2019

A Framework or a CMS? What is better to choose?

There are lot of articles who are talking about Frameworks and CMS systems. What to choose? Which is the best?

However I wanted to write an article from a totally different perspective. First of all I want to make sure that developers and customers understand the difference between the terminology “CMS” and “Framework” nowadays.

Some theory first

When we use the terminology “Framework” we are more often talking about Web application framework . A web application framework is simply based on coding. If you are a programmer or a web developer that loves coding you often choose a Web application framework for your projects.
A CMS is a Content management framework and it is based on the modules rather than code. Anyone can handle a CMS as it doesn’t require any coding knowledge. Of course programmers can also choose a CMS as they can do their job faster.

There are lot of fights…

There are lot of misunderstandings about what a framework and what a CMS can do. Lot of fans that use CMS they are saying that “We can do everything with a CMS” and the fans of the frameworks are “No we can do everything with a framework” and so on… As a fan of frameworks of course I will have the same opinion but actually this is not true! The truth is that as we use the same base(PHP,RoR,python,. NET… e.t.c.), we all can do everything. Even the myth that CMS doesn’t use a good structure isn’t truth. For example Drupal 8 is based on Symfony framework, a very good object oriented web application framework.

So what do I choose?

Nowadays the CMS is more popular than the Web application Framework as it is really easy to use it. Even if you don’t have programming knowledge you can install and create a whole website with just a CMS. So what to choose?
It is always about four basic things that you have to think before you choose a framework or a CMS in my personal experience:
1. How much time you want to spend?
2. What do you want to do and how flexible has to be this project in the future?
3. How much money (if it matters) you will take after the end of the project.
4. What the hosting environment will be
For the customer it doesn’t really matter what tools you will use. The thing that matters is to have their job done. So let’s have an example:
I will explain to you my choices about two personal projects and one normal project. This blog and www.grocerycrud.com. First of all I am a PHP web developer so I can choose either a PHP framework, either a PHP CMS.
So for this blog that you are reading right now, I used WordPress as:
1. I wanted to spend as less time as I could as it is a simple blog
2. I didn’t want to do anything extreme. Just the usual. SEO, caching, comments, nice design and less spams!
3. I will not make any profit for the creation of the project. So I don’t have as a criteria the money.
4. Free to choose.
For grocerycrud.com I choose Codeigniter framework as:
1. I wanted to spend a normal time to do it. Not too much as this is a personal project
2. I wanted to be 100% extendable as I am changing the structure all the time with new ideas. For example now I am preparing a new mini website inside the grocerycrud.com (www.grocerycrud.com/image-crud).
3. Still the money doesn’t count here as it is a personal project
4. Free to choose
For www.dailyfx.gr I chose Codeigniter framework as
1. I had only 1 month deadline for me to do it from scratch
2. It was a system that the clients wanted some extremely custom webpages (such as with an internal connection with java applet charts). I didn’t choose wordpress or drupal as it would require more time to customize all of these unique needs.
3. For money nothing more than my normal monthly salary. So the money was not something to motivate me
4. Just another shared hosting

I want to investigate more

As I said only in my personal experience are 4 simple things that I have in mind but this is only because the outsource projects that I have are often small projects so that’s why I have only these four things in mind. Of course if I want to investigate more time I think a Web Application framework is the solution and I can give you much more criteria to have in mind. As the Symfony webpage says:
You’re making progress and that’s a good thing! You already know that you are going to use a framework to develop your site or your application. But which one? Here is a checklist that you can use to avoid making a mistake:

So there are 10 criteria for choosing the correct framework. You can take a look at this article as it is really straight forward about what are the real criteria to actually choose a framework. So if you really want to investigate to a framework for your big projects, have in mind that it is a long-term commitment, so make sure that you make the right selection… for you!

What about a CMS?

On the other side it is not so bad to know programming and also be a “CMS guy”. The bad think about it is that they are many “CMS guys” that don’t have any programming knowledge and think that they can do everything with a CMS. This is also a myth! When I was talking before that with a CMS you can do everything as you have the same base it also depends if you have some basic programming skills of course. If not then you can do everything as far as the programmers/web developers let you have it. So my opinion is that if you are a “CMS guy” and you don’t have any programming knowledge it is good to start learning some basic stuff and try to create your custom plugins for the CMS from scratch.

And finally a conclusion…

So as a conclusion I have to say that it always depends of what you want to do and how much time you have to deal with it. I believe that every tool is build for a reason so as many tools you know (frameworks or CMS), so flexible you are as a professional developer. So it doesn’t worth it to be a CMS or a Framework fan. Just choose the best tool for you situation and that’s it.

Rank

seo