Spaces:
Sleeping
Sleeping
Create body_analyzer.py
Browse files- body_analyzer.py +45 -0
body_analyzer.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
HF_API_KEY = os.getenv("HF_API_KEY") # Hugging Face free account
|
| 5 |
+
HF_HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 6 |
+
|
| 7 |
+
MODELS = {
|
| 8 |
+
"ai_detector": "roberta-base-openai-detector",
|
| 9 |
+
"sentiment": "finiteautomata/bertweet-base-sentiment-analysis",
|
| 10 |
+
"spam": "mrm8488/bert-tiny-finetuned-sms-spam-detection",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def query_hf(model, text):
|
| 14 |
+
url = f"https://api-inference.huggingface.co/models/{model}"
|
| 15 |
+
res = requests.post(url, headers=HF_HEADERS, json={"inputs": text[:1000]})
|
| 16 |
+
return res.json()
|
| 17 |
+
|
| 18 |
+
def analyze_body(text):
|
| 19 |
+
findings = []
|
| 20 |
+
|
| 21 |
+
# 1. AI-generated detection
|
| 22 |
+
try:
|
| 23 |
+
result = query_hf(MODELS["ai_detector"], text)
|
| 24 |
+
if isinstance(result, list):
|
| 25 |
+
findings.append(f"Body: AI Detector → {result[0]['label']} (confidence {result[0]['score']:.2f})")
|
| 26 |
+
except:
|
| 27 |
+
findings.append("Body: AI detection failed")
|
| 28 |
+
|
| 29 |
+
# 2. Sentiment / Tone
|
| 30 |
+
try:
|
| 31 |
+
result = query_hf(MODELS["sentiment"], text)
|
| 32 |
+
if isinstance(result, list):
|
| 33 |
+
findings.append(f"Body: Sentiment → {result[0]['label']} (confidence {result[0]['score']:.2f})")
|
| 34 |
+
except:
|
| 35 |
+
findings.append("Body: Sentiment analysis failed")
|
| 36 |
+
|
| 37 |
+
# 3. Spam vs Ham
|
| 38 |
+
try:
|
| 39 |
+
result = query_hf(MODELS["spam"], text)
|
| 40 |
+
if isinstance(result, list):
|
| 41 |
+
findings.append(f"Body: Spam Detector → {result[0]['label']} (confidence {result[0]['score']:.2f})")
|
| 42 |
+
except:
|
| 43 |
+
findings.append("Body: Spam detection failed")
|
| 44 |
+
|
| 45 |
+
return findings
|