Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,22 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI(title="AI Detector API")
|
| 6 |
|
| 7 |
-
# Load model
|
| 8 |
MODEL_NAME = "roberta-base-openai-detector"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
def get_ai_probability(text: str) -> float:
|
| 14 |
-
"""Return the AI probability (0–100%) for a given text."""
|
| 15 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 16 |
with torch.no_grad():
|
| 17 |
logits = model(**inputs).logits
|
| 18 |
probs = torch.softmax(logits, dim=1)
|
| 19 |
-
|
| 20 |
-
return round(ai_score, 2)
|
| 21 |
-
|
| 22 |
-
@app.get("/")
|
| 23 |
-
async def home():
|
| 24 |
-
return {
|
| 25 |
-
"message": "Welcome to Saber’s AI Detector API 👋",
|
| 26 |
-
"usage": "Send a POST request to /analyze with JSON {'text': 'your article text'}"
|
| 27 |
-
}
|
| 28 |
|
| 29 |
@app.post("/analyze")
|
| 30 |
async def analyze_text(request: Request):
|
|
@@ -42,7 +34,7 @@ async def analyze_text(request: Request):
|
|
| 42 |
"paragraph": i,
|
| 43 |
"ai_score": ai_score,
|
| 44 |
"human_score": round(100 - ai_score, 2),
|
| 45 |
-
"
|
| 46 |
})
|
| 47 |
|
| 48 |
overall = sum([r["ai_score"] for r in results]) / len(results)
|
|
@@ -51,3 +43,10 @@ async def analyze_text(request: Request):
|
|
| 51 |
"overall_human_score": round(100 - overall, 2),
|
| 52 |
"paragraphs": results
|
| 53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
+
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI(title="AI Detector API")
|
| 7 |
|
| 8 |
+
# Load model
|
| 9 |
MODEL_NAME = "roberta-base-openai-detector"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
def get_ai_probability(text: str) -> float:
|
|
|
|
| 15 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 16 |
with torch.no_grad():
|
| 17 |
logits = model(**inputs).logits
|
| 18 |
probs = torch.softmax(logits, dim=1)
|
| 19 |
+
return round(probs[0][1].item() * 100, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.post("/analyze")
|
| 22 |
async def analyze_text(request: Request):
|
|
|
|
| 34 |
"paragraph": i,
|
| 35 |
"ai_score": ai_score,
|
| 36 |
"human_score": round(100 - ai_score, 2),
|
| 37 |
+
"content_preview": para[:200] + ("..." if len(para) > 200 else "")
|
| 38 |
})
|
| 39 |
|
| 40 |
overall = sum([r["ai_score"] for r in results]) / len(results)
|
|
|
|
| 43 |
"overall_human_score": round(100 - overall, 2),
|
| 44 |
"paragraphs": results
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
@app.get("/")
|
| 48 |
+
async def root():
|
| 49 |
+
return {"message": "AI Detector API running on port 8080"}
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
uvicorn.run(app, host="0.0.0.0", port=8080)
|