Spaces:
Running
Running
Marc Allen Lopez
commited on
Commit
·
1115f69
1
Parent(s):
00fbd84
Add FastAPI GPU inference (Docker)
Browse files- Dockerfile +9 -0
- app.py +159 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM pytorch/pytorch:2.1.2-cuda11.8-cudnn8-runtime
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 4 |
+
COPY requirements.txt /app/requirements.txt
|
| 5 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 6 |
+
COPY app.py /app/app.py
|
| 7 |
+
ENV PORT=7860
|
| 8 |
+
EXPOSE 7860
|
| 9 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from transformers import AutoTokenizer, AutoConfig, AutoModel, PreTrainedModel
|
| 8 |
+
from fastapi import FastAPI, Form
|
| 9 |
+
from fastapi.responses import JSONResponse
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
MODEL_ID = os.getenv("MODEL_ID", "desklib/ai-text-detector-v1.01")
|
| 13 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
DEFAULT_MAX_LEN = int(os.getenv("MAX_LEN", "256"))
|
| 15 |
+
DEFAULT_BATCH_SIZE = int(os.getenv("BATCH_SIZE", "16"))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class DesklibAIDetectionModel(PreTrainedModel):
|
| 19 |
+
config_class = AutoConfig
|
| 20 |
+
|
| 21 |
+
def __init__(self, config):
|
| 22 |
+
super().__init__(config)
|
| 23 |
+
self.model = AutoModel.from_config(config)
|
| 24 |
+
self.classifier = nn.Linear(config.hidden_size, 1)
|
| 25 |
+
self.init_weights()
|
| 26 |
+
|
| 27 |
+
def forward(self, input_ids, attention_mask=None, labels=None):
|
| 28 |
+
outputs = self.model(input_ids, attention_mask=attention_mask)
|
| 29 |
+
last_hidden_state = outputs[0]
|
| 30 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()
|
| 31 |
+
sum_embeddings = torch.sum(last_hidden_state * input_mask_expanded, dim=1)
|
| 32 |
+
sum_mask = torch.clamp(input_mask_expanded.sum(dim=1), min=1e-9)
|
| 33 |
+
pooled_output = sum_embeddings / sum_mask
|
| 34 |
+
logits = self.classifier(pooled_output)
|
| 35 |
+
return {"logits": logits}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def load_model():
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 40 |
+
model = DesklibAIDetectionModel.from_pretrained(MODEL_ID)
|
| 41 |
+
model.to(DEVICE)
|
| 42 |
+
model.eval()
|
| 43 |
+
|
| 44 |
+
# Warmup
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
sample = tokenizer("Hello.", truncation=True, max_length=8, return_tensors="pt")
|
| 47 |
+
input_ids = sample["input_ids"].to(DEVICE)
|
| 48 |
+
attention_mask = sample["attention_mask"].to(DEVICE)
|
| 49 |
+
if DEVICE.type == "cuda":
|
| 50 |
+
with torch.cuda.amp.autocast():
|
| 51 |
+
_ = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 52 |
+
else:
|
| 53 |
+
_ = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 54 |
+
|
| 55 |
+
return tokenizer, model
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
tokenizer, model = load_model()
|
| 59 |
+
|
| 60 |
+
app = FastAPI(title="TextSense Inference (GPU)")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def simple_sentence_split(text: str) -> List[Tuple[str, int, int]]:
|
| 64 |
+
pattern = r"[^.!?]*[.!?]+(?:\s+|$)"
|
| 65 |
+
matches = list(re.finditer(pattern, text))
|
| 66 |
+
spans: List[Tuple[str, int, int]] = []
|
| 67 |
+
last_end = 0
|
| 68 |
+
for m in matches:
|
| 69 |
+
seg = m.group().strip()
|
| 70 |
+
if not seg:
|
| 71 |
+
last_end = m.end()
|
| 72 |
+
continue
|
| 73 |
+
raw_start = m.start()
|
| 74 |
+
raw_end = m.end()
|
| 75 |
+
trim_left = 0
|
| 76 |
+
trim_right = 0
|
| 77 |
+
while raw_start + trim_left < raw_end and text[raw_start + trim_left].isspace():
|
| 78 |
+
trim_left += 1
|
| 79 |
+
while raw_end - 1 - trim_right >= raw_start + trim_left and text[raw_end - 1 - trim_right].isspace():
|
| 80 |
+
trim_right += 1
|
| 81 |
+
sentence_start = raw_start + trim_left
|
| 82 |
+
sentence_end = raw_end - trim_right
|
| 83 |
+
spans.append((seg, sentence_start, sentence_end))
|
| 84 |
+
last_end = sentence_end
|
| 85 |
+
if last_end < len(text):
|
| 86 |
+
trailing = text[last_end:].strip()
|
| 87 |
+
if trailing:
|
| 88 |
+
spans.append((trailing, last_end, len(text)))
|
| 89 |
+
return spans
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def predict_texts_batch(texts: List[str], max_len: int = DEFAULT_MAX_LEN, batch_size: int = DEFAULT_BATCH_SIZE):
|
| 93 |
+
results: List[Tuple[float, int]] = []
|
| 94 |
+
total = len(texts)
|
| 95 |
+
if total == 0:
|
| 96 |
+
return results
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
for start_idx in range(0, total, batch_size):
|
| 99 |
+
end_idx = min(start_idx + batch_size, total)
|
| 100 |
+
batch_texts = texts[start_idx:end_idx]
|
| 101 |
+
enc = tokenizer(batch_texts, padding=True, truncation=True, max_length=max_len, return_tensors="pt")
|
| 102 |
+
input_ids = enc["input_ids"].to(DEVICE)
|
| 103 |
+
attention_mask = enc["attention_mask"].to(DEVICE)
|
| 104 |
+
if DEVICE.type == "cuda":
|
| 105 |
+
with torch.cuda.amp.autocast():
|
| 106 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 107 |
+
else:
|
| 108 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 109 |
+
logits = outputs["logits"].squeeze(-1)
|
| 110 |
+
probs = torch.sigmoid(logits).detach().cpu().tolist()
|
| 111 |
+
if isinstance(probs, float):
|
| 112 |
+
probs = [probs]
|
| 113 |
+
for p in probs:
|
| 114 |
+
results.append((float(p), 1 if p >= 0.5 else 0))
|
| 115 |
+
return results
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
@app.post("/analyze")
|
| 119 |
+
async def analyze(text: str = Form(...)):
|
| 120 |
+
cleaned = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
|
| 121 |
+
cleaned = re.sub(r" +", " ", cleaned).strip()
|
| 122 |
+
spans = simple_sentence_split(cleaned)
|
| 123 |
+
probs_labels = predict_texts_batch([t for (t, _, _) in spans])
|
| 124 |
+
segments = []
|
| 125 |
+
for (seg_text, start, end), (prob, label) in zip(spans, probs_labels):
|
| 126 |
+
segments.append({
|
| 127 |
+
"text": seg_text,
|
| 128 |
+
"start": start,
|
| 129 |
+
"end": end,
|
| 130 |
+
"probability": prob,
|
| 131 |
+
"is_ai": label == 1,
|
| 132 |
+
})
|
| 133 |
+
total_length = len(cleaned)
|
| 134 |
+
ai_segments = [s for s in segments if s["is_ai"]]
|
| 135 |
+
ai_chars = sum(len(s["text"]) for s in ai_segments)
|
| 136 |
+
ai_percentage = (ai_chars / total_length) * 100 if total_length > 0 else 0
|
| 137 |
+
human_percentage = 100 - ai_percentage
|
| 138 |
+
avg_ai_prob = sum(s["probability"] for s in segments) / len(segments) if segments else 0
|
| 139 |
+
result = {
|
| 140 |
+
"cleaned_text": cleaned,
|
| 141 |
+
"segments": segments,
|
| 142 |
+
"statistics": {
|
| 143 |
+
"total_length": total_length,
|
| 144 |
+
"ai_percentage": round(ai_percentage, 2),
|
| 145 |
+
"human_percentage": round(human_percentage, 2),
|
| 146 |
+
"avg_ai_probability": round(avg_ai_prob * 100, 2),
|
| 147 |
+
"total_segments": len(segments),
|
| 148 |
+
"ai_segments_count": len(ai_segments),
|
| 149 |
+
},
|
| 150 |
+
"overall_assessment": "Likely AI-Generated" if avg_ai_prob > 0.5 else "Likely Human-Written",
|
| 151 |
+
}
|
| 152 |
+
return JSONResponse(result)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
@app.get("/healthz")
|
| 156 |
+
async def healthz():
|
| 157 |
+
return {"ok": True, "device": str(DEVICE)}
|
| 158 |
+
|
| 159 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.110.0
|
| 2 |
+
uvicorn[standard]>=0.27.0
|
| 3 |
+
transformers>=4.38.0
|
| 4 |
+
safetensors>=0.4.2
|