Spaces:
Sleeping

File size: 1,224 Bytes
79cbb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 flask import Flask, request, jsonify
import torch
import gdown
import os
from model import predict

app = Flask(__name__)

# Google Drive File ID (replace with your actual ID)
MODEL_ID = "1RNhOLcAuVUru4kpnnyFvWeL1Fu0HIOom"
MODEL_PATH = "model.pth"

# Function to download model if not present
def download_model():
    if not os.path.exists(MODEL_PATH):
        print("Downloading model from Google Drive...")
        gdown.download(f"https://drive.google.com/uc?id={MODEL_ID}", MODEL_PATH, quiet=False)

# Ensure model is downloaded before starting
download_model()

@app.route('/')
def home():
    return "Diabetic Retinopathy Classification API is running!"

@app.route('/predict', methods=['POST'])
def classify():
    if 'file' not in request.files:
        return jsonify({"error": "No file uploaded"}), 400
    
    file = request.files['file']
    if file.filename == '':
        return jsonify({"error": "No selected file"}), 400
    
    file_path = "temp_image.jpg"
    file.save(file_path)
    
    label, probabilities = predict(file_path)
    os.remove(file_path)
    
    return jsonify({"class": label, "probabilities": probabilities})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)