ADK09 commited on
Commit
5c50bbf
·
1 Parent(s): 531c353

Initial Commit

Browse files
Files changed (9) hide show
  1. .dockerignore +6 -0
  2. .gitignore +4 -0
  3. CONTRIBUTING.md +35 -0
  4. Dockerfile +20 -0
  5. LICENSE +26 -0
  6. README.md +68 -0
  7. app.py +37 -0
  8. requirements.txt +6 -0
  9. test_app.py +24 -0
.dockerignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ .git/
6
+ *.log
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
CONTRIBUTING.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Contributing to Speak Your Mind FastAPI
2
+
3
+ Thank you for your interest in contributing!
4
+
5
+ ## How to Contribute
6
+ 1. **Fork** the repository.
7
+ 2. **Clone** your fork:
8
+ ```bash
9
+ git clone https://huggingface.co/spaces/ADK09/speak-your-mind-fastapi
10
+ cd speak-your-mind-fastapi
11
+ ```
12
+ 3. **Create a new branch** for your feature or fix:
13
+ ```bash
14
+ git checkout -b feature-name
15
+ ```
16
+ 4. **Make changes** and commit:
17
+ ```bash
18
+ git commit -m "Add feature XYZ"
19
+ ```
20
+ 5. **Push to your branch** and create a **pull request**.
21
+
22
+ ## Guidelines
23
+ - Follow **PEP8** coding standards.
24
+ - Write **clear commit messages**.
25
+ - Keep PRs **small and focused**.
26
+ - **Do not modify the model itself**, as we are only using `cardiffnlp/twitter-roberta-base-emotion-latest`.
27
+
28
+ ## Running Tests
29
+ Before submitting a pull request, run:
30
+ ```bash
31
+ pytest test_app.py
32
+ ```
33
+
34
+ Happy coding!
35
+ ```
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+
6
+ ENV HF_HOME=/app/cache
7
+ ENV PATH="/home/user/.local/bin:$PATH"
8
+
9
+ WORKDIR /app
10
+
11
+ COPY --chown=user:user requirements.txt requirements.txt
12
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
13
+
14
+ RUN mkdir -p /app/cache && chown -R user:user /app/cache
15
+
16
+ COPY --chown=user:user . /app
17
+
18
+ EXPOSE 7860
19
+
20
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
LICENSE ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 ADK09
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ ## Third-Party Acknowledgment
16
+ This project **only uses** the pre-trained model **`cardiffnlp/twitter-roberta-base-emotion-latest`**,
17
+ which is publicly available under its respective license at **Hugging Face**:
18
+ [https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion-latest](https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion-latest)
19
+
20
+ This project **does not fine-tune or modify the model** in any way.
21
+
22
+ ## Disclaimer
23
+ The software is provided "as is", without warranty of any kind, express or implied,
24
+ including but not limited to the warranties of merchantability, fitness for a particular purpose,
25
+ and non-infringement. In no event shall the authors be liable for any claim, damages, or other liability
26
+ arising from the use of this software.
README.md CHANGED
@@ -9,3 +9,71 @@ license: mit
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
12
+
13
+ # Speak Your Mind FastAPI
14
+
15
+ **Speak Your Mind** is an **emotion detection API** built with **FastAPI**.
16
+ It uses the **pre-trained RoBERTa model** [`cardiffnlp/twitter-roberta-base-emotion-latest`](https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion-latest) to analyze emotions from text.
17
+
18
+ ## Features
19
+ - **FastAPI-based REST API** running on **port 7860**.
20
+ - Uses **pre-trained RoBERTa model** for emotion classification.
21
+ - **Dockerized for easy deployment**.
22
+
23
+ ## Model Information
24
+ This API uses the **`cardiffnlp/twitter-roberta-base-emotion-latest`** model without any additional training or fine-tuning.
25
+ It predicts emotions such as **joy, sadness, optimism, and more**.
26
+
27
+ - **Model Source:** [Hugging Face Model Page](https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion-latest)
28
+ - **No additional fine-tuning or training has been done**.
29
+ - **Supported Emotions:** Joy, Sadness, Optimism, etc.
30
+
31
+ ## 🛠️ Installation & Usage
32
+
33
+ ### ▶ Run Locally
34
+ ```bash
35
+ git clone https://huggingface.co/spaces/ADK09/speak-your-mind-fastapi
36
+ cd speak-your-mind-fastapi
37
+ pip install -r requirements.txt
38
+ uvicorn app:app --host 0.0.0.0 --port 7860
39
+ ```
40
+ Access the API at `http://localhost:7860/docs`
41
+
42
+ ### ▶ Run with Docker
43
+ ```bash
44
+ docker build -t speak-your-mind .
45
+ docker run -p 7860:7860 speak-your-mind
46
+ ```
47
+
48
+ ## API Endpoints
49
+ | Method | Endpoint | Description |
50
+ |--------|----------|-------------|
51
+ | `GET` | `/` | Check if API is running |
52
+ | `POST` | `/classify-emotion` | Predict emotion from text |
53
+
54
+ ### Example Request
55
+ ```json
56
+ {
57
+ "text": "I feel fantastic today!"
58
+ }
59
+ ```
60
+ ### Example Response
61
+ ```json
62
+ {
63
+ "predicted_emotion": "joy"
64
+ }
65
+ ```
66
+
67
+ ## Disclaimer
68
+ This API **only uses the pre-trained model** and does not modify or fine-tune it.
69
+ Predictions are based on the **original model's training data**
70
+
71
+ ## License
72
+ This project is licensed under the **MIT License**.
73
+
74
+ ## Acknowledgments
75
+ This project **uses the RoBERTa model fine-tuned by Cardiff NLP**.
76
+ Special thanks to the **Hugging Face** and **Cardiff NLP teams**.
77
+
78
+ - [`cardiffnlp/twitter-roberta-base-emotion-latest`](https://huggingface.co/cardiffnlp/twitter-roberta-base-emotion-latest)
79
+ ```
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import torch
5
+
6
+ app = FastAPI()
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-emotion-latest")
9
+ model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-emotion-latest")
10
+
11
+ class TextRequest(BaseModel):
12
+ text: str
13
+
14
+ @app.get("/")
15
+ def home():
16
+ return {"message": "Speak your mind emotion API is running"}
17
+
18
+ @app.post("/classify-emotion")
19
+ async def classify_emotion(request: TextRequest):
20
+ try:
21
+ text = request.text
22
+
23
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
24
+
25
+ with torch.no_grad():
26
+ outputs = model(**inputs)
27
+
28
+
29
+ logits = outputs.logits
30
+ predicted_class_id = torch.argmax(logits, dim=-1).item()
31
+ predicted_emotion = model.config.id2label[predicted_class_id]
32
+
33
+ return {"predicted_emotion": predicted_emotion}
34
+
35
+ except Exception as e:
36
+ raise HTTPException(status_code=500, detail=str(e))
37
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ httpx
6
+ pytest
test_app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.testclient import TestClient
2
+ from app import app
3
+
4
+ client = TestClient(app)
5
+
6
+ def test_home():
7
+ response = client.get("/")
8
+ assert response.status_code == 200
9
+ assert response.json() == {"message": "Speak your mind emotion API is running"}
10
+
11
+ def test_predict():
12
+ test_input = {"text": "I feel fantastic today!"}
13
+
14
+ response = client.post("/classify-emotion", json=test_input)
15
+
16
+ assert response.status_code == 200
17
+ assert "predicted_emotion" in response.json()
18
+ assert isinstance(response.json()["predicted_emotion"], str)
19
+
20
+ if __name__ == "__main__":
21
+ test_home()
22
+ test_predict()
23
+ print("All tests passed successfully!")
24
+