File size: 2,830 Bytes
b9b41fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModel
import torch.nn as nn

class CodeClassifier(nn.Module):
    def __init__(self, model_name="microsoft/codebert-base", num_labels=2):
        super().__init__()
        self.encoder = AutoModel.from_pretrained(model_name)
        self.classifier = nn.Linear(768, num_labels)
    
    def forward(self, input_ids, attention_mask):
        outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
        return self.classifier(outputs.last_hidden_state[:, 0, :])

def load_model():
    model = CodeClassifier(num_labels=2)
    
    try:
        from huggingface_hub import hf_hub_download
        model_path = hf_hub_download(
            repo_id="KrishnaKarthik/ai-code-detector",
            filename="ai_code_detector.pth"
        )
        model.load_state_dict(torch.load(model_path, map_location="cpu"))
        print("βœ… Loaded your trained AI model!")
    except Exception as e:
        print(f"❌ Error: {str(e)}")
    
    model.eval()
    return model

model = load_model()
tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")

def analyze_code(code):
    if not code.strip():
        return "Please enter some code", ""
    
    inputs = tokenizer(code, return_tensors="pt", truncation=True, max_length=512)
    
    with torch.no_grad():
        outputs = model(**inputs)
        probabilities = torch.softmax(outputs, dim=1)
        human_prob = probabilities[0][0].item()
        ai_prob = probabilities[0][1].item()
    
    human_percent = f"{human_prob:.1%}"
    ai_percent = f"{ai_prob:.1%}"
    
    if human_prob > 0.7:
        verdict = f"βœ… Likely Human-written ({human_prob:.1%} confidence)"
    elif ai_prob > 0.7:
        verdict = f"πŸ€– Likely AI-generated ({ai_prob:.1%} confidence)"
    else:
        verdict = f"⚠️ Uncertain - could be mixed"
    
    return human_percent, ai_percent, verdict

# Create Gradio interface
with gr.Blocks(title="AI Code Detector") as demo:
    gr.Markdown("# πŸ” AI-Generated Code Detector")
    gr.Markdown("Paste any code to check if it's AI-generated or human-written")
    
    with gr.Row():
        code_input = gr.Textbox(
            label="Paste your code here",
            placeholder="def hello_world():\n    print('Hello, World!')",
            lines=10
        )
    
    analyze_btn = gr.Button("Analyze Code", variant="primary")
    
    with gr.Row():
        human_output = gr.Textbox(label="Human-written Probability")
        ai_output = gr.Textbox(label="AI-generated Probability")
    
    verdict_output = gr.Textbox(label="Verdict")
    
    analyze_btn.click(
        fn=analyze_code,
        inputs=code_input,
        outputs=[human_output, ai_output, verdict_output]
    )

if __name__ == "__main__":
    demo.launch()