Spaces:
Sleeping
Sleeping
Commit
Β·
d0a31d6
1
Parent(s):
7590b75
Imported code from GitHub repo
Browse files- README.md +76 -12
- app.py +32 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,12 +1,76 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Text-Sentiment-Classifier-API
|
2 |
+
This project is a simple REST API for sentiment analysis built with FastAPI and HuggingFace Transformers. It uses the distilbert-base-uncased-finetuned-sst-2-english model to classify English text as positive, negative or (rarely) neutral, and it returns a confidence score.
|
3 |
+
|
4 |
+
# π Features
|
5 |
+
|
6 |
+
- RESTful API with FastAPI
|
7 |
+
|
8 |
+
- Pre-trained BERT-based sentiment model
|
9 |
+
|
10 |
+
- JSON input and output
|
11 |
+
|
12 |
+
- Interactive Swagger UI at /docs
|
13 |
+
|
14 |
+
# π¦ Model
|
15 |
+
|
16 |
+
The API uses:
|
17 |
+
|
18 |
+
`` distilbert-base-uncased-finetuned-sst-2-english ``
|
19 |
+
|
20 |
+
A lightweight version of BERT fine-tuned on the SST-2 dataset for sentiment classification.
|
21 |
+
|
22 |
+
# Example
|
23 |
+
|
24 |
+
Request:
|
25 |
+
``
|
26 |
+
POST /predict
|
27 |
+
{
|
28 |
+
"text": "I absolutely love this product!"
|
29 |
+
}
|
30 |
+
``
|
31 |
+
|
32 |
+
Response:
|
33 |
+
``
|
34 |
+
{
|
35 |
+
"sentiment": "positive",
|
36 |
+
"confidence": 0.9981
|
37 |
+
}
|
38 |
+
``
|
39 |
+
|
40 |
+
# π File Structure
|
41 |
+
|
42 |
+
. </br>
|
43 |
+
βββ main.py # API implementation </br>
|
44 |
+
βββ requirements.txt # Python dependencies </br>
|
45 |
+
βββ README.md # Project documentation </br>
|
46 |
+
|
47 |
+
|
48 |
+
## π How to Use
|
49 |
+
|
50 |
+
1. Enter any English sentence in the input box
|
51 |
+
2. Click **Submit**
|
52 |
+
3. View the sentiment label and confidence
|
53 |
+
|
54 |
+
|
55 |
+
## π How to Deploy on Hugging Face Spaces
|
56 |
+
|
57 |
+
1. Create a Hugging Face account <br>
|
58 |
+
Sign up at https://huggingface.co/join
|
59 |
+
|
60 |
+
2. Create a new Space
|
61 |
+
Go to https://huggingface.co/spaces and click Create new Space
|
62 |
+
|
63 |
+
3. Fill out the form
|
64 |
+
- Space name: text-sentiment-api (or your choice)
|
65 |
+
- SDK: Gradio
|
66 |
+
- Visibility: Public or Private
|
67 |
+
|
68 |
+
4. Upload the files
|
69 |
+
- app.py
|
70 |
+
- requirements.txt
|
71 |
+
- README.md (this file)
|
72 |
+
|
73 |
+
5. Wait for the build
|
74 |
+
Hugging Face will automatically install dependencies and launch the app
|
75 |
+
|
76 |
+
6. Access your app
|
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/python3
|
2 |
+
|
3 |
+
"""
|
4 |
+
This file launches a simple web interface using Gradio to classify text sentiment
|
5 |
+
(positive/negative) using a pre-trained DistilBERT model. It is designed to run
|
6 |
+
locally or directly on Hugging Face Spaces.
|
7 |
+
|
8 |
+
@author mtzortzi
|
9 |
+
"""
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
from transformers import pipeline
|
13 |
+
|
14 |
+
# Load sentiment analysis model
|
15 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model = "distilbert-base-uncased-finetuned-sst-2-english")
|
16 |
+
|
17 |
+
def analyze_sentiment(text):
|
18 |
+
result = sentiment_analyzer(text)[0]
|
19 |
+
label = result['label'].capitalize()
|
20 |
+
score = round(result['score'], 4)
|
21 |
+
return f"Sentiment: {label} (Confidence: {score})"
|
22 |
+
|
23 |
+
# Gradio interface
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=analyze_sentiment,
|
26 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence for sentiment analysis..."),
|
27 |
+
outputs="text",
|
28 |
+
title="Text Sentiment Classifier",
|
29 |
+
description="Classifies text as Positive or Negative using a DistilBERT model trained on SST-2."
|
30 |
+
)
|
31 |
+
|
32 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.110.0
|
2 |
+
uvicorn==0.29.0
|
3 |
+
transformers==4.40.1
|
4 |
+
torch>=1.13
|
5 |
+
pydantic==1.10.13
|
6 |
+
gradio==4.25.0
|
7 |
+
|