Niansuh commited on
Commit
1be017e
1 Parent(s): ed8fbbc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+
6
+ class ModelProcessor:
7
+ def __init__(self, repo_id="HuggingFaceTB/cosmo-1b"):
8
+ self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ self.tokenizer = AutoTokenizer.from_pretrained(repo_id, use_fast=True)
10
+ self.model = AutoModelForCausalLM.from_pretrained(
11
+ repo_id, torch_dtype=torch.float16, device_map={"": self.device}, trust_remote_code=True
12
+ )
13
+ self.model.eval()
14
+ self.tokenizer.pad_token = self.tokenizer.eos_token
15
+
16
+ @torch.inference_mode()
17
+ def process_data_and_compute_statistics(self, prompt):
18
+ tokens = self.tokenizer(
19
+ prompt, return_tensors="pt", truncation=True, max_length=512
20
+ ).to(self.model.device)
21
+ outputs = self.model(tokens["input_ids"])
22
+ logits = outputs.logits
23
+ shifted_labels = tokens["input_ids"][..., 1:].contiguous()
24
+ shifted_logits = logits[..., :-1, :].contiguous()
25
+ shifted_probs = torch.softmax(shifted_logits, dim=-1)
26
+ shifted_log_probs = torch.log_softmax(shifted_logits, dim=-1)
27
+ entropy = -torch.sum(shifted_probs * shifted_log_probs, dim=-1).squeeze()
28
+ logits_flat = shifted_logits.view(-1, shifted_logits.size(-1))
29
+ labels_flat = shifted_labels.view(-1)
30
+ probabilities_flat = torch.softmax(logits_flat, dim=-1)
31
+ true_class_probabilities = probabilities_flat.gather(
32
+ 1, labels_flat.unsqueeze(1)
33
+ ).squeeze(1)
34
+ nll = -torch.log(
35
+ true_class_probabilities.clamp(min=1e-9)
36
+ )
37
+ ranks = (
38
+ shifted_logits.argsort(dim=-1, descending=True)
39
+ == shifted_labels.unsqueeze(-1)
40
+ ).nonzero()[:, -1]
41
+ if entropy.clamp(max=4).median() < 2.0:
42
+ return 1
43
+ return 1 if (ranks.clamp(max=4) * nll.clamp(max=4)).mean() < 5.2 else 0
44
+
45
+ processor = ModelProcessor()
46
+
47
+ @spaces.GPU(duration=180)
48
+ def detect(prompt):
49
+ prediction = processor.process_data_and_compute_statistics(prompt)
50
+ if prediction == 1:
51
+ return "<div class='output-text'>The text is likely <b>generated</b> by a language model.</div>"
52
+ else:
53
+ return "<div class='output-text'>The text is likely <b>not generated</b> by a language model.</div>"
54
+
55
+ with gr.Blocks(
56
+ css="""
57
+ .gradio-container {
58
+ max-width: 800px;
59
+ margin: 0 auto;
60
+ }
61
+ .gr-box {
62
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
63
+ padding: 20px;
64
+ border-radius: 4px;
65
+ }
66
+ .gr-button {
67
+ background-color: #007bff;
68
+ color: white;
69
+ padding: 10px 20px;
70
+ border-radius: 4px;
71
+ }
72
+ .gr-button:hover {
73
+ background-color: #0056b3;
74
+ }
75
+ .hyperlinks a {
76
+ margin-right: 10px;
77
+ }
78
+ .output-text {
79
+ text-align: center;
80
+ font-size: 24px;
81
+ font-weight: bold;
82
+ }
83
+ """
84
+ ) as demo:
85
+ with gr.Row():
86
+ with gr.Column(scale=3):
87
+ gr.Markdown("# ENTELL Model Detection - ChatGPTBots.net")
88
+ with gr.Column(scale=1):
89
+ gr.HTML(
90
+ """
91
+ """,
92
+ elem_classes="hyperlinks",
93
+ )
94
+ with gr.Row():
95
+ with gr.Column():
96
+ prompt = gr.Textbox(
97
+ lines=8,
98
+ placeholder="Type your prompt here...",
99
+ label="Prompt",
100
+ )
101
+ submit_btn = gr.Button("Submit", variant="primary")
102
+ output = gr.HTML() # Changed to gr.HTML() to support custom HTML
103
+
104
+ submit_btn.click(fn=detect, inputs=promp