Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from torch import nn
|
|
4 |
from torchvision import models, transforms
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
# Define the number of classes
|
9 |
num_classes = 2 # Update with the actual number of classes in your dataset (e.g., 2 for healthy and anomalous)
|
@@ -62,4 +63,28 @@ iface = gr.Interface(
|
|
62 |
)
|
63 |
|
64 |
# Launch the Gradio interface
|
65 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from torchvision import models, transforms
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
from PIL import Image
|
7 |
+
from flask import Flask, request, jsonify
|
8 |
|
9 |
# Define the number of classes
|
10 |
num_classes = 2 # Update with the actual number of classes in your dataset (e.g., 2 for healthy and anomalous)
|
|
|
63 |
)
|
64 |
|
65 |
# Launch the Gradio interface
|
66 |
+
iface.launch(share=True)
|
67 |
+
|
68 |
+
# Create a Flask app
|
69 |
+
app = Flask(__name__)
|
70 |
+
|
71 |
+
# Define the API endpoint
|
72 |
+
@app.route('/predict', methods=['POST'])
|
73 |
+
def api_predict():
|
74 |
+
try:
|
75 |
+
data = request.json
|
76 |
+
image_path = data['inputs']
|
77 |
+
|
78 |
+
# Load the image
|
79 |
+
image = Image.open(image_path)
|
80 |
+
|
81 |
+
# Perform prediction
|
82 |
+
result = predict(image)
|
83 |
+
|
84 |
+
return jsonify({"result": result})
|
85 |
+
except Exception as e:
|
86 |
+
return jsonify({"error": str(e)}), 400
|
87 |
+
|
88 |
+
# Run the Flask app
|
89 |
+
if __name__ == '__main__':
|
90 |
+
app.run(host='0.0.0.0', port=8000)
|