In this project, I integrated a Flask API into a WordPress site, enabling sentiment analysis. Users submit text through WordPress, which is sent to the Flask API for analysis, returning sentiment and confidence scores in real-time.
This project integrates a Flask API into a WordPress site to perform sentiment analysis. Users can input text through the WordPress interface, which is then sent to the Flask API. The API processes the text to determine sentiment—whether it’s positive or negative—along with a confidence score. This integration enhances WordPress with dynamic data analysis capabilities, offering users immediate insights into the sentiment of their content or reviews.
- app.py (Flask API): I developed an endpoint (/predict) that processes input text, predicts sentiment (positive/negative), and returns a confidence score. Added error handling for smooth interaction.
- app.php (WordPress Integration): Created a function to send text to the Flask API, retrieve the sentiment and confidence score, and display it under the “Analyze Sentiment” button in WordPress. Enhanced the interface for clarity.
- docker-compose.yaml: Configured Flask and WordPress as services with distinct containers, defined networking, and mapped ports to ensure the WordPress interface could communicate with the Flask API effectively.
-
Clone the Repository:
git clone https://github.com/Addika1630/api_ml-service.git
-
Navigate to the Required Directory:
cd api_ml-service/bash-wordpress-docker-setup -
Build and Start Services: Ensure Docker and Docker Compose are installed, then run:
sudo docker-compose up
This command will build and start both the Flask and WordPress containers.
-
Access WordPress and Flask Services:
- Once the containers are running, open your web browser and navigate to:
- Sentiment Analysis:
http://localhost:8030/?page_id=5 - WordPress:
http://localhost:8030(or the port specified in yourdocker-compose.yaml) - Flask API:
http://localhost:5000(or the port specified in yourdocker-compose.yamlfor Flask)
- Sentiment Analysis:
- Once the containers are running, open your web browser and navigate to:
-
Testing the Integration:
- In WordPress, use the integrated plugin or shortcode to send a sample request to the Flask API.
- You should see the output of the sentiment analysis directly on the WordPress page.
-
Stopping the Services:
- To stop the running containers, press
CTRL+Cin the terminal where you randocker-compose up. - Alternatively, run:
sudo docker-compose down
- To stop the running containers, press
-
Optional: Rebuilding the Containers:
- If you've made changes to the code and need to rebuild the containers, run:
sudo docker-compose up --build
- If you've made changes to the code and need to rebuild the containers, run:
This setup will ensure your Flask API and WordPress services are properly connected and running in Docker.
Clone the repository, set up Docker containers, and navigate to the WordPress page with the sentiment analysis form. Enter text, and the Flask API will analyze the sentiment, displaying the result and confidence score directly on the page.
The WordPress page features a simple form where users can input text for sentiment analysis. Upon submitting, the Flask API processes the input and returns the sentiment (positive/negative) along with the confidence score, which is displayed below the form for the user's reference.