Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,19 @@ 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 the model once at startup
|
| 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 |
-
"""Return AI probability (0–100%) for the given text."""
|
| 16 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 17 |
with torch.no_grad():
|
| 18 |
logits = model(**inputs).logits
|
|
@@ -22,12 +24,6 @@ def get_ai_probability(text: str) -> float:
|
|
| 22 |
|
| 23 |
@app.post("/analyze")
|
| 24 |
async def analyze_text(request: Request):
|
| 25 |
-
"""
|
| 26 |
-
Example body:
|
| 27 |
-
{
|
| 28 |
-
"text": "Your text here"
|
| 29 |
-
}
|
| 30 |
-
"""
|
| 31 |
data = await request.json()
|
| 32 |
text = data.get("text", "").strip()
|
| 33 |
if not text:
|
|
@@ -39,22 +35,3 @@ async def analyze_text(request: Request):
|
|
| 39 |
for i, para in enumerate(paragraphs, start=1):
|
| 40 |
ai_score = get_ai_probability(para)
|
| 41 |
results.append({
|
| 42 |
-
"paragraph": i,
|
| 43 |
-
"ai_score": ai_score,
|
| 44 |
-
"human_score": round(100 - ai_score, 2),
|
| 45 |
-
"content_preview": para[:200] + ("..." if len(para) > 200 else "")
|
| 46 |
-
})
|
| 47 |
-
|
| 48 |
-
overall = sum([r["ai_score"] for r in results]) / len(results)
|
| 49 |
-
return {
|
| 50 |
-
"overall_ai_score": round(overall, 2),
|
| 51 |
-
"overall_human_score": round(100 - overall, 2),
|
| 52 |
-
"paragraphs": results
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
@app.get("/")
|
| 56 |
-
async def root():
|
| 57 |
-
return {"message": "AI Detector API is running. Use POST /analyze"}
|
| 58 |
-
|
| 59 |
-
if __name__ == "__main__":
|
| 60 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
import uvicorn
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# ✅ Fix permission issue on Hugging Face Spaces
|
| 8 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
| 9 |
|
| 10 |
app = FastAPI(title="AI Detector API")
|
| 11 |
|
|
|
|
| 12 |
MODEL_NAME = "roberta-base-openai-detector"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
def get_ai_probability(text: str) -> float:
|
|
|
|
| 18 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 19 |
with torch.no_grad():
|
| 20 |
logits = model(**inputs).logits
|
|
|
|
| 24 |
|
| 25 |
@app.post("/analyze")
|
| 26 |
async def analyze_text(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
data = await request.json()
|
| 28 |
text = data.get("text", "").strip()
|
| 29 |
if not text:
|
|
|
|
| 35 |
for i, para in enumerate(paragraphs, start=1):
|
| 36 |
ai_score = get_ai_probability(para)
|
| 37 |
results.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|