Amala06 commited on
Commit
273b0a2
·
verified ·
1 Parent(s): 91eb8de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -25
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" # instruction-tuned version
7
 
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
 
@@ -13,54 +13,92 @@ model = AutoModelForCausalLM.from_pretrained(
13
  device_map="auto"
14
  )
15
 
16
- # --------- Core Function ---------
17
- def evaluate_code(requirement, code):
18
- prompt = f"""
19
- You are a strict software evaluator.
20
-
21
- Your task:
22
- Check if the given code satisfies the requirement.
23
-
24
- Rules:
25
- - Answer ONLY "YES" or "NO"
26
- - Do NOT explain
27
- - Be strict and logical
28
-
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  Requirement:
30
  {requirement}
31
 
32
  Code:
33
  {code}
34
 
35
- Answer:
 
 
 
 
 
 
 
 
 
 
36
  """
37
 
 
 
 
 
38
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
39
 
40
  outputs = model.generate(
41
  **inputs,
42
- max_new_tokens=5,
43
- temperature=0.2, # low randomness for consistency
44
  do_sample=False
45
  )
46
 
47
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
 
49
- # Extract only YES/NO
50
- if "YES" in response.upper():
 
 
51
  return "✅ YES"
52
- elif "NO" in response.upper():
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 2)")
60
- gr.Markdown("Enter a requirement and code. The model will check if the requirement is met.")
61
 
62
- requirement = gr.Textbox(label="Requirement", lines=4, placeholder="e.g. Function should return sum of two numbers")
63
- code = gr.Textbox(label="Code", lines=10, placeholder="Paste your code here...")
 
 
 
 
 
 
 
 
 
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