DEADLOCK007X commited on
Commit
0bae633
·
verified ·
1 Parent(s): 931d213

Update tinyllama_inference.py

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +35 -20
tinyllama_inference.py CHANGED
@@ -1,42 +1,57 @@
1
  import json
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
  def load_model():
5
- # Use a public model for code evaluation.
6
  model_name = "Salesforce/codegen-350M-mono"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForCausalLM.from_pretrained(model_name)
9
  return tokenizer, model
10
 
11
  def evaluate_code(question, code):
12
- # Construct a prompt for the AI evaluator.
13
- prompt = f"""
14
- You are an expert code evaluator.
15
- Rate the user's solution to the following problem from 0-5 (0 = completely incorrect, 5 = excellent).
16
- Also provide a concise "feedback" message.
 
 
17
  Problem: "{question}"
18
  Solution: "{code}"
19
- Return ONLY valid JSON: {{"stars": number, "feedback": string}}
20
- Do not include any extra text outside the JSON.
21
  """
22
- # Load model and tokenizer (for simplicity, we load them per request; consider caching for performance)
23
  tokenizer, model = load_model()
 
24
  inputs = tokenizer(prompt, return_tensors="pt")
25
- outputs = model.generate(**inputs, max_new_tokens=150)
 
 
 
 
 
26
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
- try:
28
- result = json.loads(response_text.strip())
29
- except Exception as e:
30
- result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
 
 
 
 
 
 
 
 
31
  return result
32
 
33
- # For direct testing from the command line:
34
  if __name__ == "__main__":
35
  import sys
36
  if len(sys.argv) < 3:
37
  print(json.dumps({"error": "Please provide a question and code as arguments"}))
38
- else:
39
- question = sys.argv[1]
40
- code = sys.argv[2]
41
- result = evaluate_code(question, code)
42
- print(json.dumps(result))
 
1
  import json
2
+ import re
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
  def load_model():
6
+ # Using a public model for code evaluation.
7
  model_name = "Salesforce/codegen-350M-mono"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name)
10
  return tokenizer, model
11
 
12
  def evaluate_code(question, code):
13
+ # Refined prompt to enforce JSON-only output.
14
+ prompt = f"""You are an expert code evaluator.
15
+ Evaluate the user's solution to the following problem.
16
+ Return ONLY a JSON object with two keys:
17
+ - "stars": an integer between 0 and 5 (0 means completely incorrect, 5 means excellent).
18
+ - "feedback": a concise message.
19
+ Do not include any additional text.
20
  Problem: "{question}"
21
  Solution: "{code}"
 
 
22
  """
23
+ # Load model and tokenizer.
24
  tokenizer, model = load_model()
25
+ # Generate a response with reduced max tokens and a lower temperature for determinism.
26
  inputs = tokenizer(prompt, return_tensors="pt")
27
+ outputs = model.generate(
28
+ **inputs,
29
+ max_new_tokens=100,
30
+ temperature=0.2,
31
+ pad_token_id=tokenizer.eos_token_id
32
+ )
33
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
34
+
35
+ # Attempt to extract the JSON object from the response.
36
+ match = re.search(r'\{.*\}', response_text)
37
+ if match:
38
+ json_text = match.group(0)
39
+ try:
40
+ result = json.loads(json_text)
41
+ except Exception as e:
42
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
43
+ else:
44
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract JSON from AI response."}
45
+
46
  return result
47
 
48
+ # For direct testing from the command line.
49
  if __name__ == "__main__":
50
  import sys
51
  if len(sys.argv) < 3:
52
  print(json.dumps({"error": "Please provide a question and code as arguments"}))
53
+ sys.exit(1)
54
+ question = sys.argv[1]
55
+ code = sys.argv[2]
56
+ result = evaluate_code(question, code)
57
+ print(json.dumps(result))