mfkhaneducationtwo commited on
Commit
ab51942
·
verified ·
1 Parent(s): fb39efb

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +22 -0
  2. Usage.py +21 -0
  3. main.py +33 -0
  4. model.h5 +3 -0
  5. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use official python base image
2
+ FROM python:3.10-slim
3
+
4
+ # Install required system packages
5
+ RUN apt-get update && apt-get install -y build-essential
6
+
7
+ # Set working directory
8
+ WORKDIR /code
9
+
10
+ # Copy local files into container
11
+ COPY requirements.txt .
12
+ COPY main.py .
13
+ COPY model.h5 .
14
+
15
+ # Install Python dependencies
16
+ RUN pip install --no-cache-dir -r requirements.txt
17
+
18
+ # Expose port
19
+ EXPOSE 7860
20
+
21
+ # Start the server
22
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
Usage.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ # Your deployed API URL
4
+ api_url = "https://ali124k-124-HP-1.hf.space/predict"
5
+
6
+ # Path to the image you want to classify
7
+ image_path = "Apple_scab.jpg" # <-- replace this with your image path
8
+
9
+ # Open image file in binary mode
10
+ with open(image_path, "rb") as image_file:
11
+ files = {"file": image_file}
12
+ response = requests.post(api_url, files=files)
13
+
14
+ # Check response status
15
+ if response.status_code == 200:
16
+ result = response.json()
17
+ print("Prediction:", result["prediction"])
18
+ print("Confidence:", result["confidence"])
19
+ else:
20
+ print("Error:", response.status_code)
21
+ print(response.text)
main.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ import numpy as np
3
+ from PIL import Image
4
+ import io
5
+ from tensorflow import keras
6
+
7
+ # Load model
8
+ model = keras.models.load_model('model.h5')
9
+
10
+ # Class labels (ordered list)
11
+ class_labels = [
12
+ 'COVID19', 'NORMAL', 'PNEUMONIA'
13
+ ]
14
+
15
+ # Initialize FastAPI app
16
+ app = FastAPI()
17
+
18
+ # Preprocess function
19
+ def preprocess_image(image_bytes):
20
+ image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
21
+ image = image.resize((224, 224))
22
+ img_array = np.array(image) / 255.0 # normalize (assuming your model was trained with normalization)
23
+ img_array = np.expand_dims(img_array, axis=0) # add batch dimension
24
+ return img_array
25
+
26
+ @app.post("/predict")
27
+ async def predict(file: UploadFile = File(...)):
28
+ image_bytes = await file.read()
29
+ img_array = preprocess_image(image_bytes)
30
+ predictions = model.predict(img_array)
31
+ predicted_class = class_labels[np.argmax(predictions)]
32
+ confidence = float(np.max(predictions))
33
+ return {"prediction": predicted_class, "confidence": confidence}
model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:50773612987f3a24633d93ea7e7cff08480b0ddd23e0ea07a623b4803726e279
3
+ size 136038112
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pillow
4
+ tensorflow
5
+ python-multipart
6
+ numpy