File size: 2,720 Bytes
5198b48
 
 
 
 
0920d0b
 
 
 
 
 
 
 
 
5198b48
de7419c
0e4c8f8
 
 
de7419c
 
 
 
 
5198b48
 
cab4e24
 
ea95da5
cf02fb2
5198b48
cf02fb2
5198b48
cf02fb2
5198b48
 
 
 
 
 
 
 
cab4e24
5198b48
 
 
 
837244b
5198b48
 
 
ea95da5
0e4c8f8
 
5198b48
 
 
 
 
 
 
 
 
 
 
 
 
75ee0f4
 
ea95da5
 
 
75ee0f4
 
 
5198b48
 
 
 
 
 
 
 
ef679ef
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
import gradio as gr
import Evaluate1
import Evaluate2
import re

import os
import torch

# CRITICAL: Redirect cache to temporary storage
os.environ['TORCH_HOME'] = '/tmp/torch_cache'
os.environ['HUB_DIR'] = '/tmp/torch_hub'
os.environ['TMPDIR'] = '/tmp'
torch.hub.set_dir('/tmp/torch_hub')

def load_cpp_file(file):
    try:
        text = file.name
        if file is None:
            return ""
        with open(text, 'r', encoding='utf-8') as f:
            content = f.read()
        return content
    except:
        return ""

def Evaluate_Code(code, method):
    if len(code) >= 1500000:
        return "Your code is too large", "-"
    choices=["V.1.1 (No Format)", "V.1.2 (Format Code)", "Combine"]
    if method == choices[0]:
        return Evaluate1.eval(code)
    elif method == choices[1]:
        return Evaluate2.eval(code)
    elif method == choices[2]:
        label1, conf1 = Evaluate1.eval(code)
        label2, conf2 = Evaluate2.eval(code)
        conf1_val = float(conf1.replace("%", "").strip())
        conf2_val = float(conf2.replace("%", "").strip())
        avg_conf = (conf1_val + conf2_val) / 2.0
        label = "AI" if avg_conf > 50.0 else "Human"
        return label, f"{avg_conf:.2f} %"
    else:
        return "Invalid method", "-"

with gr.Blocks() as web:
    with gr.Row():
        with gr.Column(scale=1):
            code_box = gr.Textbox(lines=20, label="** C++ Code")
        with gr.Column(scale=1):
            cpp_file = gr.File(label="Upload C++ File (.cpp)", file_types=[".cpp"])
            method_choice = gr.Radio(
                choices=["V.1.1 (No Format)", "V.1.2 (Format Code)", "Combine"],
                value="Combine",
                label="Choose Model"
            )
            check_btn = gr.Button("Check")

    # Result section
    with gr.Row():
        gr.Markdown("### Result :")

    with gr.Row():
        with gr.Column(scale=1):
            label_box = gr.Textbox(label="Label", interactive=False)
        with gr.Column(scale=1):
            confidence_box = gr.Textbox(label="AI Percentage", interactive=False)

    gr.Markdown(
        "<p style='font-size: 0.9em; color: gray;'>"
        "<strong>Notes:</strong><br>"
        "• The <em>Combined</em> method calculates the average result from two models and evaluates based on that.<br>"
        "• You can submit any type of text, but we recommend submitting C++ code, as that is what our model was trained on."
        "</p>"
    )
    
    # Bind functions
    cpp_file.change(fn=load_cpp_file, inputs=cpp_file, outputs=code_box)
    check_btn.click(
        fn=Evaluate_Code,
        inputs=[code_box, method_choice],
        outputs=[label_box, confidence_box]
    )

web.launch()