a0ms1n's picture
Update app.py
4230a5f
raw
history blame
1.97 kB
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()