Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +15 -0
- README.md +2 -4
- app.py +17 -15
- requirements.txt +2 -2
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
# Install CPU-only PyTorch (much smaller than GPU build), then the rest
|
| 8 |
+
RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu && \
|
| 9 |
+
pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY app.py .
|
| 12 |
+
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -3,9 +3,7 @@ title: CodeSheriff Inference
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
| 6 |
-
sdk:
|
| 7 |
-
|
| 8 |
-
python_version: "3.11"
|
| 9 |
-
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
|
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
|
|
|
|
|
|
| 8 |
pinned: false
|
| 9 |
---
|
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
CodeSheriff Inference Space
|
| 3 |
|
| 4 |
-
|
| 5 |
-
and exposes a /predict
|
| 6 |
"""
|
| 7 |
|
| 8 |
-
import gradio as gr
|
| 9 |
import torch
|
|
|
|
| 10 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 11 |
|
| 12 |
MODEL_ID = "jayansh21/codesheriff-bug-classifier"
|
| 13 |
NUM_LABELS = 5
|
|
@@ -20,6 +21,8 @@ LABEL_NAMES = {
|
|
| 20 |
4: "Logic Flaw",
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
| 23 |
print("Loading CodeSheriff classifier β¦")
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 25 |
model = AutoModelForSequenceClassification.from_pretrained(
|
|
@@ -29,13 +32,15 @@ model.eval()
|
|
| 29 |
print("Model loaded β
")
|
| 30 |
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
"""Classify a code snippet and return label, confidence, label_id."""
|
| 34 |
-
|
|
|
|
| 35 |
return {"label": "Clean", "confidence": 0.0, "label_id": 0}
|
| 36 |
|
| 37 |
encoding = tokenizer(
|
| 38 |
-
|
| 39 |
truncation=True,
|
| 40 |
padding="max_length",
|
| 41 |
max_length=MAX_LENGTH,
|
|
@@ -55,13 +60,10 @@ def predict(code_snippet: str) -> dict:
|
|
| 55 |
}
|
| 56 |
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
title="π CodeSheriff Bug Classifier",
|
| 63 |
-
description="Fine-tuned CodeBERT model for detecting common bug patterns.",
|
| 64 |
-
api_name="predict",
|
| 65 |
-
)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
CodeSheriff Inference Space
|
| 3 |
|
| 4 |
+
Minimal FastAPI server that loads the fine-tuned CodeBERT classifier
|
| 5 |
+
and exposes a POST /predict endpoint. Called remotely by the Render backend.
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 8 |
import torch
|
| 9 |
+
from fastapi import FastAPI
|
| 10 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 11 |
+
import uvicorn
|
| 12 |
|
| 13 |
MODEL_ID = "jayansh21/codesheriff-bug-classifier"
|
| 14 |
NUM_LABELS = 5
|
|
|
|
| 21 |
4: "Logic Flaw",
|
| 22 |
}
|
| 23 |
|
| 24 |
+
app = FastAPI(title="CodeSheriff Inference")
|
| 25 |
+
|
| 26 |
print("Loading CodeSheriff classifier β¦")
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 28 |
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
| 32 |
print("Model loaded β
")
|
| 33 |
|
| 34 |
|
| 35 |
+
@app.post("/predict")
|
| 36 |
+
def predict(data: dict):
|
| 37 |
"""Classify a code snippet and return label, confidence, label_id."""
|
| 38 |
+
code = data.get("code", "")
|
| 39 |
+
if not code or not code.strip():
|
| 40 |
return {"label": "Clean", "confidence": 0.0, "label_id": 0}
|
| 41 |
|
| 42 |
encoding = tokenizer(
|
| 43 |
+
code,
|
| 44 |
truncation=True,
|
| 45 |
padding="max_length",
|
| 46 |
max_length=MAX_LENGTH,
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
|
| 63 |
+
@app.get("/health")
|
| 64 |
+
def health():
|
| 65 |
+
return {"status": "ok"}
|
| 66 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 2 |
-
torch
|
| 3 |
transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
transformers
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|