Spaces:
Paused
Paused
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig | |
| # --------- Model Config --------- | |
| MODEL_NAME = "google/gemma-7b-it" | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_compute_dtype=torch.float16 | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_NAME, | |
| quantization_config=bnb_config, | |
| device_map="auto" | |
| ) | |
| # --------- General Prompt (Test-Case Based) --------- | |
| def pre_processing(requirement, code): | |
| return f""" | |
| # ROLE | |
| You are a strict Python code evaluator. | |
| # TASK | |
| Determine whether the code satisfies the requirement. | |
| # INSTRUCTIONS | |
| Step 1: Convert the requirement into 3 clear test cases (input β expected output). | |
| Step 2: Analyze the code logic carefully. | |
| Step 3: Simulate the code for each test case (DO NOT execute, reason mentally). | |
| Step 4: Compare actual vs expected outputs. | |
| # DECISION RULE | |
| - If ALL test cases match β YES | |
| - If ANY test case fails β NO | |
| # RULES | |
| - Be strict | |
| - Do not assume missing logic | |
| - Do not execute code | |
| - Do not guess | |
| - Include edge cases (if applicable) | |
| # OUTPUT FORMAT | |
| Test Cases: | |
| 1. Input β Expected Output | |
| 2. Input β Expected Output | |
| 3. Input β Expected Output | |
| Evaluation: | |
| 1. PASS/FAIL | |
| 2. PASS/FAIL | |
| 3. PASS/FAIL | |
| Final Answer: YES or NO | |
| # INPUT | |
| Requirement: | |
| {requirement} | |
| Code: | |
| {code} | |
| """ | |
| # --------- Core Function --------- | |
| def evaluate_code(requirement, code): | |
| prompt = pre_processing(requirement, code) | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=60, | |
| temperature=0.1, | |
| do_sample=False, | |
| repetition_penalty=1.2 | |
| ) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| response_upper = response.upper() | |
| # Extract final answer | |
| if "FINAL ANSWER: YES" in response_upper: | |
| return "β YES" | |
| elif "FINAL ANSWER: NO" in response_upper: | |
| return "β NO" | |
| else: | |
| return "β οΈ Unable to determine\n\n" + response | |
| # --------- UI --------- | |
| with gr.Blocks() as app: | |
| gr.Markdown("## π§ Code Requirement Validator (Gemma 7B - General)") | |
| gr.Markdown("Evaluates code using test-case based reasoning (high accuracy).") | |
| requirement = gr.Textbox(label="Requirement", lines=4) | |
| code = gr.Textbox(label="Code", lines=10) | |
| output = gr.Textbox(label="Result") | |
| btn = gr.Button("Evaluate") | |
| btn.click(fn=evaluate_code, inputs=[requirement, code], outputs=output) | |
| app.launch() |