|
from fastapi import FastAPI |
|
from pydantic import BaseModel |
|
from transformers import pipeline, DistilBertForSequenceClassification, AutoTokenizer |
|
import os |
|
import uvicorn |
|
|
|
|
|
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.on_event("startup") |
|
async def load_model(): |
|
global sentiment_model |
|
model = DistilBertForSequenceClassification.from_pretrained("tommyliphys/ai-detector-distilbert", from_tf=True) |
|
tokenizer = AutoTokenizer.from_pretrained("tommyliphys/ai-detector-distilbert") |
|
sentiment_model = pipeline("text-classification", model=model, tokenizer=tokenizer) |
|
|
|
|
|
class TextRequest(BaseModel): |
|
text: str |
|
|
|
|
|
@app.post("/predict") |
|
async def predict(request: TextRequest): |
|
try: |
|
result = sentiment_model(request.text) |
|
return {"result": result} |
|
except Exception as e: |
|
return {"error": str(e)} |
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |