Skip to content

Commit

Permalink
Adding text classifier example
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuri committed Dec 14, 2024
1 parent e1a7b58 commit c9eb4bb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.ipynb_checkpoints
.DS_Store
text-classifier/__pycache__
text-classifier/venv
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Neural Networks Programming
# Machine Learning Programming

1. [Linear Regression](https://github.com/jpuri/neural-networks/tree/main/linear-regression)
2. [Shallow Neural Network](https://github.com/jpuri/neural-networks/tree/main/shallow-neural-network)
3. [Deep Neural Network](https://github.com/jpuri/neural-networks/tree/main/deep-neural-network)
4. [Text Classification](https://github.com/jpuri/neural-networks/tree/main/text-classifier)
12 changes: 12 additions & 0 deletions text-classifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Text Classification

Project setup:

```bash
pip install -r requirements.txt
python3 text_classifier.py
```

Open url like: `http://0.0.0.0:8000/classify?text=This is a test sentence.`

The api is deployed at: [https://text-classifier-na9p.onrender.com/classify?text=This is a test sentence](https://text-classifier-na9p.onrender.com/classify?text=This is a test sentence.).
17 changes: 17 additions & 0 deletions text-classifier/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
annotated-types==0.7.0
anyio==4.7.0
click==8.1.7
fastapi==0.115.6
h11==0.14.0
idna==3.10
joblib==1.4.2
nltk==3.9.1
pydantic==2.10.3
pydantic_core==2.27.1
regex==2024.11.6
sniffio==1.3.1
starlette==0.41.3
textblob==0.18.0.post0
tqdm==4.67.1
typing_extensions==4.12.2
uvicorn==0.32.1
29 changes: 29 additions & 0 deletions text-classifier/text_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from fastapi import FastAPI
from textblob import TextBlob
from textblob.classifiers import NaiveBayesClassifier
import nltk
import uvicorn

nltk.download('punkt')
nltk.download('punkt_tab')

data = [
('I love my country.', 'pos'),
('This is an amazing place!', 'pos'),
('I do not like the smell of this place.', 'neg'),
('I do not like this restaurant', 'neg'),
('I am tired of hearing your nonsense.', 'neg'),
("I always aspire to be like him", 'pos'),
("It's a horrible performance.", "neg")
]

model = NaiveBayesClassifier(data)

app = FastAPI()

@app.get("/classify")
async def read_root(text: str):
return { "class": model.classify(text) }

if __name__ == "__main__":
uvicorn.run("text_classifier:app", host="0.0.0.0", port=8000, reload=False)

0 comments on commit c9eb4bb

Please sign in to comment.