====== Product Sentiment Analysis App ====== This is a web application that searches X/Twitter for tweets about a product, analyzes their sentiment using Azure Cognitive Services and Azure OpenAI, and displays a summary. ===== Infrastructure ===== ==== Required Services ==== * **Azure OpenAI Service** – Used to summarize tweet sentiments via the Chat Completions API. * **Azure Text Analytics** – For classifying sentiment (positive, negative, neutral) of individual tweets via the Sentiment Analysis API. * **Twitter API v2** – To fetch tweets related to the searched keyword using the recent search endpoint. * **Azure App Service** – For hosting the backend using the FastAPI framework. ==== Environment Variables ==== * `AZURE_OPENAI_KEY` * `AZURE_OPENAI_ENDPOINT` * `AZURE_OPENAI_DEPLOYMENT_NAME` * `AZURE_TEXT_ANALYTICS_KEY` * `AZURE_TEXT_ANALYTICS_ENDPOINT` * `X_BEARER_TOKEN` Store them in a `.env` file or as App Settings in Azure App Service. ===== Backend Code (app.py) ===== ==== Libraries Used ==== * `fastapi` – Defines HTTP endpoints. * `requests` – Used to interact with the Twitter API. * `openai` – Azure OpenAI SDK for calling GPT models. * `azure.ai.textanalytics` – Azure SDK for Text Analytics API. * `jinja2` – Template rendering. * `uvicorn` – Development server for FastAPI. ==== Endpoints ==== * `GET /` – Renders the form (HTML template). * `POST /analyze` – Handles form submission, performs: - Tweet search. - Sentiment classification. - Summary generation. ==== Key Functions ==== === search_tweets(product_name) === - Calls Twitter API v2: - Endpoint: `https://api.twitter.com/2/tweets/search/recent` - Auth: Bearer Token (`X_BEARER_TOKEN`) - Returns up to 10 recent English tweets about the keyword. === analyze_sentiment(texts) === - Calls Azure Text Analytics API: - Endpoint: `/text/analytics/v3.1/sentiment` - Auth: API Key (`AZURE_TEXT_ANALYTICS_KEY`) - Returns document sentiment (positive, negative, neutral) and confidence scores. === summarize_with_gpt(tweets) === - Calls Azure OpenAI Chat Completions API: - Endpoint: `/openai/deployments//chat/completions` - Model: gpt-35-turbo or compatible - Prompts GPT to summarize public sentiment based on labeled tweet data. ===== Frontend Code (index.html) ===== Uses Bootstrap for styling. Features: * Form for entering a product. * Display area showing: - Sentiment-labeled tweets. - GPT-generated summary. ===== User Steps ===== ==== 1. Configure Environment ==== Create `.env` with: ``` AZURE_OPENAI_KEY=your_key AZURE_OPENAI_ENDPOINT=https://your-endpoint.openai.azure.com/ AZURE_OPENAI_DEPLOYMENT_NAME=your_deployment AZURE_TEXT_ANALYTICS_KEY=your_text_analytics_key AZURE_TEXT_ANALYTICS_ENDPOINT=https://your-text-analytics.cognitiveservices.azure.com/ TWITTER_BEARER_TOKEN=your_twitter_bearer_token ``` ==== 2. Install Dependencies ==== Run: pip install -r requirements.txt ==== 3. Launch the App ==== Run: uvicorn app:app --reload Then open your browser to: http://localhost:8000 ==== 4. Use the App ==== * Enter a product keyword (e.g., "AirPods"). * Submit. * View tweet list with sentiment tags. * View summary generated by GPT. ===== File Structure ===== ^ File ^ Purpose ^ | `app.py` | Main backend logic | | `templates/index.html` | Frontend | | `requirements.txt` | Dependencies | ===== How It Works ===== - User inputs a keyword. - Tweets are fetched using Twitter API v2. - Sentiment analysis is performed using Azure Text Analytics. - Tweets are summarized using Azure OpenAI GPT. - Result is rendered via HTML template.