-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (40 loc) · 1.55 KB
/
Copy pathmain.py
File metadata and controls
50 lines (40 loc) · 1.55 KB
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
47
48
49
50
# -*- coding: utf-8 -*-
"""Prediction Server for Vertex AI
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1eml86CidWa8WEvCNAPPRwoFRmbNFVsuP
"""
from fastapi import FastAPI, Request
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import os
# Initialize the FastAPI app
app = FastAPI()
# --- Load the Fine-Tuned Model ---
# The model will be packaged inside the container with this script.
model_path = "./climatebert_misinfo_model"
classifier = None
# This event handler runs when the server starts up.
@app.on_event("startup")
def load_model():
global classifier
if os.path.isdir(model_path):
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
print("✅ ClimateBERT model loaded successfully!")
else:
print(f"--- FATAL ERROR: Model not found at {model_path} ---")
# Define the prediction endpoint
@app.post("/predict")
async def predict(request: Request):
data = await request.json()
text = data.get("text", "")
if not text or not classifier:
return {"error": "Text is missing or model is not loaded."}
# The pipeline returns a list of dictionaries, e.g., [{'label': 'news', 'score': 0.99}]
prediction = classifier(text)[0]
return prediction
# Health check endpoint for Vertex AI
@app.get("/health")
def health():
return {"status": "ok"}