KnoxfireyNeurons commited on
Commit
a4bdaed
·
verified ·
1 Parent(s): 2029420

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import gradio as gr
4
+ from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
5
+
6
+ # 1. THE CUSTOM ARCHITECTURE
7
+ class MultiSampleDropoutHead(nn.Module):
8
+ def __init__(self, config):
9
+ super().__init__()
10
+ self.dense = nn.Linear(config.hidden_size, config.hidden_size)
11
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
12
+ self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
13
+ self.multi_dropout = nn.ModuleList([nn.Dropout(0.1 * (i+1)) for i in range(5)])
14
+
15
+ def forward(self, features, **kwargs):
16
+ x = features[:, 0, :]
17
+ x = torch.tanh(self.dense(x))
18
+ return sum([self.out_proj(d(x)) for d in self.multi_dropout]) / 5
19
+
20
+ # 2. LOAD THE BRAIN FROM YOUR MODEL REPO
21
+ # We point directly to your new repository name
22
+ MODEL_PATH = "KnoxfireyNeurons/Roberta_Base"
23
+
24
+ print(f"Loading Golden Brain from {MODEL_PATH}...")
25
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
26
+ config = AutoConfig.from_pretrained(MODEL_PATH)
27
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH, config=config)
28
+ model.classifier = MultiSampleDropoutHead(config)
29
+ model.eval()
30
+
31
+ # 3. THE PREDICTION FUNCTION
32
+ def classify_review(text):
33
+ if not text.strip():
34
+ return "Please enter a review."
35
+
36
+ inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=256)
37
+
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
+
41
+ probs = torch.nn.functional.softmax(outputs, dim=-1).squeeze().tolist()
42
+ real_prob = probs[0]
43
+ fake_prob = probs[1]
44
+
45
+ return {"Fake (CG)": fake_prob, "Real (Original)": real_prob}
46
+
47
+ # 4. BUILD THE WEBSITE FRONTEND
48
+ theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate")
49
+
50
+ with gr.Blocks(theme=theme) as demo:
51
+ gr.Markdown("<h1 style='text-align: center;'>🤖 Fake Review Detector</h1>")
52
+ gr.Markdown("<p style='text-align: center;'>Powered by a custom RoBERTa-base model trained to 98.6% Accuracy.</p>")
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ input_text = gr.Textbox(label="Enter the Product Review", lines=6, placeholder="Paste review...")
57
+ submit_btn = gr.Button("Analyze Review", variant="primary")
58
+ with gr.Column():
59
+ output_label = gr.Label(label="AI Analysis Results")
60
+
61
+ submit_btn.click(fn=classify_review, inputs=input_text, outputs=output_label)
62
+
63
+ demo.launch()