Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import ast
|
| 4 |
import streamlit as st
|
| 5 |
import torch
|
| 6 |
import torch.nn.functional as F
|
| 7 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 8 |
import re
|
| 9 |
-
import math
|
| 10 |
import logging
|
| 11 |
import pandas as pd
|
| 12 |
|
|
@@ -21,42 +17,6 @@ st.logo(
|
|
| 21 |
link="https://dejan.ai/",
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# --- Load heuristic weights from environment secrets, with JSON→Python fallback ---
|
| 25 |
-
@st.cache_resource
|
| 26 |
-
def load_heuristic_weights():
|
| 27 |
-
def _load(env_key):
|
| 28 |
-
raw = os.environ[env_key]
|
| 29 |
-
try:
|
| 30 |
-
return json.loads(raw)
|
| 31 |
-
except json.JSONDecodeError:
|
| 32 |
-
return ast.literal_eval(raw)
|
| 33 |
-
ai = _load("AI_WEIGHTS_JSON")
|
| 34 |
-
og = _load("OG_WEIGHTS_JSON")
|
| 35 |
-
return ai, og
|
| 36 |
-
|
| 37 |
-
AI_WEIGHTS, OG_WEIGHTS = load_heuristic_weights()
|
| 38 |
-
SIGMOID_K = 0.5
|
| 39 |
-
|
| 40 |
-
def tokenize(text):
|
| 41 |
-
return re.findall(r'\b[a-z]{2,}\b', text.lower())
|
| 42 |
-
|
| 43 |
-
def classify_text_likelihood(text: str) -> float:
|
| 44 |
-
tokens = tokenize(text)
|
| 45 |
-
if not tokens:
|
| 46 |
-
return 0.5
|
| 47 |
-
ai_score = og_score = matched = 0
|
| 48 |
-
for t in tokens:
|
| 49 |
-
aw = AI_WEIGHTS.get(t, 0)
|
| 50 |
-
ow = OG_WEIGHTS.get(t, 0)
|
| 51 |
-
if aw or ow:
|
| 52 |
-
matched += 1
|
| 53 |
-
ai_score += aw
|
| 54 |
-
og_score += ow
|
| 55 |
-
if matched == 0:
|
| 56 |
-
return 0.5
|
| 57 |
-
net = ai_score - og_score
|
| 58 |
-
return 1 / (1 + math.exp(-SIGMOID_K * net))
|
| 59 |
-
|
| 60 |
# --- Logging & Streamlit setup ---
|
| 61 |
logging.basicConfig(level=logging.INFO)
|
| 62 |
logger = logging.getLogger(__name__)
|
|
@@ -158,11 +118,8 @@ if st.button("Classify", type="primary"):
|
|
| 158 |
st.markdown("### 🔍 Highlighted Text")
|
| 159 |
st.markdown(" ".join(highlighted_sentences), unsafe_allow_html=True)
|
| 160 |
|
|
|
|
| 161 |
avg = torch.mean(probs, dim=0)
|
| 162 |
model_ai = avg[0].item()
|
| 163 |
-
heuristic_ai = classify_text_likelihood(text)
|
| 164 |
-
combined = min(model_ai + heuristic_ai, 1.0)
|
| 165 |
|
| 166 |
-
st.subheader(f"⚖️ AI Likelihood: {
|
| 167 |
-
st.write(f"🤖 Model: {model_ai*100:.1f}%")
|
| 168 |
-
st.write(f"🛠️ Heuristic: {heuristic_ai*100:.1f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
import re
|
|
|
|
| 6 |
import logging
|
| 7 |
import pandas as pd
|
| 8 |
|
|
|
|
| 17 |
link="https://dejan.ai/",
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# --- Logging & Streamlit setup ---
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
| 22 |
logger = logging.getLogger(__name__)
|
|
|
|
| 118 |
st.markdown("### 🔍 Highlighted Text")
|
| 119 |
st.markdown(" ".join(highlighted_sentences), unsafe_allow_html=True)
|
| 120 |
|
| 121 |
+
# Overall score (just model avg)
|
| 122 |
avg = torch.mean(probs, dim=0)
|
| 123 |
model_ai = avg[0].item()
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
st.subheader(f"⚖️ AI Likelihood: {model_ai*100:.1f}%")
|
|
|
|
|
|