Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitattributes +1 -0
- Dockerfile +17 -0
- ml_api.py +58 -0
- pneumonia_mode.sav +3 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
pneumonia_mode.sav filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a base image
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy everything into the container
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose the API port
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Run the FastAPI app
|
| 17 |
+
CMD ["uvicorn", "ml_api:app", "--host", "0.0.0.0", "--port", "7860"]
|
ml_api.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
import uvicorn
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import pickle
|
| 10 |
+
import requests
|
| 11 |
+
import threading
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Load the trained model
|
| 16 |
+
model = pickle.load(open('pneumonia_mode.sav', 'rb'))
|
| 17 |
+
|
| 18 |
+
IMAGE_SIZE = (150, 150)
|
| 19 |
+
|
| 20 |
+
def preprocess_image(image_data):
|
| 21 |
+
img = Image.open(BytesIO(image_data)).convert("RGB")
|
| 22 |
+
img = img.resize(IMAGE_SIZE)
|
| 23 |
+
img_array = np.array(img) / 255.0 # Normalize
|
| 24 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 25 |
+
return img_array
|
| 26 |
+
|
| 27 |
+
@app.post("/predict/")
|
| 28 |
+
async def predict(file: UploadFile = File(...)):
|
| 29 |
+
image_data = await file.read()
|
| 30 |
+
img_array = preprocess_image(image_data)
|
| 31 |
+
probability = model.predict(img_array)[0][0]
|
| 32 |
+
|
| 33 |
+
label = "Pneumonia" if probability > 0.5 else "Normal"
|
| 34 |
+
confidence = float(probability) if probability > 0.5 else float(1 - probability)
|
| 35 |
+
|
| 36 |
+
return {"prediction": label, "confidence": confidence}
|
| 37 |
+
|
| 38 |
+
# Start FastAPI server in a separate thread to prevent event loop issues
|
| 39 |
+
def start_server():
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
# Run the server in a separate thread
|
| 44 |
+
server_thread = threading.Thread(target=start_server, daemon=True)
|
| 45 |
+
server_thread.start()
|
| 46 |
+
|
| 47 |
+
# Wait a moment to ensure the server starts
|
| 48 |
+
import time
|
| 49 |
+
time.sleep(2)
|
| 50 |
+
|
| 51 |
+
# Send a request to the API
|
| 52 |
+
url = "http://127.0.0.1:8000/predict"
|
| 53 |
+
file_path = "/Users/singh/Downloads/images.jpg"
|
| 54 |
+
with open(file_path, "rb") as f:
|
| 55 |
+
files = {"file": f}
|
| 56 |
+
response = requests.post(url, files=files)
|
| 57 |
+
|
| 58 |
+
print(response.json())
|
pneumonia_mode.sav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b362f1b8dfff7b1adec53b906da6072db50bdd07795f38ea2defa29c5a7adbf
|
| 3 |
+
size 62110265
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
numpy
|
| 4 |
+
tensorflow
|
| 5 |
+
pillow
|
| 6 |
+
pickle-mixin
|
| 7 |
+
requests
|