Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch.nn as nn
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
|
| 5 |
|
| 6 |
-
# 1. ARCHITECTURE
|
| 7 |
class MultiSampleDropoutHead(nn.Module):
|
| 8 |
def __init__(self, config):
|
| 9 |
super().__init__()
|
|
@@ -14,21 +14,30 @@ class MultiSampleDropoutHead(nn.Module):
|
|
| 14 |
def forward(self, features, **kwargs):
|
| 15 |
x = features[:, 0, :]
|
| 16 |
x = torch.tanh(self.dense(x))
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
tokenizer =
|
| 23 |
-
config = AutoConfig.from_pretrained(MODEL_REPO)
|
| 24 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_REPO, config=config)
|
| 25 |
-
model.classifier = MultiSampleDropoutHead(config)
|
| 26 |
-
model.eval()
|
| 27 |
|
| 28 |
-
# 3. PREDICTION
|
| 29 |
-
def
|
| 30 |
-
if not text or
|
| 31 |
-
return {"
|
| 32 |
|
| 33 |
inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=256)
|
| 34 |
|
|
@@ -36,21 +45,22 @@ def classify_review(text):
|
|
| 36 |
outputs = model(**inputs)
|
| 37 |
|
| 38 |
probs = torch.nn.functional.softmax(outputs, dim=-1).squeeze().tolist()
|
|
|
|
|
|
|
| 39 |
return {
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
}
|
| 43 |
|
| 44 |
-
# 4. INTERFACE
|
| 45 |
-
# We use the simplest possible launch to let Hugging Face handle the proxy
|
| 46 |
demo = gr.Interface(
|
| 47 |
-
fn=
|
| 48 |
-
inputs=gr.Textbox(label="
|
| 49 |
-
outputs=gr.Label(label="AI
|
| 50 |
title="🤖 Fake Review Detector",
|
| 51 |
-
description="
|
| 52 |
allow_flagging="never"
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
demo.launch()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
|
| 5 |
|
| 6 |
+
# 1. ARCHITECTURE DEFINITION
|
| 7 |
class MultiSampleDropoutHead(nn.Module):
|
| 8 |
def __init__(self, config):
|
| 9 |
super().__init__()
|
|
|
|
| 14 |
def forward(self, features, **kwargs):
|
| 15 |
x = features[:, 0, :]
|
| 16 |
x = torch.tanh(self.dense(x))
|
| 17 |
+
logits = sum([self.out_proj(d(x)) for d in self.multi_dropout]) / 5
|
| 18 |
+
return logits
|
| 19 |
|
| 20 |
+
# 2. MODEL LOADER
|
| 21 |
+
# Using a function to ensure garbage collection
|
| 22 |
+
def get_model():
|
| 23 |
+
repo = "KnoxfireyNeurons/Roberta_Base"
|
| 24 |
+
print(f"Initializing Golden Brain from {repo}...")
|
| 25 |
+
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(repo)
|
| 27 |
+
config = AutoConfig.from_pretrained(repo)
|
| 28 |
+
model = AutoModelForSequenceClassification.from_pretrained(repo, config=config)
|
| 29 |
+
|
| 30 |
+
# Critical: Re-attach the custom head
|
| 31 |
+
model.classifier = MultiSampleDropoutHead(config)
|
| 32 |
+
model.eval()
|
| 33 |
+
return model, tokenizer
|
| 34 |
|
| 35 |
+
model, tokenizer = get_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# 3. PREDICTION ENGINE
|
| 38 |
+
def predict(text):
|
| 39 |
+
if not text or len(text.strip()) == 0:
|
| 40 |
+
return {"Please enter a review": 0.0}
|
| 41 |
|
| 42 |
inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=256)
|
| 43 |
|
|
|
|
| 45 |
outputs = model(**inputs)
|
| 46 |
|
| 47 |
probs = torch.nn.functional.softmax(outputs, dim=-1).squeeze().tolist()
|
| 48 |
+
|
| 49 |
+
# Return dictionary for Gradio's Label component
|
| 50 |
return {
|
| 51 |
+
"Original (Real)": probs[0],
|
| 52 |
+
"Computer Generated (Fake)": probs[1]
|
| 53 |
}
|
| 54 |
|
| 55 |
+
# 4. INTERFACE LAUNCH
|
|
|
|
| 56 |
demo = gr.Interface(
|
| 57 |
+
fn=predict,
|
| 58 |
+
inputs=gr.Textbox(label="Review Content", placeholder="Paste review text here...", lines=5),
|
| 59 |
+
outputs=gr.Label(num_top_classes=2, label="AI Prediction"),
|
| 60 |
title="🤖 Fake Review Detector",
|
| 61 |
+
description="Analyze product reviews for AI-generated patterns using a SOTA RoBERTa model.",
|
| 62 |
allow_flagging="never"
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|