Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,57 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
from datasets import load_dataset
|
4 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
5 |
import torch
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# Step 7: Train the model
|
63 |
-
try:
|
64 |
-
trainer.train()
|
65 |
-
print("Training completed successfully.")
|
66 |
-
except Exception as e:
|
67 |
-
print(f"Failed to train the model. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from fastapi import FastAPI, UploadFile, File
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from io import BytesIO
|
5 |
+
from PIL import Image
|
6 |
+
from torchvision import transforms
|
7 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
8 |
+
|
9 |
+
# Initialize the FastAPI app
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Load the model and feature extractor
|
13 |
+
model = AutoModelForImageClassification.from_pretrained("yolac/BacterialMorphologyClassification")
|
14 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("yolac/BacterialMorphologyClassification")
|
15 |
+
|
16 |
+
# Define the image preprocessing transform
|
17 |
+
preprocess = transforms.Compose([
|
18 |
+
transforms.Resize((224, 224)), # Adjust size as needed for your model
|
19 |
+
transforms.ToTensor(),
|
20 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
21 |
+
])
|
22 |
+
|
23 |
+
# Helper function to preprocess the image
|
24 |
+
def preprocess_image(image: Image.Image):
|
25 |
+
image = image.convert("RGB") # Ensure the image is in RGB format
|
26 |
+
image = preprocess(image)
|
27 |
+
image = image.unsqueeze(0) # Add a batch dimension
|
28 |
+
return image
|
29 |
+
|
30 |
+
# Define the prediction endpoint
|
31 |
+
@app.post("/predict/")
|
32 |
+
async def predict(file: UploadFile = File(...)):
|
33 |
+
try:
|
34 |
+
# Read the uploaded image
|
35 |
+
image_data = await file.read()
|
36 |
+
image = Image.open(BytesIO(image_data))
|
37 |
+
|
38 |
+
# Preprocess the image
|
39 |
+
image_tensor = preprocess_image(image)
|
40 |
+
|
41 |
+
# Perform inference
|
42 |
+
model.eval()
|
43 |
+
with torch.no_grad():
|
44 |
+
outputs = model(image_tensor)
|
45 |
+
logits = outputs.logits # Get the model's raw output
|
46 |
+
predicted_class_idx = torch.argmax(logits, dim=1).item()
|
47 |
+
|
48 |
+
# Map the predicted class index to the class labels
|
49 |
+
class_labels = ["Cocci", "Bacilli", "Spirilla"] # Replace with your actual class labels
|
50 |
+
predicted_class = class_labels[predicted_class_idx]
|
51 |
+
|
52 |
+
return {"predicted_class": predicted_class}
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
return {"error": str(e)}
|
56 |
+
|
57 |
+
# Run the app using the command: uvicorn app:app --reload
|
|
|
|
|
|
|
|
|
|
|
|