yagnik12 commited on
Commit
e7d2c96
·
verified ·
1 Parent(s): 27f9fcd

Update ai_text_detector_valid_final.py

Browse files
Files changed (1) hide show
  1. ai_text_detector_valid_final.py +27 -16
ai_text_detector_valid_final.py CHANGED
@@ -5,16 +5,18 @@ import requests
5
  import numpy as np
6
 
7
  # Hugging Face Token
8
- HF_TOKEN = os.getenv("HF_TOKEN") # export HF_TOKEN="your_token" before running
 
 
9
 
10
  # Headers for API
11
  headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
12
 
13
  # Multiple AI text detection models
14
  MODELS = {
15
- "DeBERTa Detector": "microsoft/deberta-v3-base",
16
- "MonkeyDAnh": "MonkeyDAnh/deberta-v3-base-finetuned-ai-human-detector",
17
- "Andreas122001": "Andreas122001/roberta-base-openai-detector"
18
  # SzegedAI handled separately since it's a Space
19
  }
20
 
@@ -30,22 +32,31 @@ def run_hf_model(model_id, text):
30
  return {"Human Probability": float(probs[0]*100), "AI Probability": float(probs[1]*100)}
31
  except Exception as e:
32
  return {"error": str(e)}
33
-
34
- def run_szegedai(text):
35
- """Call the SzegedAI Space API"""
36
  try:
37
- response = requests.post(
38
- "https://huggingface.co/spaces/SzegedAI/AI_Detector/run/predict",
39
- headers=headers,
40
- json={"data": [text]},
41
- timeout=30
42
- )
43
  response.raise_for_status()
44
  result = response.json()
45
- return result # Raw result, we can format it later
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  except Exception as e:
47
  return {"error": str(e)}
48
-
49
  def detect_text(text):
50
  results = {}
51
  # Transformers models
@@ -53,7 +64,7 @@ def detect_text(text):
53
  results[name] = run_hf_model(model_id, text)
54
 
55
  # SzegedAI (Space)
56
- results["SzegedAI Detector"] = run_szegedai(text)
57
 
58
  # Final verdict (simple rule-based)
59
  ai_probs = []
 
5
  import numpy as np
6
 
7
  # Hugging Face Token
8
+ HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face token (optional if space is public)
9
+ SZEGEDAI_URL = "https://hf.space/embed/SzegedAI/AI_Detector/api/predict/"
10
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
11
 
12
  # Headers for API
13
  headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
14
 
15
  # Multiple AI text detection models
16
  MODELS = {
17
+ "DeBERTa Detector": "distilbert-base-uncased-finetuned-sst-2-english",
18
+ "MonkeyDAnh":"MonkeyDAnh/my-awesome-ai-detector-roberta-base-v4-human-vs-machine-finetune",
19
+ "Andreas122001":"andreas122001/roberta-academic-detector"
20
  # SzegedAI handled separately since it's a Space
21
  }
22
 
 
32
  return {"Human Probability": float(probs[0]*100), "AI Probability": float(probs[1]*100)}
33
  except Exception as e:
34
  return {"error": str(e)}
35
+ def szegedai_predict(text):
 
 
36
  try:
37
+ payload = {"data": [text]}
38
+ response = requests.post(SZEGEDAI_URL, json=payload, headers=HEADERS, timeout=30)
 
 
 
 
39
  response.raise_for_status()
40
  result = response.json()
41
+
42
+ raw = result["data"][0] # e.g. "Human Probability: 99.83% | AI Probability: 0.17%"
43
+
44
+ human_match = re.search(r"Human[^0-9]*([\d.]+)%", raw)
45
+ ai_match = re.search(r"AI[^0-9]*([\d.]+)%", raw)
46
+
47
+ if human_match and ai_match:
48
+ human_prob = float(human_match.group(1))
49
+ ai_prob = float(ai_match.group(1))
50
+ return {
51
+ "Human Probability": round(human_prob, 2),
52
+ "AI Probability": round(ai_prob, 2),
53
+ }
54
+ else:
55
+ return {"error": f"Unexpected response: {raw}"}
56
+
57
  except Exception as e:
58
  return {"error": str(e)}
59
+
60
  def detect_text(text):
61
  results = {}
62
  # Transformers models
 
64
  results[name] = run_hf_model(model_id, text)
65
 
66
  # SzegedAI (Space)
67
+ results["SzegedAI Detector"] = szegedai_predict(text)
68
 
69
  # Final verdict (simple rule-based)
70
  ai_probs = []