czar-ml commited on
Commit
bfc4c06
·
1 Parent(s): 8c4d0c2

fix: set main.py tracking URI-5

Browse files
Files changed (1) hide show
  1. app/main.py +12 -3
app/main.py CHANGED
@@ -6,7 +6,8 @@ import mlflow
6
  import time
7
  import uuid
8
 
9
- app = FastAPI(title="Sentiment Analysis API")
 
10
 
11
  # Initialize model once on startup
12
  classifier = pipeline(
@@ -14,6 +15,14 @@ classifier = pipeline(
14
  model="distilbert-base-uncased-finetuned-sst-2-english"
15
  )
16
 
 
 
 
 
 
 
 
 
17
  class PredictRequest(BaseModel):
18
  text: str = Field(..., min_length=1, max_length=512)
19
 
@@ -34,13 +43,13 @@ async def predict(request: PredictRequest) -> PredictResponse:
34
 
35
  mlflow.log_metric("inference_latency_ms", (time.time() - start_time) * 1000)
36
  mlflow.log_param("input_text_length", len(request.text))
37
- mlflow.log_metric("confidence_score", result['score'])
38
 
39
  return PredictResponse(
40
  request_id=str(uuid.uuid4()),
41
  text=request.text,
42
  sentiment=result['label'].upper(), # POSITIVE or NEGATIVE
43
- confidence=result['score']
44
  )
45
 
46
  @app.get("/health")
 
6
  import time
7
  import uuid
8
 
9
+ # Initialize FastAPI
10
+ app = FastAPI()
11
 
12
  # Initialize model once on startup
13
  classifier = pipeline(
 
15
  model="distilbert-base-uncased-finetuned-sst-2-english"
16
  )
17
 
18
+ # Setup MLflow experiment
19
+ mlflow.set_tracking_uri("file:///home/appuser/mlruns")
20
+ experiment_name = "sentiment-analysis"
21
+ if mlflow.get_experiment_by_name(experiment_name) is None:
22
+ mlflow.create_experiment(experiment_name)
23
+ mlflow.set_experiment(experiment_name)
24
+
25
+
26
  class PredictRequest(BaseModel):
27
  text: str = Field(..., min_length=1, max_length=512)
28
 
 
43
 
44
  mlflow.log_metric("inference_latency_ms", (time.time() - start_time) * 1000)
45
  mlflow.log_param("input_text_length", len(request.text))
46
+ mlflow.log_metric("confidence_score", float(result['score'])
47
 
48
  return PredictResponse(
49
  request_id=str(uuid.uuid4()),
50
  text=request.text,
51
  sentiment=result['label'].upper(), # POSITIVE or NEGATIVE
52
+ confidence=float(result['score'])
53
  )
54
 
55
  @app.get("/health")