Download app.py from a0ms1n/AI-Code-Detector_for-Competitive-Programming: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/a0ms1n/AI-Code-Detector_for-Competitive-Programming/resolve/main/app.py
- Command line
-
hf download hf://spaces/a0ms1n/AI-Code-Detector_for-Competitive-Programming/app.py
-
curl -L -o app.py https://huggingface.co/spaces/a0ms1n/AI-Code-Detector_for-Competitive-Programming/resolve/main/app.py
2.72 kB
| 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() |