File size: 7,679 Bytes
49b1ef8 71f5043 132a2dd b0d6a30 49b1ef8 b0d6a30 132a2dd a37c9c1 132a2dd b0d6a30 ef20835 b0d6a30 ec5bad4 03d2b1d 1f36bf0 03d2b1d 1f36bf0 f85c14a 1f36bf0 f85c14a 03d2b1d 1f36bf0 03d2b1d ec5bad4 5050833 ec5bad4 5050833 b0d6a30 349bcd6 132a2dd 349bcd6 b0d6a30 132a2dd 349bcd6 b0d6a30 49b1ef8 142cc53 132a2dd b0d6a30 132a2dd 49b1ef8 b0d6a30 132a2dd 49b1ef8 b0d6a30 132a2dd 49b1ef8 132a2dd b0d6a30 132a2dd b0d6a30 132a2dd b0d6a30 49b1ef8 5a64fc3 71f5043 132a2dd 71f5043 b0d6a30 132a2dd 49b1ef8 b0d6a30 132a2dd d8eac00 49b1ef8 b0d6a30 7d670e0 49b1ef8 5050833 49b1ef8 132a2dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import gradio as gr
import os
import json
import uuid
import torch
import datetime
import torch.nn as nn
from transformers import AutoTokenizer, AutoModel, AutoConfig
from huggingface_hub import HfApi, create_repo, hf_hub_download
from torchcrf import CRF
# Constants
HF_DATASET_REPO = "M2ai/mgtd-logs"
HF_TOKEN = os.getenv("Mgtd")
DATASET_CREATED = False
# Model identifiers
code = "ENG"
pntr = 2
model_name_or_path = "microsoft/mdeberta-v3-base"
hf_token = os.environ.get("Mgtd") # Set this before running
# Download model checkpoint
file_path = hf_hub_download(
repo_id="1024m/MGTD-Long-New",
filename=f"{code}/mdeberta-epoch-{pntr}.pt",
token=hf_token,
local_dir="./checkpoints"
)
# Define CRF model
class AutoModelCRF(nn.Module):
def __init__(self, model_name_or_path, dropout=0.075):
super().__init__()
self.config = AutoConfig.from_pretrained(model_name_or_path)
self.num_labels = 2
self.encoder = AutoModel.from_pretrained(model_name_or_path, trust_remote_code=True, config=self.config)
self.dropout = nn.Dropout(dropout)
self.linear = nn.Linear(self.config.hidden_size, self.num_labels)
self.crf = CRF(self.num_labels, batch_first=True)
def forward(self, input_ids, attention_mask):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
seq_output = self.dropout(outputs[0])
emissions = self.linear(seq_output)
tags = self.crf.decode(emissions, attention_mask.byte())
return tags, emissions
# Load model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
model = AutoModelCRF(model_name_or_path)
checkpoint = torch.load(file_path, map_location="cpu")
model.load_state_dict(checkpoint.get("model_state_dict", checkpoint), strict=False)
model = model.to(device)
model.eval()
# Inference function
def get_word_probabilities(text):
try:
text = " ".join(text.split(" ")[:2048])
except Exception as e:
print("Error during text preprocessing:", e)
return []
try:
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
except Exception as e:
print("Error during tokenization or moving inputs to device:", e)
return []
try:
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
except Exception as e:
print("Error during token conversion:", e)
return []
try:
with torch.no_grad():
tags, emission = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
except Exception as e:
print("Error during model inference:", e)
return []
try:
probs = torch.softmax(emission, dim=-1)[0, :, 1].cpu().numpy()
except Exception as e:
print("Error during softmax or extracting class probabilities:", e)
return []
word_probs = []
current_word = ""
current_probs = []
try:
for token, prob in zip(tokens, probs):
if token in ["<s>", "</s>"]:
continue
if token.startswith("β"):
if current_word and current_probs:
word_probs.append(sum(current_probs) / len(current_probs))
current_word = token[1:] if token != "β" else ""
current_probs = [prob]
else:
current_word += token
current_probs.append(prob)
if current_word and current_probs:
word_probs.append(sum(current_probs) / len(current_probs))
except Exception as e:
print("Error during word aggregation:", e)
return []
word_probs = [float(p) for p in word_probs]
return word_probs
# def get_word_classifications(text):
# text = " ".join(text.split(" ")[:2048])
# inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
# inputs = {k: v.to(device) for k, v in inputs.items()}
# tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
# with torch.no_grad():
# tags, emissions = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
# word_tags = []
# color_output = []
# current_word = ""
# current_prob = 0.0
# for token, prob in zip(tokens, tags[0]):
# if token in ["<s>", "</s>"]:
# continue
# if token.startswith("β"):
# if current_word:
# word_tags.append(round(current_prob, 3))
# color = (
# "green" if current_prob < 0.25 else
# "yellow" if current_prob < 0.5 else
# "orange" if current_prob < 0.75 else
# "red"
# )
# color_output.append(f'<span style="color:{color}">{current_word}</span>')
# current_word = token[1:] if token != "β" else ""
# current_prob = prob
# else:
# current_word += token
# current_prob = max(current_prob, prob)
# if current_word:
# word_tags.append(round(current_prob, 3))
# color = (
# "green" if current_prob < 0.25 else
# "yellow" if current_prob < 0.5 else
# "orange" if current_prob < 0.75 else
# "red"
# )
# color_output.append(f'<span style="color:{color}">{current_word}</span>')
# output = " ".join(color_output)
# return output, word_tags
# HF logging setup
def setup_hf_dataset():
global DATASET_CREATED
if not DATASET_CREATED and HF_TOKEN:
try:
create_repo(HF_DATASET_REPO, repo_type="dataset", token=HF_TOKEN, exist_ok=True)
DATASET_CREATED = True
print(f"Dataset {HF_DATASET_REPO} is ready.")
except Exception as e:
print(f"Error setting up dataset: {e}")
# Main inference + logging function
def infer_and_log(text_input):
word_tags = get_word_probabilities(text_input)
timestamp = datetime.datetime.now().isoformat()
submission_id = str(uuid.uuid4())
log_data = {
"id": submission_id,
"timestamp": timestamp,
"input": text_input,
"output_tags": word_tags
}
os.makedirs("logs", exist_ok=True)
log_file = f"logs/{timestamp.replace(':', '_')}.json"
with open(log_file, "w") as f:
json.dump(log_data, f, indent=2)
if HF_TOKEN and DATASET_CREATED:
try:
HfApi().upload_file(
path_or_fileobj=log_file,
path_in_repo=f"logs/{os.path.basename(log_file)}",
repo_id=HF_DATASET_REPO,
repo_type="dataset",
token=HF_TOKEN
)
print(f"Uploaded log {submission_id}")
except Exception as e:
print(f"Error uploading log: {e}")
return json.dumps(word_tags, indent=2)
def clear_fields():
return "", ""
# Prepare dataset once
setup_hf_dataset()
# Gradio UI
with gr.Blocks() as app:
gr.Markdown("Machine Generated Text Detector")
with gr.Row():
input_box = gr.Textbox(label="Input Text", lines=10)
output_box = gr.Textbox(label="Output Text", lines=10, interactive=False)
with gr.Row():
submit_btn = gr.Button("Submit")
clear_btn = gr.Button("Clear")
submit_btn.click(fn=infer_and_log, inputs=input_box, outputs=output_box)
clear_btn.click(fn=clear_fields, outputs=[input_box, output_box])
if __name__ == "__main__":
app.launch()
|