Problem Statement
- Company Type: E-commerce
- Problem: E-commerce companies need to quickly and efficiently analyze customer sentiment based on user reviews, feedback, and social media to gauge perception of new or existing product launches.
- Challenge: Large numbers of user sentiments need to be efficiently analyzed close to real-time to provide timely feedback to the business.
Technical Approach
Languages & Tools:
- Backend Development: Python & Flask
- Frontend Interaction: JavaScript
- Sentiment Analysis: OpenAI’s GPT Model
- API Integration: OpenAI Python Package
- User Interface: HTML & CSS
- Asynchronous Requests: AJAX, Axios, Fetch API
- Data Handling: JSON
– **Process:**
– Details on setting up the Flask app, integrating OpenAI API for sentiment analysis, and creating a user-friendly interface with JavaScript.
Setting Up the Flask App
1. Install Python:
– Download and install the latest version of Python from the official website.
- Navigate to Python’s Official Website: Go to [https://www.python.org/downloads/][1] to find the latest version of Python. As of the latest information, Python 3.12.2 is the most recent stable release14.
- Choose the Correct Installer: Select the appropriate installer for your operating system. For Windows, you can choose from the Windows installer (64-bit) or Windows installer (32-bit) depending on your system architecture4.
- Run the Installer: After downloading the installer, run it on your machine. During the installation process, make sure to check the option to “Add Python to PATH” to ensure that Python is accessible from the command line
2. Create a Virtual Environment:
– Open Command Prompt and navigate to your project directory.
– Create a virtual environment: `python -m venv venv`.
3. Activate the Virtual Environment:
– Activate the virtual environment:
– On Windows: `.\venv\Scripts\activate`.

4. Install Flask:
– Install Flask within the virtual environment: `pip install Flask`.
5. Create Your Flask Application:
– In your project directory, create a new file named `app.py`.
– Initialize the Flask app by adding:
“`python
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello, World!’
“`
6. Run Your Flask Application:
– In Command Prompt, navigate to your project directory.
– Run the Flask app: `flask run`.
– Access the app in a web browser at `http://127.0.0.1:5000/`.

7. Integrate OpenAI API:
– Install the OpenAI Python package: `pip install openai`.
– Set up an endpoint in your `app.py` for sentiment analysis, using the OpenAI API to process the text.

8. Create a User-Friendly Interface with JavaScript:
– In your project directory, create a `templates` folder for HTML files.
– Use HTML & CSS to design the frontend, and JavaScript to handle user interactions.
– Implement AJAX calls from the frontend to your Flask backend for sentiment analysis.
9. Test the Complete Application:
– Ensure the Flask app is running.
– Open the web interface, input text for sentiment analysis, and view the results.

Code Snippets
API integration
import openai
...
chat_completion = client.chat.completions.create(
messages=system + user,
model="gpt-3.5-turbo",
max_tokens=60,
top_p=1.0,
)
Sentiment Analysis Logic
system = [
{"role": "system", "content": "You are a sentiment analysis bot designed to analyze feedback on various features of e-commerce products. \
Your task is to identify specific features mentioned in a piece of feedback, assess the sentiment for each feature individually, \
and classify them as positive, negative, mixed, or ambiguous. You should return the analysis in JSON format. \
For example:\
Feedback: 'The camera quality of this smartphone is outstanding, capturing vibrant colors and details. However, the battery life is disappointing, barely lasting a day with moderate use.'\
Response: {\
'features': {\
'camera quality': 'positive',\
'battery life': 'negative'\
}\
}\
Another example: \
Feedback: 'The laptop's screen is bright and crisp, making it great for watching movies, but I've had mixed feelings about the keyboard's responsiveness.'\
Response: {\
'features': {\
'screen': 'positive',\
'keyboard responsiveness': 'mixed'\
}\
}\
And: \
Feedback: 'The product dimensions were not as described, which was confusing.'\
Response: {\
'features': {\
'product dimensions': 'ambiguous'\
}\
}\
Always use this structured approach to ensure clarity and precision in sentiment analysis."}
]
Front-End Code for Displaying Results
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentiment Analysis Tool</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#result { margin-top: 20px; }
</style>
</head>
<body>
<h1>Sentiment Analysis</h1>
<textarea id="textInput" rows="4" cols="50" placeholder="Enter text here..."></textarea>
<button onclick="analyzeText()">Analyze Sentiment</button>
<div id="result"></div>
<script>
async function analyzeText() {
const textInput = document.getElementById('textInput').value;
const response = await fetch('/analyze', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: textInput })
});
const data = await response.json();
document.getElementById('result').innerText = 'Sentiment: ' + data.sentiment;
}
</script>
</body>
</html>

