-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from danilyef/pr3_fashapi
PR3: Write a FastAPI server for your model, with tests and CI integration.
- Loading branch information
Showing
15 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
## Run | ||
```bash | ||
pip install -r requirements.txt | ||
streamlit run project/main.py | ||
streamlit run main.py | ||
``` | ||
|
||
## Tests | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
## Run | ||
```bash | ||
pip install -r requirements.txt | ||
python project/main.py | ||
python main.py | ||
``` | ||
|
||
## Tests | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## Run | ||
|
||
```bash | ||
cd homework_9 | ||
uvicorn pr3.app:app --reload | ||
``` | ||
|
||
## Tests |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from fastapi import FastAPI | ||
from pydantic import BaseModel | ||
from .utils import Model | ||
|
||
model = Model(model_name='distilbert-base-uncased-finetuned-sst-2-english') | ||
|
||
app = FastAPI() | ||
|
||
|
||
class SentimentRequest(BaseModel): | ||
text: str | ||
|
||
class SentimentResponse(BaseModel): | ||
text: str | ||
sentiment: str | ||
probability: float | ||
|
||
class ProbabilityResponse(BaseModel): | ||
text: str | ||
probability: float | ||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"message": "Welcome to the sentiment analysis API"} | ||
|
||
@app.post("/predict") | ||
def predict_sentiment(request: SentimentRequest) -> SentimentResponse: | ||
label = model.predict(request.text) | ||
probability = model.predict_proba(request.text) | ||
return SentimentResponse( | ||
text=request.text, | ||
sentiment=label, | ||
probability=float(probability) | ||
) | ||
|
||
@app.post("/probability") | ||
def get_probability(request: SentimentRequest) -> ProbabilityResponse: | ||
probability = model.predict_proba(request.text) | ||
return ProbabilityResponse( | ||
text=request.text, | ||
probability=float(probability) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification | ||
import torch | ||
|
||
class Model: | ||
def __init__(self, model_name="distilbert-base-uncased-finetuned-sst-2-english"): | ||
self.tokenizer = DistilBertTokenizer.from_pretrained(model_name) | ||
self.model = DistilBertForSequenceClassification.from_pretrained(model_name) | ||
self.model.eval() | ||
|
||
def predict(self, text): | ||
inputs = self.tokenizer( | ||
text, return_tensors="pt", truncation=True, padding=True | ||
) | ||
with torch.no_grad(): | ||
outputs = self.model(**inputs) | ||
predicted_class_id = torch.argmax(outputs.logits, dim=1).item() | ||
return self.model.config.id2label[predicted_class_id] | ||
|
||
def predict_proba(self, text): | ||
inputs = self.tokenizer( | ||
text, return_tensors="pt", truncation=True, padding=True | ||
) | ||
with torch.no_grad(): | ||
outputs = self.model(**inputs) | ||
probabilities = torch.softmax(outputs.logits, dim=1) | ||
return probabilities.squeeze().max().item() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from fastapi.testclient import TestClient | ||
from pr3.app import app | ||
|
||
client = TestClient(app) | ||
|
||
def test_read_root(): | ||
response = client.get("/") | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "Welcome to the sentiment analysis API"} | ||
|
||
def test_predict_sentiment(): | ||
# Test with positive text | ||
response = client.post( | ||
"/predict", | ||
json={"text": "I love this movie!"} | ||
) | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert "text" in data | ||
assert "sentiment" in data | ||
assert "probability" in data | ||
assert data["text"] == "I love this movie!" | ||
assert isinstance(data["sentiment"], str) | ||
assert isinstance(data["probability"], float) | ||
assert 0 <= data["probability"] <= 1 | ||
|
||
# Test with negative text | ||
response = client.post( | ||
"/predict", | ||
json={"text": "I hate this movie!"} | ||
) | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert "text" in data | ||
assert "sentiment" in data | ||
assert "probability" in data | ||
assert data["text"] == "I hate this movie!" | ||
assert isinstance(data["sentiment"], str) | ||
assert isinstance(data["probability"], float) | ||
assert 0 <= data["probability"] <= 1 | ||
|
||
def test_get_probability(): | ||
response = client.post( | ||
"/probability", | ||
json={"text": "This is a test message"} | ||
) | ||
assert response.status_code == 200 | ||
data = response.json() | ||
assert "text" in data | ||
assert "probability" in data | ||
assert data["text"] == "This is a test message" | ||
assert isinstance(data["probability"], float) | ||
assert 0 <= data["probability"] <= 1 | ||
|
||
def test_invalid_request(): | ||
# Test missing text field | ||
response = client.post( | ||
"/predict", | ||
json={} | ||
) | ||
assert response.status_code == 422 | ||
|
||
response = client.post( | ||
"/probability", | ||
json={} | ||
) | ||
assert response.status_code == 422 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import pytest | ||
from pr1.func_st.utils import Model | ||
from pr1.utils import Model | ||
|
||
@pytest.fixture | ||
def model(): | ||
|