mahmoudsaber0 commited on
Commit
bcab1b7
·
verified ·
1 Parent(s): 78388f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -5,21 +5,29 @@ 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):
 
 
 
 
 
 
23
  data = await request.json()
24
  text = data.get("text", "").strip()
25
  if not text:
@@ -46,7 +54,7 @@ async def analyze_text(request: Request):
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)
 
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
19
  probs = torch.softmax(logits, dim=1)
20
+ ai_score = probs[0][1].item() * 100
21
+ return round(ai_score, 2)
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:
 
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)