Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
# --------- Load Model ---------
|
| 6 |
-
MODEL_NAME = "google/gemma-2b-it"
|
| 7 |
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
|
@@ -13,54 +13,92 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 13 |
device_map="auto"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ---------
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
Requirement:
|
| 30 |
{requirement}
|
| 31 |
|
| 32 |
Code:
|
| 33 |
{code}
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 39 |
|
| 40 |
outputs = model.generate(
|
| 41 |
**inputs,
|
| 42 |
-
max_new_tokens=
|
| 43 |
-
temperature=0.
|
| 44 |
do_sample=False
|
| 45 |
)
|
| 46 |
|
| 47 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
|
| 49 |
-
# Extract
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
return "✅ YES"
|
| 52 |
-
elif "NO" in
|
| 53 |
return "❌ NO"
|
| 54 |
else:
|
| 55 |
-
return "⚠️ Unable to determine"
|
| 56 |
|
| 57 |
# --------- UI ---------
|
| 58 |
with gr.Blocks() as app:
|
| 59 |
-
gr.Markdown("## 🧠 Code Requirement Validator (Gemma
|
| 60 |
-
gr.Markdown("
|
| 61 |
|
| 62 |
-
requirement = gr.Textbox(
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
output = gr.Textbox(label="Result")
|
| 66 |
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
# --------- Load Model ---------
|
| 6 |
+
MODEL_NAME = "google/gemma-2b-it"
|
| 7 |
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
|
|
|
| 13 |
device_map="auto"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# --------- Improved Prompt Function ---------
|
| 17 |
+
def pre_processing(requirement, code):
|
| 18 |
+
return f"""
|
| 19 |
+
# ROLE
|
| 20 |
+
You are a strict software testing system.
|
| 21 |
+
You evaluate Python code against given requirements and do not make assumptions.
|
| 22 |
+
|
| 23 |
+
# DESCRIPTION
|
| 24 |
+
Your job is to compare the Python code against the requirement
|
| 25 |
+
and ensure that the code strictly meets the requirement.
|
| 26 |
+
|
| 27 |
+
# INSTRUCTIONS
|
| 28 |
+
Step 1: Break the requirement into clear testable conditions.
|
| 29 |
+
Step 2: Check the code against each condition.
|
| 30 |
+
Step 3: Mark each condition as YES or NO.
|
| 31 |
+
Step 4: If all conditions are YES → YES, else → NO.
|
| 32 |
+
|
| 33 |
+
# RULES
|
| 34 |
+
- Be strict
|
| 35 |
+
- Do not assume missing functionality
|
| 36 |
+
- Do not execute the code
|
| 37 |
+
- Do not guess
|
| 38 |
+
- If unsure, mark NO
|
| 39 |
+
- Follow output format exactly
|
| 40 |
+
|
| 41 |
+
# INPUT DATA
|
| 42 |
Requirement:
|
| 43 |
{requirement}
|
| 44 |
|
| 45 |
Code:
|
| 46 |
{code}
|
| 47 |
|
| 48 |
+
# OUTPUT FORMAT
|
| 49 |
+
|
| 50 |
+
Conditions:
|
| 51 |
+
1. Condition
|
| 52 |
+
2. Condition
|
| 53 |
+
|
| 54 |
+
Evaluation of each condition:
|
| 55 |
+
1. YES/NO
|
| 56 |
+
2. YES/NO
|
| 57 |
+
|
| 58 |
+
Final Answer: YES or NO
|
| 59 |
"""
|
| 60 |
|
| 61 |
+
# --------- Core Function ---------
|
| 62 |
+
def evaluate_code(requirement, code):
|
| 63 |
+
prompt = pre_processing(requirement, code)
|
| 64 |
+
|
| 65 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 66 |
|
| 67 |
outputs = model.generate(
|
| 68 |
**inputs,
|
| 69 |
+
max_new_tokens=150, # increased for structured output
|
| 70 |
+
temperature=0.1, # more deterministic
|
| 71 |
do_sample=False
|
| 72 |
)
|
| 73 |
|
| 74 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 75 |
|
| 76 |
+
# --------- Extract Final Answer ---------
|
| 77 |
+
response_upper = response.upper()
|
| 78 |
+
|
| 79 |
+
if "FINAL ANSWER: YES" in response_upper:
|
| 80 |
return "✅ YES"
|
| 81 |
+
elif "FINAL ANSWER: NO" in response_upper:
|
| 82 |
return "❌ NO"
|
| 83 |
else:
|
| 84 |
+
return "⚠️ Unable to determine\n\nFull Response:\n" + response
|
| 85 |
|
| 86 |
# --------- UI ---------
|
| 87 |
with gr.Blocks() as app:
|
| 88 |
+
gr.Markdown("## 🧠 Code Requirement Validator (Gemma)")
|
| 89 |
+
gr.Markdown("Checks if your Python code satisfies a requirement using structured evaluation.")
|
| 90 |
|
| 91 |
+
requirement = gr.Textbox(
|
| 92 |
+
label="Requirement",
|
| 93 |
+
lines=4,
|
| 94 |
+
placeholder="e.g. Function should return sum of two numbers"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
code = gr.Textbox(
|
| 98 |
+
label="Code",
|
| 99 |
+
lines=10,
|
| 100 |
+
placeholder="Paste your Python code here..."
|
| 101 |
+
)
|
| 102 |
|
| 103 |
output = gr.Textbox(label="Result")
|
| 104 |
|