Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +46 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "-b", "0.0.0.0:7860", "app:app"]
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
from flask_cors import CORS
|
| 5 |
+
from transformers import ViTImageProcessor, AutoModelForImageClassification
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Initialize Flask app
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
CORS(app) # Enable CORS for all routes
|
| 13 |
+
|
| 14 |
+
# Load model and processor
|
| 15 |
+
processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
| 16 |
+
model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
| 17 |
+
|
| 18 |
+
# Classification function
|
| 19 |
+
def classify_image(image_url):
|
| 20 |
+
try:
|
| 21 |
+
image = Image.open(requests.get(image_url, stream=True).raw)
|
| 22 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
|
| 26 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 27 |
+
return model.config.id2label[predicted_class_idx]
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return str(e)
|
| 30 |
+
|
| 31 |
+
# API route to classify the image
|
| 32 |
+
@app.route('/api/classify', methods=['GET'])
|
| 33 |
+
def classify():
|
| 34 |
+
print('ran')
|
| 35 |
+
image_url = request.args.get('url')
|
| 36 |
+
print(image_url)
|
| 37 |
+
|
| 38 |
+
if not image_url:
|
| 39 |
+
return jsonify({'error': 'No image URL provided'}), 400
|
| 40 |
+
|
| 41 |
+
classification = classify_image(image_url)
|
| 42 |
+
return jsonify({'classification': classification})
|
| 43 |
+
|
| 44 |
+
# Run the Flask server
|
| 45 |
+
if __name__ == '__main__':
|
| 46 |
+
app.run(debug=True, host='0.0.0.0')
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask_cors
|
| 3 |
+
transformers
|
| 4 |
+
uvicorn[standard]
|
| 5 |
+
Pillow
|