Spaces:
Sleeping
Sleeping
Commit
·
8869a1f
1
Parent(s):
08a0af6
Add Application File
Browse files- Dockerfile +21 -0
- app.py +33 -0
- quantized_model/config.json +34 -0
- quantized_model/model_quantized.pt +3 -0
- quantized_model/preprocessor_config.json +23 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
# Avoid Python .pyc files
|
4 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
5 |
+
ENV PYTHONUNBUFFERED=1
|
6 |
+
|
7 |
+
# Set up user and workspace
|
8 |
+
RUN useradd -m -u 1000 user
|
9 |
+
USER user
|
10 |
+
WORKDIR /app
|
11 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
12 |
+
|
13 |
+
# Copy and install dependencies
|
14 |
+
COPY --chown=user requirements.txt .
|
15 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
16 |
+
|
17 |
+
# Copy all app code
|
18 |
+
COPY --chown=user . .
|
19 |
+
|
20 |
+
# Run the app
|
21 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from transformers import AutoProcessor, AutoConfig, AutoModelForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import io
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Load processor and config
|
10 |
+
processor = AutoProcessor.from_pretrained("quantized_model")
|
11 |
+
config = AutoConfig.from_pretrained("dima806/deepfake_vs_real_image_detection")
|
12 |
+
|
13 |
+
# Load model architecture and quantized weights
|
14 |
+
model = AutoModelForImageClassification.from_config(config)
|
15 |
+
model_quantized = torch.quantization.quantize_dynamic(
|
16 |
+
model, {torch.nn.Linear}, dtype=torch.qint8
|
17 |
+
)
|
18 |
+
model_quantized.load_state_dict(torch.load("quantized_model/model_quantized.pt", map_location="cpu"))
|
19 |
+
model_quantized.eval()
|
20 |
+
|
21 |
+
@app.post("/predict")
|
22 |
+
async def predict(file: UploadFile = File(...)):
|
23 |
+
contents = await file.read()
|
24 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
25 |
+
|
26 |
+
inputs = processor(images=image, return_tensors="pt")
|
27 |
+
with torch.no_grad():
|
28 |
+
logits = model_quantized(**inputs).logits
|
29 |
+
predicted_idx = logits.argmax(-1).item()
|
30 |
+
confidence = logits.softmax(-1)[0][predicted_idx].item()
|
31 |
+
label = model_quantized.config.id2label[predicted_idx]
|
32 |
+
|
33 |
+
return {"label": label, "confidence": confidence}
|
quantized_model/config.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_attn_implementation_autoset": true,
|
3 |
+
"architectures": [
|
4 |
+
"ViTForImageClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.0,
|
7 |
+
"encoder_stride": 16,
|
8 |
+
"hidden_act": "gelu",
|
9 |
+
"hidden_dropout_prob": 0.0,
|
10 |
+
"hidden_size": 768,
|
11 |
+
"id2label": {
|
12 |
+
"0": "Real",
|
13 |
+
"1": "Fake"
|
14 |
+
},
|
15 |
+
"image_size": 224,
|
16 |
+
"initializer_range": 0.02,
|
17 |
+
"intermediate_size": 3072,
|
18 |
+
"label2id": {
|
19 |
+
"Fake": 1,
|
20 |
+
"Real": 0
|
21 |
+
},
|
22 |
+
"layer_norm_eps": 1e-12,
|
23 |
+
"model_type": "vit",
|
24 |
+
"num_attention_heads": 12,
|
25 |
+
"num_channels": 3,
|
26 |
+
"num_hidden_layers": 12,
|
27 |
+
"patch_size": 16,
|
28 |
+
"pooler_act": "tanh",
|
29 |
+
"pooler_output_size": 768,
|
30 |
+
"problem_type": "single_label_classification",
|
31 |
+
"qkv_bias": true,
|
32 |
+
"torch_dtype": "float32",
|
33 |
+
"transformers_version": "4.51.3"
|
34 |
+
}
|
quantized_model/model_quantized.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:326c5407df0c791de2246f53db8c03081ced7628f42d8b74afff4d6d3ef32aa0
|
3 |
+
size 88529646
|
quantized_model/preprocessor_config.json
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"do_convert_rgb": null,
|
3 |
+
"do_normalize": true,
|
4 |
+
"do_rescale": true,
|
5 |
+
"do_resize": true,
|
6 |
+
"image_mean": [
|
7 |
+
0.5,
|
8 |
+
0.5,
|
9 |
+
0.5
|
10 |
+
],
|
11 |
+
"image_processor_type": "ViTImageProcessor",
|
12 |
+
"image_std": [
|
13 |
+
0.5,
|
14 |
+
0.5,
|
15 |
+
0.5
|
16 |
+
],
|
17 |
+
"resample": 2,
|
18 |
+
"rescale_factor": 0.00392156862745098,
|
19 |
+
"size": {
|
20 |
+
"height": 224,
|
21 |
+
"width": 224
|
22 |
+
}
|
23 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
fastapi
|
4 |
+
uvicorn
|
5 |
+
pillow
|
6 |
+
python-multipart
|