Spaces:
Sleeping
Sleeping
File size: 1,653 Bytes
9497244 | 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 | from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import torch
import io
app = FastAPI()
# Enable CORS so the React frontend can communicate with this API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, change this to your frontend URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
device = torch.device("cpu") # Use CPU for standard web hosting unless paying for GPU servers
print("Loading model into server memory...")
# Point this to your fine-tuned local folder, or the base model if testing
model_path = "Vdv26/trocr-captcha-finetuned"
processor = TrOCRProcessor.from_pretrained(model_path)
model = VisionEncoderDecoderModel.from_pretrained(model_path).to(device)
@app.post("/api/predict")
async def predict_captcha(file: UploadFile = File(...)):
# 1. Read the uploaded image bytes from the internet
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
# 2. Run inference
pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
with torch.no_grad():
generated_ids = model.generate(pixel_values, max_new_tokens=10)
prediction = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
# 3. Return the JSON response to the frontend
return {"filename": file.filename, "prediction": prediction.replace(' ', '')}
# Run locally using: uvicorn main:app --reload |