Make your apps smarter with Embeddable AI

Provided by the IBM Skills Network team

Sina Nazeri
The Power of AI
4 min readNov 29, 2023

--

Explore the World of Embeddable AI! Join us on an exciting adventure into Embeddable AI. This project is for those who are curious and eager to learn AI skills.

Pro Tip: skip the rest and take this free project on cognitiveclass.ai.

What You’ll Learn By the end of this project, here’s what you’ll achieve:

  1. Understanding Embeddable AI Basics: Learn about the importance and basic concepts of adding AI to applications. This is especially useful for apps that don’t always have internet access.
  2. Practical Experience with Watson Embeddable AI: Get hands-on experience with Watson’s AI tools. You’ll focus on understanding how to analyze emotions and detect language structures in text.
  3. Getting to Know Gradio: Learn about Gradio, a tool that makes it easier to develop applications.

Understanding Embeddable AI Embeddable AI helps in two main ways:

  1. Works Without Constant Internet: It provides AI solutions even when there’s no stable internet connection.
  2. Easy Integration into Apps: It allows AI to be smoothly added into applications.

In this project, we’ll use IBM Watson’s NLP (Natural Language Processing) technology designed for Embeddable AI.

What’s in IBM Watson NLP for Embed? This tool offers IBM’s advanced NLP capabilities in a package that’s easy to use in various applications. It comes with:

  • A Collection of Pretrained Models: These are ready-to-use for various NLP tasks.
  • The Watson NLP Runtime Image: This contains the necessary files and a high-performance framework to run NLP tasks efficiently. It’s designed for easy use and quick demonstrations.

Here is a breakdown of what is included:

Getting Started with Emotion Analysis Using cURL Let’s dive into using Embeddable AI with a practical example: predicting emotions from text.

What We’re Doing: We’ll use pre-trained models to identify emotions in a given text. For instance, the text “I hate school. School is bad.” might be classified as expressing anger and sadness.

Setting Up the Emotion Analysis: We’ll use the Skills Network Emotion service for this. Here are the key details:

  • Hostname: sn-watson-emotion.labs.skills.network
  • Path: /v1/watson.runtime.nlp.v1/NlpService/EmotionPredict
  • Model: emotion_aggregated-workflow_lang_en_stock

Running the cURL Command:

We’ll execute a cURL command in the terminal to analyze emotions in a sentence like I love learning! Learning is really the best! Here’s the command setup:

# Setup virtual environment
EMOTION_HOST="sn-watson-emotion.labs.skills.network"
EMOTION_PATH="/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict"
EMOTION_MODEL="emotion_aggregated-workflow_lang_en_stock"
SENTENCE="I love learning! Learning is really the best!"
curl -X POST "https://${EMOTION_HOST}${EMOTION_PATH}" -H "grpc-metadata-mm-model-id: ${EMOTION_MODEL}" -d "{ \"raw_document\": { \"text\": \"${SENTENCE}\" } }" | jq

This will yield a JSON response highlighting the detected emotions.

At a glance you can see that joy is the most prevalent emotion, with “I love learning!” being the most joyous part!

Build Your First Embeddable AI App with Gradio Ready to create your first app using Embeddable AI? Let’s build an app that performs Emotion Analysis, similar to what we did with cURL.

Steps to Create the App:

  1. Set Up the File:

Create a new file named emotion.py.

2. Write the getEmotion Function:

  • Use the requests library to make an HTTP API call.
  • Here’s the function code:
import requests
def getEmotion(text):
url = "https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict"
headers = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
data = {"raw_document": { "text": text }}
response = requests.post(url, headers=headers, json=data)
return response.json()

3. Integrate with Gradio:

  • Replace the greet method in your Gradio app with getEmotion.
import gradio as gr
import requests

# ... [getEmotion function code] ...

demo = gr.Interface(fn=getEmotion, inputs="text", outputs="json")
if __name__ == "__main__":
demo.launch()
  • Here’s how your Gradio app code will look:

Dig Deeper by Creating a Movie Review Analysis App by taking the Project (for free)👇🏻:

Out of the movie Review Analysis app:

You can follow me on Medium or LinkedIn and stay tuned for more articles on Data Science, Machine Learning, and AI.

If you are interested in learning more about my projects, check out my IBM skills network profile:

--

--

Sina Nazeri
The Power of AI

Data Scientist at IBM with broad ML skills: Classification, Clustering, CV, NLP, Generative AI. Strong academic background & research/work experience.