Fasika
commited on
Commit
·
20c7ee4
1
Parent(s):
ded1bfb
- app.py +2 -31
- requirements.txt +0 -4
app.py
CHANGED
@@ -1,36 +1,7 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from typing import List
|
4 |
-
import torch
|
5 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
# Load model and tokenizer once at startup
|
10 |
-
checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
|
11 |
-
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
12 |
-
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
|
13 |
-
|
14 |
-
class TextData(BaseModel):
|
15 |
-
texts: List[str]
|
16 |
-
|
17 |
@app.get("/")
|
18 |
-
def
|
19 |
-
return {"
|
20 |
-
|
21 |
-
@app.post("/predict")
|
22 |
-
def predict(data: TextData):
|
23 |
-
# Tokenize the input texts
|
24 |
-
tokens = tokenizer(data.texts, padding=True, truncation=True, return_tensors="pt")
|
25 |
-
|
26 |
-
# Perform inference without gradient tracking
|
27 |
-
with torch.no_grad():
|
28 |
-
outputs = model(**tokens)
|
29 |
-
|
30 |
-
# Calculate softmax probabilities and determine the predictions
|
31 |
-
predictions = torch.argmax(outputs.logits, dim=-1).tolist()
|
32 |
-
|
33 |
-
# Prepare the response with texts and their corresponding predictions
|
34 |
-
results = [{"text": text, "prediction": prediction} for text, prediction in zip(data.texts, predictions)]
|
35 |
-
|
36 |
-
return {"results": results}
|
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
+
def greet_json():
|
7 |
+
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,6 +1,2 @@
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
3 |
-
pydantic
|
4 |
-
typing
|
5 |
-
torch
|
6 |
-
transformers
|
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
|
|
|
|
|
|
|