Update main.py
Browse files
main.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# ✅ Set cache to writable directory
|
| 3 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 4 |
-
os.environ["HF_HOME"] = "/tmp/hf-home"
|
| 5 |
-
|
| 6 |
from fastapi import FastAPI, Request
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
class
|
| 17 |
text: str
|
| 18 |
|
| 19 |
@app.post("/classify")
|
| 20 |
-
async def
|
| 21 |
-
candidate_labels = ["contains electronic components", "
|
| 22 |
-
result = classifier(
|
| 23 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load zero-shot classification pipeline
|
| 6 |
+
classifier = pipeline(
|
| 7 |
+
"zero-shot-classification",
|
| 8 |
+
model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0"
|
| 9 |
+
)
|
| 10 |
|
| 11 |
+
# Define FastAPI app
|
| 12 |
+
app = FastAPI()
|
| 13 |
|
| 14 |
+
# Define input schema
|
| 15 |
+
class InputText(BaseModel):
|
| 16 |
text: str
|
| 17 |
|
| 18 |
@app.post("/classify")
|
| 19 |
+
async def classify_text(data: InputText):
|
| 20 |
+
candidate_labels = ["contains electronic components", "does not contain electronic components"]
|
| 21 |
+
result = classifier(data.text, candidate_labels, multi_label=False)
|
| 22 |
+
return {
|
| 23 |
+
"input": data.text,
|
| 24 |
+
"label": result["labels"][0],
|
| 25 |
+
"score": result["scores"][0]
|
| 26 |
+
}
|