File size: 1,974 Bytes
5198b48 de7419c 0e4c8f8 de7419c 5198b48 4230a5f 0e4c8f8 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 | import gradio as gr
import Evaluate1
import Evaluate2
import re
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 method == "No Format":
return Evaluate1.eval(code)
elif method == "Format Code":
return Evaluate2.eval(code)
elif method == "Combine":
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", "0.00 %"
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)
# 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() |