Commit
·
9f48851
1
Parent(s):
d1fe2cb
feat: added model file
Browse files
main.py
CHANGED
@@ -1,46 +1,47 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
|
46 |
# # Launch FastAPI with Uvicorn
|
@@ -48,11 +49,9 @@
|
|
48 |
# uvicorn.run(app, host="0.0.0.0", port=8000, workers=1)
|
49 |
|
50 |
|
51 |
-
from fastapi import FastAPI
|
|
|
|
|
52 |
|
53 |
-
app=FastAPI()
|
54 |
|
55 |
-
@app.get("/")
|
56 |
-
def hello():
|
57 |
-
return {"hello":"success"}
|
58 |
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import RobertaTokenizerFast, RobertaForSequenceClassification, TextClassificationPipeline
|
4 |
+
import uvicorn
|
5 |
+
|
6 |
+
# Define FastAPI app
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Load Model on Startup
|
10 |
+
HUGGINGFACE_MODEL_PATH = "bespin-global/klue-roberta-small-3i4k-intent-classification"
|
11 |
+
|
12 |
+
print("Loading model...") # Log message
|
13 |
+
try:
|
14 |
+
loaded_tokenizer = RobertaTokenizerFast.from_pretrained(HUGGINGFACE_MODEL_PATH)
|
15 |
+
loaded_model = RobertaForSequenceClassification.from_pretrained(HUGGINGFACE_MODEL_PATH)
|
16 |
+
|
17 |
+
# Create Text Classification Pipeline
|
18 |
+
text_classifier = TextClassificationPipeline(
|
19 |
+
tokenizer=loaded_tokenizer,
|
20 |
+
model=loaded_model,
|
21 |
+
return_all_scores=True
|
22 |
+
)
|
23 |
+
print("Model loaded successfully.") # Log message
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error loading model: {e}")
|
26 |
+
text_classifier = None
|
27 |
+
|
28 |
+
|
29 |
+
# Define Request Model
|
30 |
+
class PredictionRequest(BaseModel):
|
31 |
+
text: str
|
32 |
+
|
33 |
+
@app.get("/")
|
34 |
+
def hello():
|
35 |
+
return {"Message":"Space is running Good.",
|
36 |
+
"Status":"Healthy"}
|
37 |
+
# Prediction Endpoint
|
38 |
+
@app.post("/predict")
|
39 |
+
def predict_intent(request: PredictionRequest):
|
40 |
+
if text_classifier is None:
|
41 |
+
return {"error": "Model not found"}
|
42 |
+
preds_list = text_classifier(request.text)
|
43 |
+
best_pred = max(preds_list[0], key=lambda x: x["score"]) # Get highest-scoring intent
|
44 |
+
return {"intent": best_pred["label"], "confidence": best_pred["score"]}
|
45 |
|
46 |
|
47 |
# # Launch FastAPI with Uvicorn
|
|
|
49 |
# uvicorn.run(app, host="0.0.0.0", port=8000, workers=1)
|
50 |
|
51 |
|
52 |
+
# from fastapi import FastAPI
|
53 |
+
|
54 |
+
# app=FastAPI()
|
55 |
|
|
|
56 |
|
|
|
|
|
|
|
57 |
|