GaneshArihanth commited on
Commit
a0c1a8b
ยท
verified ยท
1 Parent(s): b533b51

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +255 -1
README.md CHANGED
@@ -1,11 +1,265 @@
1
  ---
2
  title: Bert Sentiment Api
3
- emoji: ๐Ÿ†
4
  colorFrom: green
5
  colorTo: green
6
  sdk: docker
7
  pinned: false
8
  license: mit
 
 
 
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Bert Sentiment Api
3
+ emoji: ๐Ÿ”—
4
  colorFrom: green
5
  colorTo: green
6
  sdk: docker
7
  pinned: false
8
  license: mit
9
+ thumbnail: >-
10
+ https://cdn-uploads.huggingface.co/production/uploads/67d7a90493e0004ce35798ed/YVex6OlxZf5nD8lb6Pd3M.png
11
+ short_description: High-accuracy Sentiment Analysis API using a fine-tuned BERT
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15
+
16
+ # Sentiment Analyzer ๐Ÿง 
17
+
18
+ A premium, interactive web application for real-time sentiment analysis. This project leverages a fine-tuned **BERT** model to classify text as **Positive** or **Negative** with high accuracy, wrapped in a modern, aesthetically pleasing React frontend.
19
+
20
+ ---
21
+
22
+ ## ๐Ÿ“˜ Beginner's Corner: What is this?
23
+
24
+ If you are new to Machine Learning, welcome! ๐Ÿ‘‹
25
+
26
+ **What is Sentiment Analysis?**
27
+ Imagine you have a super-smart assistant who reads a sentence and instantly tells you if the person is happy, angry, or disappointed. That is **Sentiment Analysis**. It's widely used by companies to understand customer feedback, monitor social media, and improve products.
28
+
29
+ **How does it work?**
30
+ We don't just look for keywords like "good" or "bad". We use a sophisticated AI model called **BERT** that understands the *context* of a sentence. For example, it knows that "not bad" is actually positive, even though it contains the word "bad".
31
+
32
+ ---
33
+
34
+ ## ๐Ÿง  Deep Dive: The BERT Architecture
35
+
36
+ This project is built on **BERT** (Bidirectional Encoder Representations from Transformers), a revolutionary model developed by Google.
37
+
38
+ ### 1. The Transformer "Engine"
39
+ At its core, BERT uses a **Transformer** architecture. Unlike older models that read text from left to right (like humans do), Transformers read the entire sentence at once. This allows the model to understand the relationship between every word simultaneously.
40
+
41
+ ### 2. Bidirectional Context
42
+ The "B" in BERT stands for **Bidirectional**.
43
+ - **Unidirectional models** read: "The quick brown..." (can't see what's coming next).
44
+ - **BERT** reads: "...quick brown fox..." (sees both "quick" and "fox" to understand "brown").
45
+ This allows BERT to understand nuance. For example, it can distinguish between a "river **bank**" and a "**bank** account" based on the surrounding words.
46
+
47
+ ### 3. Fine-Tuning
48
+ We didn't build BERT from scratch (that takes supercomputers!). We used a technique called **Transfer Learning**:
49
+ 1. **Pre-training**: Google trained BERT on massive amounts of text (Wikipedia, Books) to teach it the English language.
50
+ 2. **Fine-tuning**: We took this "smart" model and gave it a specific job: "Read these reviews and tell me if they are positive or negative." This is what makes our model specialized for Sentiment Analysis.
51
+
52
+ ---
53
+
54
+ ## ๐Ÿ”ญ Project Scope
55
+
56
+ ### Current Capabilities
57
+ - **Real-time Analysis**: Instant feedback as you type or submit text.
58
+ - **Binary Classification**: accurately identifies **Positive** vs **Negative** sentiment.
59
+ - **Confidence Scoring**: Tells you *how sure* the model is about its prediction (e.g., "99.8% confident").
60
+ - **Interactive UI**: A high-end user interface that demonstrates how AI can be presented beautifully.
61
+
62
+ ### Future Roadmap
63
+ - **Batch Processing**: Upload a CSV file of thousands of reviews for bulk analysis.
64
+ - **Multi-Class Sentiment**: Detecting Neutral, Angry, Sad, or Sarcastic tones.
65
+ - **History Tracking**: Saving past analyses to a database.
66
+ - **API Integration**: Allowing other developers to use this backend as a service.
67
+
68
+ ---
69
+
70
+ ## โœจ Features
71
+
72
+ ### ๐Ÿš€ Advanced NLP Backend
73
+ - **BERT Model**: Utilizes `BertForSequenceClassification` fine-tuned for sentiment analysis.
74
+ - **High Performance**: Optimized for fast inference on local machines (supports MPS/CPU).
75
+ - **Flask API**: Robust REST API handling prediction requests.
76
+ - **CORS Enabled**: Configured to securely handle cross-origin requests from the frontend.
77
+
78
+ ### ๐ŸŽจ Premium Frontend Experience
79
+ - **Interactive DotGrid Background**: A physics-based background where dots react to mouse movements and clicks with inertia and shockwave effects (powered by **GSAP**).
80
+ - **Variable Proximity Heading**: The main title dynamically adjusts its font weight and size based on how close your mouse cursor is (powered by **Framer Motion**).
81
+ - **ShinyText Effect**: A subtle, premium shimmer animation on the subtitle.
82
+ - **Glassmorphism Design**: Modern, frosted-glass UI elements with smooth hover effects and transitions.
83
+ - **Dynamic Feedback**: Visual cues for positive (Green) and negative (Red) results, including a confidence confidence bar.
84
+
85
+ ---
86
+
87
+ ## ๐Ÿ› ๏ธ Tech Stack
88
+
89
+ ### Backend
90
+ - **Python 3.x**: The programming language for the AI logic.
91
+ - **Flask**: A lightweight web framework to create the API.
92
+ - **PyTorch**: The deep learning library running the BERT model.
93
+ - **Transformers**: Hugging Face's library to easily load and use BERT.
94
+
95
+ ### Frontend
96
+ - **React 18**: The library for building the user interface.
97
+ - **Vite**: A super-fast tool for building and running the frontend.
98
+ - **GSAP & Framer Motion**: Libraries for complex, high-performance animations.
99
+ - **CSS3**: Custom styling for the glassmorphism and layout.
100
+
101
+ ---
102
+
103
+ ## ๐Ÿ“ฆ Installation Guide (Local)
104
+
105
+ ### Prerequisites
106
+ You need these installed on your computer:
107
+ - **Python 3.8+**: To run the backend.
108
+ - **Node.js 16+**: To run the frontend.
109
+
110
+ ### 1. Backend Setup (The Brain)
111
+ This sets up the server that runs the AI model.
112
+
113
+ 1. Navigate to the backend folder:
114
+ ```bash
115
+ cd backend
116
+ ```
117
+ 2. Install the necessary "tools" (libraries) for Python:
118
+ ```bash
119
+ pip install -r requirements.txt
120
+ ```
121
+ *This installs Flask, PyTorch, and Transformers.*
122
+
123
+ ### 2. Frontend Setup (The Face)
124
+ This sets up the website you interact with.
125
+
126
+ 1. Open a new terminal and navigate to the frontend folder:
127
+ ```bash
128
+ cd frontend
129
+ ```
130
+ 2. Install the necessary "tools" (packages) for the website:
131
+ ```bash
132
+ npm install
133
+ ```
134
+ *This downloads React, GSAP, and other interface libraries.*
135
+
136
+ ### 3. Running Locally
137
+ 1. **Start Backend**:
138
+ ```bash
139
+ # In backend folder
140
+ python app.py
141
+ ```
142
+ 2. **Start Frontend**:
143
+ ```bash
144
+ # In frontend folder
145
+ npm run dev
146
+ ```
147
+ 3. Open the link shown (e.g., `http://localhost:5173`).
148
+
149
+ ---
150
+
151
+ ## โ˜๏ธ Deployment Guide (Step-by-Step)
152
+
153
+ This project uses a **Split Deployment** strategy.
154
+ - **Why?** The BERT model is large (hundreds of MBs). Free frontend hosting (like Vercel) cannot handle this size.
155
+ - **Solution**: We host the "Brain" (Model) on **Hugging Face Spaces** (which offers free CPU hosting for models) and the "Face" (Frontend) on **Vercel** (which is optimized for React).
156
+
157
+ ### Part 1: Deploying the Backend (Hugging Face Spaces)
158
+
159
+ 1. **Create an Account**: Go to [huggingface.co](https://huggingface.co/) and sign up.
160
+ 2. **Create a Space**:
161
+ - Click on your profile picture -> **New Space**.
162
+ - **Space Name**: `bert-sentiment-api` (or similar).
163
+ - **License**: `MIT`.
164
+ - **SDK**: Select **Docker**.
165
+ - **Docker Template**: Select **Blank**.
166
+ - **Space Hardware**: Keep it as **CPU Basic (Free)**.
167
+ - Click **Create Space**.
168
+ 3. **Upload Files**:
169
+ - Go to the **Files** tab of your new Space.
170
+ - Click **Add file** -> **Upload files**.
171
+ - **Drag and drop** the following files from your local `hf_space` folder:
172
+ - `app.py`
173
+ - `requirements.txt`
174
+ - `Dockerfile`
175
+ - **CRITICAL**: Drag and drop **ALL FILES** from your local `saved_bert_sentiment_model` folder into the root.
176
+ - *Do not upload the folder itself, upload the files INSIDE it.*
177
+ - You should see `config.json`, `pytorch_model.bin`, etc., listed right next to `app.py`.
178
+ - Click **Commit changes to main**.
179
+ 4. **Wait for Build**:
180
+ - The Space will show "Building". This might take 2-5 minutes.
181
+ - When it says **Running**, your API is ready!
182
+ 5. **Get the API URL**:
183
+ - Click the **three dots** (โ‹ฎ) in the top right -> **Embed this space**.
184
+ - Copy the **Direct URL**. It looks like: `https://username-space.hf.space`.
185
+
186
+ ### Part 2: Deploying the Frontend (Vercel)
187
+
188
+ 1. **Push to GitHub**:
189
+ - Ensure your project is pushed to a GitHub repository.
190
+ 2. **Import to Vercel**:
191
+ - Go to [vercel.com](https://vercel.com) and log in.
192
+ - Click **Add New...** -> **Project**.
193
+ - Select your GitHub repository.
194
+ 3. **Configure Project**:
195
+ - **Framework Preset**: Select **Vite**.
196
+ - **Root Directory**: Click Edit and select `frontend`.
197
+ 4. **Environment Variables**:
198
+ - Expand the **Environment Variables** section.
199
+ - **Key**: `VITE_API_URL`
200
+ - **Value**: Paste the **Direct URL** from Hugging Face (Part 1, Step 5).
201
+ - *Example*: `https://ganesharihanth-bert-sentiment-api.hf.space`
202
+ - *Note*: Remove any trailing slash `/` at the end.
203
+ 5. **Deploy**:
204
+ - Click **Deploy**.
205
+ - Wait for the confetti! ๐ŸŽ‰
206
+
207
+ ### Part 3: Verification
208
+ - Open your Vercel app URL.
209
+ - Type "I am so happy this works!" and click Analyze.
210
+ - If you see "Positive" and a confidence score, everything is connected!
211
+
212
+ ---
213
+
214
+ ## ๐Ÿ“ก API Documentation
215
+
216
+ For developers who want to use the backend programmatically.
217
+
218
+ ### POST `/predict`
219
+ Analyzes the sentiment of the provided text.
220
+
221
+ **Request Body:**
222
+ ```json
223
+ {
224
+ "text": "I love this product!"
225
+ }
226
+ ```
227
+
228
+ **Response:**
229
+ ```json
230
+ {
231
+ "sentiment": "Positive",
232
+ "confidence": 0.998,
233
+ "label": 1
234
+ }
235
+ ```
236
+
237
+ ---
238
+
239
+ ## ๐Ÿ“‚ Project Structure
240
+
241
+ ```
242
+ Sentiment analysis/
243
+ โ”œโ”€โ”€ backend/ # Local Flask API
244
+ โ”‚ โ”œโ”€โ”€ app.py
245
+ โ”‚ โ””โ”€โ”€ requirements.txt
246
+ โ”œโ”€โ”€ frontend/ # React Frontend
247
+ โ”‚ โ”œโ”€โ”€ src/
248
+ โ”‚ โ”‚ โ”œโ”€โ”€ App.jsx # Main Logic
249
+ โ”‚ โ”‚ โ””โ”€โ”€ ...
250
+ โ”‚ โ””โ”€โ”€ package.json
251
+ โ”œโ”€โ”€ hf_space/ # Deployment files for Hugging Face
252
+ โ”‚ โ”œโ”€โ”€ app.py
253
+ โ”‚ โ”œโ”€โ”€ requirements.txt
254
+ โ”‚ โ””โ”€โ”€ Dockerfile
255
+ โ”œโ”€โ”€ saved_bert_sentiment_model/ # The "Brain" (Model files)
256
+ โ””โ”€โ”€ README.md # You are reading this!
257
+ ```
258
+
259
+ ## ๐Ÿค Contributing
260
+
261
+ Contributions are welcome! If you have ideas to make the UI cooler or the model smarter, feel free to fork the repo and submit a Pull Request.
262
+
263
+ ## ๐Ÿ“ License
264
+
265
+ This project is open source and available under the [MIT License](LICENSE).