[Practice] Make a Watson app with Python! # 1 [Language discrimination]

One of the courses in developer Works, [Create Python apps] with IBM Watson and IBM Bluemix](https://developer.ibm.com/courses/all/create-python-apps-ibm-watson-ibm-bluemix/).

Developer Works is recorded when you log in and complete the text, so it's a good idea to have an account. (Badges accumulate)

I did it in a Mac environment. To be honest, I think it's a little difficult to understand because I will describe what I did in almost all letters, so it may be better to refer to this as a supplement while watching the video linked above. The material talks about both the django version of the app and the flask version, but I'll just trace the django version. I think.

スクリーンショット 2017-07-15 10.08.19.png

Let's go!

Explore the development Environment

Building a virtual environment

Open a terminal. If Python is not on your PC, install Python. Check the python version. $ which python3 After confirming, create a virtual environment "d-env". The "3.6" part will be replaced with each version confirmed in which python3.

$ virtualenv d-env -p /Library/Frameworks/Python.framework/Versions/3.6/bin/python3


 Enter d-env.
```$source d-env/bin/activate```

## Download teaching materials
 Download watson-developer-cloud [python-primer-companion-code](https://github.com/watson-developer-cloud/python-primer-companion-code) on github.
 If you create two virtual environments for django and flask as shown in the video, it might be easier to create a parent folder that puts them together.
 After unzipping the file, it is better to rename it to a short name that is easy to use later, so rename the downloaded folder to "src" according to the guide.

## Text installation
 Open a terminal.

 Install src / episode-1 / django / src / requirements.txt
 Go to the directory where requirements.txt is and

#### **`$pip install -r requirements.txt (installation)`**
```txt (installation)


## Create a new project in a new directory
 Then go to src / episode1 / django / src.
```mkdir tmp```Create a directory called tmp with.
```django-admin startproject projwatson```You can create projwatson under the directory with.
 Go to projwatson and

#### **`python manage.The app will be launched on the local server with py run server.`**
```The app will be launched on the local server with py run server.

 However, the screen below appears and does not start up properly.
 ![スクリーンショット 2017-07-15 21.58.23.png](https://qiita-image-store.s3.amazonaws.com/0/141025/09357c21-8293-1653-d0e9-c9a117146329.png)
 If you add / wl / lang / to the end of the URL, a screen like that will appear.
 ![スクリーンショット 2017-07-15 15.14.37.png](https://qiita-image-store.s3.amazonaws.com/0/141025/d8c87505-ffae-eb4e-4735-d4c9a590a447.png)

 However, it doesn't work even if I input it yet.

 What's wrong with not having a clean start as in the text?

 https: // localhost: 8000 / admin is the Administration page.
 Let's also create an Administration privileged user to enter there.

 Get out of run mode with Control + C.

 SQLite3 is used by default in django, but we will create migration SQL from here.

#### **`python manage.py makemigrations`**
```py makemigrations

 (If nothing is done, the message “No changes detected,” will be displayed.)


#### **`python manage.py migrate`**
```py migrate


 Create an administrator user.

#### **`python manage.py createsuperuser`**
```py createsuperuser


#### **`python manage.py shell`**
```py shell


## Edit yml file
 Open the folder for this project that you first downloaded in a text editor. I use [Atom](https://atom.io/).
 Rewrite manifest.yml in src / episode1 / django / src / projwatson. Rewrite the domain to mybluemix.net and the name and host to the app name and its host name that you gave when you launched the python service from the catalog with bluemix.
 Similarly, rewrite domain, name and host in manifest.yml in src / episode1 / flask / src.

## Preparing to use Watson Service
 Go back to the terminal and install Requests.
```pip install requests```
 Install the Watson Developer Cloud SDK.
```pip install --upgrade watson-developer-cloud```
```pip freeze```

#### **`pip freeze > requirements.txt`**
```txt


 Open runtime.txt in Atom and rewrite it to the version of python you are using.

 Do the same for f-env (environment for flask).

Deploy to Bluemix
## Log in to bluemix
 * Perform the following work in the directory containing the manifest.yml file.
 The rest of the work is based on the assumption that the app has been deployed in the southern United States.
 Set endpoints in the Bluemix US internal region

#### **`cf api https://api.ng.bluemix.net`**

Log in to Bluemix. cf login -u (bluemix account id) After this, you will be asked for the password, so enter it. At this time, the password will not be displayed on the screen, so enter it carefully.

Now you can use `cf push``` to build your app on blumiex according to the contents of manifest.yml. That said, in my case, the version of python was 3.6.0 and it didn't match the version accepted by Python Buildpack when I followed this flow, so [Cloud Foundry Documentation](http://docs. According to cloudfoundry.org/buildpacks/python/), I just pushed with `cf push (app name) -b https://github.com/cloudfoundry/buildpack-python.git``` and it worked.

Add Watson services to your app

Add a project called watsonlanguage.

python manage.py startapp watsonlanguage



 Open the details of the corresponding app in Bluemix (click the name field from the dashboard)
 Click "New Connection" in the connection field.
 Select "Language Translator" from the catalog and select the binding destination from the connection field here.
 ![スクリーンショット 2017-07-15 23.30.35.png](https://qiita-image-store.s3.amazonaws.com/0/141025/a2fbd1c2-5b3b-3c2b-2c58-43252acba8ea.png)
 It will be available after restage.

 Click the connection field of the app you are creating and click "Display Credentials". Since the user ID and password for using the translation function are written, in the wl.py file
try:
language_translation = LanguageTranslation(username='Your watson language translation service username key',
password='sevice password key')
 Let's embed it in the part of.
 ![スクリーンショット 2017-07-15 23.33.43.png](https://qiita-image-store.s3.amazonaws.com/0/141025/92819593-4e63-da8d-04ff-f56a38631098.png)

 I have to rewrite the contents of some files.
 First, rewrite the watson-developer-cloud field in requirements.txt of src / episode1 / django / src / projwatson to 0.26.1 to update it.
 Also, the service that was Watson Language Translation when this text was created has been renamed to Watson Language Translator, so it's in the early part of wl.py in src / episode1 / django / src / projwatson / watsonlanguage / views.
```from watson_developer_cloud import languagetranslationv2 as languagetranslation```To```from watson_developer_cloud import languagetranslatorv2 as languagetranslation```Let's rewrite it to.
 Then, when you access (app name) / wl / lang from your browser, you should see an app that can determine which language the string you typed is in!
 (In the example below, it is determined to be German)
 ![スクリーンショット 2017-07-16 1.18.37.png](https://qiita-image-store.s3.amazonaws.com/0/141025/50cf7315-2d14-356e-9cf2-b8faf15c06f2.png)

 Well, this time it's over.
 Thank you for your hard work.





Recommended Posts

[Practice] Make a Watson app with Python! # 1 [Language discrimination]
[Practice] Make a Watson app with Python! # 2 [Translation function]
Make a desktop app with Python with Electron
Make a fortune with Python
Let's make a GUI with python.
Make a recommender system with python
Let's make a graph with python! !!
Let's make a shiritori game with Python
Daemonize a Python web app with Supervisor
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Make Qt for Python app a desktop app
I tried a functional language with Python
Make a scraping app with Python + Django + AWS and change jobs
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Try to make a Python module in C language
Try to make a command standby tool with python
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
I made a net news notification app with Python
Let's make a simple game with Python 3 and iPhone
Make a breakpoint on the c layer with python
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
100 Language Processing with Python Knock 2015
Make Puyo Puyo AI with Python
Make a bookmarklet in Python
Create a directory with python
Make a fire with kdeplot
Let's make a websocket client with Python. (Access token authentication)
Associate Python Enum with a function and make it Callable
Experiment to make a self-catering PDF for Kindle with Python
Make a Mac menu bar resident weather app with rumps!
[Python] Make a simple maze game with Pyxel-Make enemies appear-
[5th] I tried to make a certain authenticator-like tool with python
[Python] What is a with statement?
[Python] Make a graph that can be moved around with Plotly
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Make Echolalia LINEbot with Python + heroku
If you know Python, you can make a web application with Django
Make apache log csv with python
Rubyist tried to make a simple API with Python + bottle + MySQL
[2nd] I tried to make a certain authenticator-like tool with python
100 Language Processing Knock with Python (Chapter 1)
Make a sound with Jupyter notebook
Make multiple numerical elevation data into a single picture with Python
Solve ABC166 A ~ D with Python
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~