More Related Content
Similar to IBM Watson & PHP, A Practical Demonstration (20)
More from Clark Everetts (7)
IBM Watson & PHP, A Practical Demonstration
- 1. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 1
IBM Watson & PHP:
A Practical
Demonstration
Clark Everetts, Rogue Wave Software
php[tek] 2018
31 May 2018
Atlanta Georgia
- 2. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 2
Agenda
• The Goal
• Possible ML use cases
• Sample database
• IBM Cloud brief overview
• Text to Speech :-)
• Speech to Text :-/
• Initial Chatbot with Watson Assistant
• Next Steps / Further Research
- 3. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 3
My end goal
• Can you find how much of your inventory is on hand or
allocated?
• Which product lines your customers are buying, and in what
volume?
• Hands-free?
• How can Watson services help you:
• understand how much stock is available
• who is buying and selling it
• spot trends
• Key concepts and points:
• speech-to-text
• text-to-speech
• interactive application on IBM i
• powered by Watson in the cloud
• data residing on the IBM I – not stored in cloud
- 4. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 4
Machine Learning Use-Case: Code Quality
Higher quality libraries are
• Easier to understand
• Easier to maintain
• Easier to test
• Get updated sooner
• Bugfixes
• New features
• Don’t break as easily or as often
• Greater interest/participation from dev team and users
*Campbell County Kentucky Public Library
- 5. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 5
Code Quality: Cognitive vs. Static Analysis
• PHPLOC – size metrics, complexity, dependencies, structure
– https://github.com/sebastianbergmann/phploc
• Copy/Paste Detector – finds duplicated code (exact dups)
– https://github.com/sebastianbergmann/phpcpd
• PHP Depend -
– https://pdepend.org/
• PHP Mess Detector
– https://phpmd.org/
• Phpstan
– https://github.com/phpstan/phpstan
• Phan
– https://github.com/phan/phan
• Exakat
– https://www.exakat.io/
• PHPMetrics
– http://www.phpmetrics.org/
Watson:
?
- 6. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 6
Know anybody with a warehouse?
Product picking system
Text-to-speech and db-connectivity are key.
- 7. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 7
BIRT Sample Database
BIRT – Business Intelligence Reporting Tools
Eclipse Project, has an interesting sample database
Classic marine, car, truck, and train models.
DB Tables:
Offices
Employees
Customers
Orders
Payments
Order Details
Products
Product Lines
- 8. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 8
BIRT Database Schema
- 9. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 9
IBM Bluemix Cloud
https://console.bluemix.net/ (login with IBM id)
Stay informed!!
https://www.ibm.com/blogs/bluemix/
- 10. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 10
IBM Cloud Catalog Categories
Infrastructure
Compute
Storage
Network
Security
Containers
VMware
Platform
Boilerplates
APIs
Application Services
Blockchain
Cloud Foundry Apps
Data & Analytics
DevOps
Finance
Functions
Integrate
Internet of Things
Mobile
Network
Security
Storage
Watson
- 11. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 11
IBM Cloud Catalog: Watson
- 12. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 12
Choosing the right services
• Several related / overlapping services
• Need good understanding
• Application
• functionality, scope, breadth,
depth
• Nature of data
• static / dynamic
• structure & contents
• size
• Watson services
• what each provides, &
doesn’t
• setup, (re)training, update
• initial and ongoing cost
- 13. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 13
IBM Cloud Catalog: Watson
- 14. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 14
Challenges
• Unstructured speech to structured db queries
• Correctly parsing the resulting text
• Building correct SQL query from text
• Knowing when not to attempt a query
Question: “Who are my customers?”
Appropriate Query: Select customerName from Customers
join Employees on salesrepEmployeeNumber = employeeNumber
where salesrepEmployeeNumber = X;
• Intention, Context, Memory
• Q1. Who are my customers? (company name or point of contact)
• Q2. Where are they located? (city, state, province, sales region,
country?)
• Q3. Who has not ordered in three months?
“my” (authenticated user), “they” (results of Q1), “who” (results of Q1, not
Q2)
- 15. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 15
Text-to-Speech: Overview
• Quickly converts written text to natural-sounding speech
• Voice-driven and screenless interfaces
• Anywhere where audio is the preferred method of output
• home automation solutions
• assistance tools for the vision-impaired
• reading text and email messages aloud to drivers
• video script narration and voice over
• reading-based educational tools
- 16. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 16
Text-to-Speech: Features
• HTTP and WebSocket interfaces
• SSML (Speech Synthesis Markup Language)
• Audio formats
• Ogg format with Opus or Vorbis codec
• Waveform Audio File Format (WAV)
• Free Lossless Audio Codec (FLAC)
• MP3 (Motion Picture Experts Group, or MPEG)
• Web Media (WebM) with the Opus or Vorbis codec
• Linear 16-bit Pulse-Code Modulation (PCM)
• mu-law (u-law)
• basic audio
• Languages
• English, French, German, Italian, Japanese, Spanish, and
Brazilian Portuguese
- 17. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 17
Text-to-Speech: Features (cont’d)
• Voices -
• one male or female voice, sometimes both, for each
language
• US and UK English and Castilian, Latin American, North
American Spanish
• uses appropriate cadence and intonation
• Expressiveness
• Add’l SSML element to indicate a speaking style
• GoodNews, Apology, Uncertainty
• currently US English Allison voice only
• Voice transformation
• pitch, rate, timbre
• built-in transformations for “young” and “soft”
• Customization of pronunciation
• International Phonetic Alphabet (IPA)
• IBM Symbolic Phonetic Representation (SPR)
• Text-to-speech demo https://text-to-speech-
demo.ng.bluemix.net/
- 18. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 18
Text-to-Speech: Initial Test
<?php
namespace ClarkPhpWatsonDemo;
use GuzzleHttpClient;
use ZendDiactorosRequest;
use ZendDiactorosUri;
use ZendDiactorosResponse;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../src/classic-models.php';
call_user_func(function () {
$config = getConfig()['text-to-speech'];
$client = new Client();
$request = (new Request())
->withUri((new Uri("{$config['uri']}/synthesize"))
->withQuery('voice=en-US_AllisonVoice'))
->withMethod('POST')
->withAddedHeader('Authorization',
'Basic ' .
base64_encode("{$config['username']}:{$config['password']}"))
->withAddedHeader('Content-Type', 'application/json')
->withAddedHeader('Accept', $config['mime_type']);
- 19. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 19
Text-to-Speech: Initial Test (cont’d)
$request->getBody()->write(
json_encode(['text' => 'Customer requested special shipment. The
instructions were passed along to the warehouse.'])
);
$response = $client->send($request); // send to Watson
// send audio output back to web browser
header("Content-Type: {$config['mime_type']}");
header('Content-Disposition: inline; filename=result.ogg');
header('Content_Length: ' . strlen($response->getBody()));
print $response->getBody();
});
- 20. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 20
Application Configuration
<?php
return [
'text-to-speech' => [
'mime_type' => 'audio/ogg;codecs=opus',
'uri' => 'https://stream.watsonplatform.net/text-to-speech/api/v1',
],
'speech-to-text' => [
'mime_type' => 'audio/ogg;codecs=opus',
'uri' => 'https://stream.watsonplatform.net/speech-to-text/api/v1',
],
'db' => [
'I71EDU' => [
'options' => [
'autocommit' => DB2_AUTOCOMMIT_OFF,
'DB2_ATTR_CASE' => DB2_CASE_NATURAL,
'cursor' => DB2_FORWARD_ONLY,
'i5_lib' => 'CLARK',
'i5_naming' => DB2_I5_NAMING_OFF,
'i5_query_optimize' => DB2_ALL_IO,
],
],
],
];
- 21. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 21
Credentials Configuration
<?php
return [
'text-to-speech' => [
'username' => ‘a-big-long-obfuscated-hash',
'password' => ‘a-secret-hash'
],
'speech-to-text' => [
'username' => ' another-obfuscated-hash',
'password' => ' another-secret-hash'
],
'db' => [
'I71EDU' => [
'userprofile' => ‘my-user-name',
'password' => ‘my-password'
],
],
];
Keep auth credentials out of source code repository by putting them in
a separate file, and mark that file to be ignore by version control.
- 22. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 22
Merged Configuration
function getConfig()
{
return array_merge_recursive(
require_once __DIR__ . '/../config/config.php',
require_once __DIR__ . '/../config/credentials.php'
);
}
Calling the above yields the following merged configuration:
[
'text-to-speech' => [
'mime_type' => 'audio/ogg;codecs=opus',
'uri' => 'https://stream.watsonplatform.net/text-to-speech/api/v1/',
'username' => ‘a-big-long-obfuscated-hash',
'password' => ‘a-secret-hash‘
],
'speech-to-text' => […],
'db' => […]
]
- 23. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 23
Text-to-Speech: Product Description
call_user_func(function ()
{
$config = getConfig();
$db = getDbConnection($config);
$productDesc = getRandomProductDescription($db);
$client = new Client();
$request = (new Request())
->withUri((new Uri("{$config['text-to-speech']['uri']}/synthesize"))
->withQuery('voice=en-US_AllisonVoice’)) // config candidate
->withMethod('POST')
->withAddedHeader(
'Authorization',
'Basic ' . base64_encode("{$config['text-to-
speech']['username']}:{$config['text-to-speech']['password']}")
)->withAddedHeader('Content-Type', 'application/json')
->withAddedHeader('Accept', $config['text-to-speech']['mime_type']);
- 24. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 24
Text-to-Speech: Product Description (cont’d)
$request->getBody()->write(
json_encode(['text' => $productDesc ?: 'I was unable to access any
product description.'])
);
$response = $client->send($request);
header("Content-Type: {$config['text-to-speech']['mime_type']}");
header('Content-Disposition: inline; filename=result.ogg');
header('Content_Length: ' . strlen($response->getBody()));
print $response->getBody();
});
- 25. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 25
Quick Demo
- 26. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 26
Speech-to-Text: Overview
• Converts human voice into the written word
• Potential uses
• voice control of embedded systems
• transcription of meetings and conference calls
• dictation
• Combines information about grammar and language structure with
knowledge of the composition of the audio signal to generate a
transcription
• Languages: US and UK English, Japanese, Arabic, Mandarin, Brazilian
Portuguese, Spanish (Latin American & Spain), French, Localized
version available in Japan via SoftBank
• Metadata object in JSON response
• confidence score (per word), start/end time (per word), and
alternate hypotheses / N-Best (per phrase), word alternatives per
(sequential) time intervals
• Keyword Spotting (beta)
• search for keywords in audio stream. Begin/end times, confidence
score for each instance of the keyword found
- 27. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 27
Speech-to-Text: Overview (cont’d)
• Mobile SDKs (beta) for iOS and Android
• Optional metadata in JSON response includes
• confidence score (per word), start/end time (per word),
alternate hypotheses / N-Best (per phrase). alternatives per
(sequential) time interval
• Keyword search in audio stream
• beginning and end times
• confidence score for each instance found
• currently available at no additional charge
- 28. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 28
Speech-to-Text: Available Interfaces
• WebSocket
• single version of recognize method for transcribing audio
• low latency, high throughput over a full-duplex connection
• HTTP REST
• HTTP POST versions of recognize method for transcribing audio
with or without establishing a session with the service
• Accept input audio via
• request body
• multipart form data consisting of one or more audio files
• Can establish and maintain sessions with the service
• Obtain information about supported languages and models
- 29. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 29
Speech-to-Text: Available Interfaces
• Asynchronous HTTP
• Provides a non-blocking POST recognitions method
• Additional methods let you register a callback URL, to which the
service sends job status and optional results or to check the
status of jobs and retrieve results manually
• Uses HMAC-SHA1 signatures based on a user-specified secret
to provide authentication and data integrity for callback
notifications
- 30. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 30
Speech-to-Text: First Attempt
What I said:
Hello! This is Clark Everetts with Rogue Wave Software. I’m unable to
take your call at the moment, but if you leave your name, telephone
number, and a brief message, I’ll return your call as soon as possible.
Thank-you!
What I got:
hello this is or ever will when software mama unable to halt the
moment but if you leave your home telephone number and message
of return your call as soon as possible and
- 31. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 31
Speech-to-Text: Alternatives
“ … as soon as possible and “ (selected with highest confidence of
0.628)
“ … as soon as possible “
“ … as soon as possible are “
"alternatives": [
{
"timestamps": [
[
"hello",
0.13,
0.56
],
[
"this",
0.59,
0.76
],
[
"is",
0.76,
0.98
],
[
"or",
1.02,
1.3
],
[
"ever",
1.33,
1.64
],
- 32. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 32
Speech-to-Text: Recent Attempt
What I said:
Hello! This is Clark Everetts with Rogue Wave Software. I’m unable to
take your call at the moment, but if you leave your name, telephone
number, and a brief message, I’ll return your call as soon as possible.
Thank-you!
What I got:
hello this is or ever will one software on unable to recall the moment
but if you own telephone number which of the turn your call as soon
as possible
My theory: Terrible quality of audio taken from my laptop. Even after
editing out the worst hiss, the speech, though clear enough to a
person, isn’t clear enough for speech recognition.
- 33. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 33
Speech-to-Text test: Code
<?php
namespace ClarkPhpWatsonDemo;
use GuzzleHttpClient;
use ZendDiactorosRequest;
use ZendDiactorosUri;
use ZendDiactorosResponse;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../src/classic-models.php';
call_user_func(function () {
$config = getConfig();
$config = $config['speech-to-text'];
$client = new Client();
- 34. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 34
Speech-to-Text test: Code (cont’d)
$request = (new Request())
->withUri((new Uri("{$config['uri']}/recognize"))
->withQuery('timestamps=true&max_alternatives=3&inactivity_timeout=3'))
->withMethod('POST')
->withAddedHeader('Authorization',
'Basic ' . base64_encode("{$config['username']}:{$config['password']}"))
->withAddedHeader('Content-Type', 'audio/flac')
->withAddedHeader('Accept', 'application/json');
$request->getBody()
->write(file_get_contents(__DIR__ . '/../docs/telephone-greeting.flac'));
$response = $client->send($request);
print('<pre>');
printf("Response status: %d (%s)n",
$response->getStatusCode(), $response->getReasonPhrase()
);
printf("Headers:n");
foreach ($response->getHeaders() as $header => $values) {
printf(" %s: %sn", $header, implode(', ', $values));
}
printf("Message:n%sn", $response->getBody());
print '</pre>';
});
- 35. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 35
Watson Assistant: Overview
• Natural language interface backend for any application
• Natural conversation flows between your applications and users
• Automate interactions with end users
• Common applications
• virtual agents
• chat bots
• Trainable through web application, import of CSV files, or web API
- 36. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 36
Assistant: Where to get help
- 37. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 37
Watson Assistant: Intents – what are they?
• Purpose or goal expressed in a customer's input
• answering a question
• placing an order
• processing a bill payment
• canceling a process
• turning something on
• turning something off
• Watson recognizes the intent expressed in input
• Uses intent to choose appropriate dialog flow for responding to it
- 38. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 38
Watson Assistant: Intents - training
• Train Watson with minimum of five example statements
• More is better
• Best case: real user statements or questions for examples of intent
• Not necessary for examples to work with the same aspect of your
system; the important thing is they express intent
• Example: finding the location of something (#find_location intent)
• Customers, Employees, and Offices have locations
• Employees are associated with Office location.
• Where is customer Atelier graphique?
• Where is customer Dragon Souveniers located?
• What city is customer Diecast Classics headquartered in?
• Customer Herkku Gifts is located where?
• The location of William Patterson is where?
• Find Mary Patterson?
• What is the location of Julie Firrelli?
- 39. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 39
Watson Assistant: Entities
• Represent a class of object or a data type relevant to a user's purpose
• Recognizes entities mentioned in the user's input
• Conversation service uses entities to choose the specific actions to
take to fulfill an intent.
• Entities clarify a user's intent.
• Different entity values trigger different responses for the same intent.
• Entities for which there is a location:
• Employees
• Diane Murphy
• Mary Patterson, …
• Customers
• Technics Stores Inc.
• Herkku Gifts…
- 40. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 40
Dialog Flow
- 41. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 41
Conversation Dialog Flow (cont’d)
- 42. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 42
- 43. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 43
- 44. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 44
Conversation Dialog Flow (cont’d)
- 45. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 45
Testing Conversation Flow
- 46. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 46
Where to go from here?
• Next Phase
• Add more to Assistant instance, for workflow and context, and
“flow” to appropriate action, driving appropriate DB queries
• Use API to build and modify Assistant Dialog
• Making programmatic calls from an Assistant dialog node
• Improve recognition of Speech-to-Text
• Using spotted key words, parse text files and build SQL queries?
• Follow-on research
• Natural Language Processing
• Trend analysis, perhaps unexpected correlations (need more
realistic data and a much larger data set)
• Other Watson services
- 47. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 47
So, who is this guy?
• Clark Everetts
• Rogue Wave Software (acquired Zend 2015)
• PHP since 2005
• Professional Services Consultant
• Architecture & Performance Audits
• PHP, Zend Framework Training
• Zend Server
• App Development, Best Practices
• Many moons ago: missile launch control,
space station embedded software
• clark.everetts@roguewave.com
• @clarkphp on Twitter
- 48. © 2018 Rogue Wave Software, Inc. All Rights Reserved. 48
THANK-YOU
Stay in touch!
clark.everetts@roguewave.com
@clarkphp
Source code will be at
https://github.com/clarkphp/watson-classic-models
Your feedback is invaluable!
https://joind.in/talk/6a1fe