Spaces:
Sleeping
Sleeping
Commit Β·
e13ffcc
0
Parent(s):
π Deploy Hugging Face Gradio version with updated README
Browse files- .gitignore +21 -0
- README.md +51 -0
- app.py +26 -0
- requirements.txt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python virtual environment
|
| 2 |
+
venv/
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
|
| 6 |
+
# VSCode & OS junk
|
| 7 |
+
.vscode/
|
| 8 |
+
.DS_Store
|
| 9 |
+
|
| 10 |
+
# Environment files
|
| 11 |
+
.env
|
| 12 |
+
.env.*
|
| 13 |
+
|
| 14 |
+
# Model folders (only ignore unnecessary subfolders)
|
| 15 |
+
model/__results__files/
|
| 16 |
+
model/results.zip
|
| 17 |
+
model/sentiment_model/
|
| 18 |
+
model/*__huggingface_repo__*
|
| 19 |
+
|
| 20 |
+
# Don't ignore actual model files that are used!
|
| 21 |
+
# Keep: model.safetensors, tokenizer.json, config.json, etc.
|
README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π§ Sentiment Analysis V2 (Gradio Version)
|
| 2 |
+
|
| 3 |
+
A lightweight sentiment classification web app using a fine-tuned **BERT** model.
|
| 4 |
+
|
| 5 |
+
> Hosted entirely on [π€ Hugging Face Spaces](https://huggingface.co/spaces/McKlay/SentimentAnalysisV2-HF) using **Gradio** and **Transformers**.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## βοΈ Tech Stack
|
| 10 |
+
- π€ **Transformers** for BERT-based text classification
|
| 11 |
+
- π₯ **PyTorch** for model inference
|
| 12 |
+
- π¨ **Gradio** for building the web interface
|
| 13 |
+
- π **Hugging Face Spaces** for free app hosting
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## π§ͺ Try It Out
|
| 18 |
+
Paste a sentence into the textbox and get real-time sentiment probabilities (positive/negative) with confidence levels.
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## π File Structure
|
| 23 |
+
SentimentAnalysisV2-HF/
|
| 24 |
+
βββ app.py # Main Gradio app
|
| 25 |
+
βββ model/ # empty, the model is located in model hub McKlay/sentiment-analysis-v2
|
| 26 |
+
βββ requirements.txt # Dependency file
|
| 27 |
+
βββ .gitignore βββ README.md
|
| 28 |
+
|
| 29 |
+
---
|
| 30 |
+
|
| 31 |
+
## β
How It Works
|
| 32 |
+
|
| 33 |
+
1. Loads the tokenizer and model from the `model/` folder.
|
| 34 |
+
2. Processes input text and runs inference using BERT.
|
| 35 |
+
3. Returns the confidence scores for each sentiment label.
|
| 36 |
+
4. All of this happens **live** in your browser via Gradio!
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## β¨ Author
|
| 41 |
+
|
| 42 |
+
**Clay Mark Sarte**
|
| 43 |
+
[GitHub: @McKlay](https://github.com/McKlay)
|
| 44 |
+
Passionate about AI, ML, and building useful tools with minimal stack.
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
## π License
|
| 49 |
+
|
| 50 |
+
MIT License β free to use and modify.
|
| 51 |
+
|
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer from Hugging Face Hub
|
| 6 |
+
MODEL_DIR = "McKlay/sentiment-analysis-v2"
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_DIR)
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_DIR)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
# Define prediction function
|
| 12 |
+
def analyze_sentiment(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 16 |
+
labels = ['Negative', 'Positive']
|
| 17 |
+
return {labels[i]: float(probs[0][i]) for i in range(2)}
|
| 18 |
+
|
| 19 |
+
# Launch Gradio interface
|
| 20 |
+
gr.Interface(
|
| 21 |
+
fn=analyze_sentiment,
|
| 22 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a comment..."),
|
| 23 |
+
outputs=gr.Label(num_top_classes=2),
|
| 24 |
+
title="Sentiment Analysis V2 (Gradio)",
|
| 25 |
+
description="Real-time sentiment analysis using a BERT-based model. Powered by π€ Transformers and Gradio."
|
| 26 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|