@app.route('/classify-batch', methods=['POST'])
def classify_batch():
try:
data = request.get_json()
texts = data.get('texts')
if not texts or not isinstance(texts, list) or len(texts) == 0:
return jsonify({'error': 'Texts array is required'}), 400
results = client.nlp.classify_batch(
texts=texts,
categories=[
'Positive Feedback',
'Negative Feedback',
'Feature Request',
'Bug Report',
'Question'
]
)
return jsonify(results)
except Exception as e:
print(f"Batch classification error: {e}")
return jsonify({'error': 'Batch classification failed'}), 500
Conclusion
Congratulations! You've built a text classification system using Invictus AI. This system can automatically categorize customer feedback, which can help your team prioritize and respond to feedback more efficiently.
Next Steps
Explore Custom Models to train a classifier on your own data
Learn how to implement Sentiment Analysis alongside classification
Check out our API Reference for more advanced options# Text Classification Tutorial
Introduction
This tutorial will guide you through building a text classification system using Invictus AI. By the end, you'll have a working application that can automatically categorize text into predefined categories.
Prerequisites
An Invictus AI account with an API key
Basic understanding of JavaScript or Python
Installed the Invictus AI SDK (see Installation)
Step 1: Set Up Your Project
First, let's create a new project and install the required dependencies.
@app.route('/classify', methods=['POST'])
def classify():
try:
data = request.get_json()
text = data.get('text')
if not text:
return jsonify({'error': 'Text is required'}), 400
result = client.nlp.classify(
text=text,
categories=[
'Positive Feedback',
'Negative Feedback',
'Feature Request',
'Bug Report',
'Question'
]
)
return jsonify(result)
except Exception as e:
print(f"Classification error: {e}")
return jsonify({'error': 'Classification failed'}), 500
Step 4: Test Your Classifier
Start your server:
For JavaScript
node app.js
For Python
python app.py
Now, let's test our classifier with some sample texts:
# For Positive Feedback
curl -X POST \
http://localhost:3000/classify \
-H 'Content-Type: application/json' \
-d '{"text": "I love your product! It has made my life so much easier."}'
# For Feature Request
curl -X POST \
http://localhost:3000/classify \
-H 'Content-Type: application/json' \
-d '{"text": "It would be great if you could add dark mode to the app."}'
# For Bug Report
curl -X POST \
http://localhost:3000/classify \
-H 'Content-Type: application/json' \
-d '{"text": "The app crashes whenever I try to upload an image."}'
Step 5: Building a Simple Front-End (Optional)
Let's create a simple HTML form to test our classifier.
const path = require('path');
// Add this before your routes
app.use(express.static(path.join(__dirname)));
// Add this after your routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
For Python
Update your app.py to serve the HTML file:
@app.route('/')
def index():
return app.send_static_file('index.html')
# Make sure to create a 'static' folder and put your index.html there
Step 6: Enhancing the Classifier
For more accurate classification, you can use Invictus AI's advanced configuration options: