☁️Emotion Prediction Web App with Flask and ModelArts
Introduction🚀
Hi everyone 📢 , in this article , we will explore how to build an Emotion Prediction application using Huawei Cloud ModelArts.
Sentiment analysis plays a crucial role in understanding human behaviour and developing various applications. Predicting emotions from text has become an important task in natural language processing.
By integrating with ModelArts and Flask, we will develop a web-based system that accurately predicts emotions from textual contributions
Services Used 👀
- Object Storage Service(OBS)
- ModelArts
- Elastic Cloud Server (ECS)
Service Details 🔨
We can make our project by following the steps below.
1️⃣ Create a Dataset
To begin, we need a dataset for training our Emotion Prediction model. We can obtain a text dataset from Kaggle, for example.
Exeml text classification only supports Chinese language for now . Exeml will be getting more language support in the coming days. So I translated dataset to chinese .
2️⃣ Upload Dataset to OBS
I created a bucket in OBS and created input and output files . Then I uploaded dataset to input file in OBS .
3️⃣ Create Text Classification Project
Next, we will move to the ModelArts ExeML Service and create a text classification model. In the input and output section, we will specify the path of the OBS bucket we created earlier.
We see the created exeml text classification project.
Once the project is created, we need to label the dataset. We can do this by navigating to ModelArts > Data Management > Datasets > “your dataset” and creating a labeling job.
Then click the Add data . In the import path , we enter the input path of the bucket we created in OBS. You can select whether the data set is labelled or unlabelled. According to your dataset select labelling format.
The dataset has been labelled .
After labeling the dataset, we can proceed to train the model. The training process will provide us with valuable metrics to evaluate the performance of our model.
Click the Deploy button and deploy your model.
After you deploy your model, it is published as a real-time service . Then click the ModelArts > Service Deployment > Real-Time Services .
We are testing our model and it is working successfully.
4️⃣ Create a Web Application with Flask
To interact with the Emotion Prediction model, we will create a web application using Flask. Flask is a Python web framework that will allow us to communicate with the Huawei Cloud ModelArts API for making predictions.
We will create the frontend of the web application using HTML, specifically index.html and result.html files. The Flask application will handle user input, make requests to the Huawei Cloud ModelArts API, and display the predicted emotion label and scores.
The code snippet below showcases the Flask application structure:
from flask import Flask, render_template, request
import requests
import json
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST', 'GET'])
def predict():
text = request.form['text']
# Making a request to the Huawei Cloud ModelArts API
api_url = 'your_api_url'
headers = {'Content-Type': 'application/json',
'x-auth-token': 'your_token'}
data = {'text': text}
response = requests.post(api_url, data=json.dumps(data), headers=headers)
result = response.json()
print(result)
# Extract the predicted label and scores
predicted_label = result['predicted_label']
scores = result['scores']
return render_template('result.html', predicted_label=predicted_label, scores=scores)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
5️⃣ Run the App
After creating an Elastic Cloud Server (ECS), we will upload the Flask application files onto the server. Once the files are uploaded, we can run the application.
When we enter the address of the application, the login page welcomes us.
When we enter a random text and submit it, it predict us the label.
The application is working successfully .
Conclusion 🎈
In this article, we explored the process of building an Emotion Prediction web application using Flask and Huawei Cloud ModelArts. By leveraging the power of ModelArts’ text classification capabilities and integrating it with Flask, we were able to develop a system that accurately predicts emotions from textual inputs.
👓You can reach me from my Linkedin account for all your questions and requests.
References 📚
1- Huawei Cloud OBS
2- Huawei Cloud ModelArts
3- Huawei Cloud ECS