File size: 2,965 Bytes
49b1ef8 71f5043 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd a37c9c1 132a2dd 49b1ef8 132a2dd 49b1ef8 349bcd6 132a2dd 349bcd6 132a2dd 349bcd6 132a2dd 349bcd6 49b1ef8 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd 71f5043 132a2dd 71f5043 132a2dd 49b1ef8 132a2dd 49b1ef8 132a2dd 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 |
import gradio as gr
import os
import json
import uuid
import torch
import datetime
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from huggingface_hub import HfApi, create_repo, upload_file
from datasets import Dataset
# Configuration
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english"
HF_DATASET_REPO = "M2ai/mgtd-logs"
HF_TOKEN = os.getenv("Mgtd")
DATASET_CREATED = False
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
# Make directories
os.makedirs("logs", exist_ok=True)
def setup_hf_dataset():
global DATASET_CREATED
if not DATASET_CREATED and HF_TOKEN:
try:
api = HfApi()
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}")
elif not HF_TOKEN:
print("Warning: HF_TOKEN not set. Logs will be saved locally only.")
def infer_and_log(text_input):
inputs = tokenizer(text_input, return_tensors="pt", truncation=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits.tolist()
predicted = torch.argmax(outputs.logits, dim=-1).item()
label = model.config.id2label[predicted]
timestamp = datetime.datetime.now().isoformat()
submission_id = str(uuid.uuid4())
log_data = {
"id": submission_id,
"timestamp": timestamp,
"input": text_input,
"logits": logits
}
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:
api = HfApi()
api.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} to {HF_DATASET_REPO}")
except Exception as e:
print(f"Error uploading to HF dataset: {e}")
return label
def clear_fields():
return "", ""
# Setup the dataset on startup
setup_hf_dataset()
with gr.Blocks() as app:
gr.Markdown("## AI Text Detector")
with gr.Row():
input_box = gr.Textbox(label="Input Text", lines=10, interactive=True)
output_box = gr.Textbox(label="Output", lines=2, 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()
|