Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Watson and PHP Inventory Integration 2017-11-09...

Watson and PHP Inventory Integration 2017-11-09 Status

2017-11-09 status of an ongoing project to see how IBM Watson services can help manage inventory, product lines, customer orders, and sales reps. End goals: speech-to-text; text-to-speech; conversational, interactive application on IBM i; powered by Watson in the cloud; data residing on local IBM i.

Clark Everetts

November 09, 2017
Tweet

More Decks by Clark Everetts

Other Decks in Programming

Transcript

  1. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Watson & PHP Inventory Integration Clark Everetts, Rogue Wave Software
  2. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    So, who is this guy? • Clark Everetts • Rogue Wave Software (acquired Zend 2015) • PHP since 2005 • Zend Certified Engineer • 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 • [email protected] • @clarkphp on Twitter • +ClarkEveretts on Google+
  3. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    The Premise – 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 your IBM i
  4. © 2017 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. © 2017 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/ • Phan – https://github.com/phan/phan • Exakat – https://www.exakat.io/ • PHPMetrics – http://www.phpmetrics.org/
  6. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  7. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    IBM Bluemix Cloud https://console.bluemix.net/ (login with IBM id) Stay informed!! https://www.ibm.com/blogs/bluemix/
  8. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  9. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  10. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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)
  11. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  12. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  13. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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.mybluemix.net/
  14. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Text-to-Speech: Initial Test <?php namespace ClarkPhp\WatsonDemo; use GuzzleHttp\Client; use Zend\Diactoros\Request; use Zend\Diactoros\Uri; use Zend\Diactoros\Response; 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']);
  15. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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(); });
  16. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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, ], ], ], ];
  17. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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.
  18. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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' => […] ]
  19. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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')) ->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']);
  20. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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(); });
  21. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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, Portuguese, Spanish, 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
  22. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  23. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Speech-to-Text: Available Interfaces • WebSocket • single version of recognize method for transcribing audio • easy implementation, 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
  24. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  25. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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
  26. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    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 ],
  27. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Speech-to-Text test: Code <?php namespace ClarkPhp\WatsonDemo; use GuzzleHttp\Client; use Zend\Diactoros\Request; use Zend\Diactoros\Uri; use Zend\Diactoros\Response; require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../src/classic-models.php'; /** * Self-called anon. function to create its own scope and keep global namespace clean. */ call_user_func(function () { $config = getConfig(); $config = $config['speech-to-text']; $client = new Client();
  28. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Speech-to-Text test: Code (cont’d) $request = (new Request()) ->withUri((new Uri("{$config['uri']}/recognize")) ->withQuery('timestamps=true&max_alternatives=3&inactivity_timeout=1')) ->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: %s\n", $header, implode(', ', $values)); } printf("Message:\n%s\n", $response->getBody()); print '</pre>'; });
  29. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Watson Conversation: 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
  30. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Watson Conversation: Intents • Purpose or goal expressed in a customer's input • answering a question • placing an order • processing a bill payment • cancelling 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
  31. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Watson Conversation: Intents (cont’d) • 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 (#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?
  32. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Watson Conversation: 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…
  33. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    Where to go from here? • Next Phase • Improve recognition of Speech-to-Text • Using spotted key words, parse text files and build SQL queries? • Add more to Conversation instance, for workflow and context, and “flow” to appropriate action, driving appropriate DB2 queries • Use API to build and modify Conversation Dialog • Follow-on research • Natural Language Processing • Trend analysis, perhaps unexpected correlations (probably need more realistic data and a much larger data set) • Other Watson services
  34. © 2016 Rogue Wave Software, Inc. All Rights Reserved. ‹#›

    THANK-YOU! [email protected] @clarkphp +ClarkEveretts Stay in touch! Your feedback is invaluable. Source code at https://github.com/clarkphp/watson-classic-models