DEADLOCK007X commited on
Commit
e65d0ad
·
verified ·
1 Parent(s): 1ba3747

Update tinyllama_inference.py

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +43 -44
tinyllama_inference.py CHANGED
@@ -1,44 +1,43 @@
1
- # CODEXGAME/backend/ai_evaluator/tinyllama_inference.py
2
-
3
- import json
4
- import torch
5
- from transformers import AutoTokenizer, AutoModelForCausalLM
6
-
7
- def load_model():
8
- # Change the model identifier if needed – this should be a TinyLlama variant available on Hugging Face.
9
- model_name = "TheBloke/tiny-llama-7b"
10
- tokenizer = AutoTokenizer.from_pretrained(model_name)
11
- model = AutoModelForCausalLM.from_pretrained(model_name)
12
- return tokenizer, model
13
-
14
- def evaluate_code(question, code):
15
- # Construct a prompt for the AI evaluator.
16
- prompt = f"""
17
- You are an expert code evaluator.
18
- Rate the user's solution to the following problem from 0-5 (0 = completely incorrect, 5 = excellent).
19
- Also provide a concise "feedback" message.
20
- Problem: "{question}"
21
- Solution: "{code}"
22
- Return ONLY valid JSON: {{"stars": number, "feedback": string}}
23
- Do not include any extra text outside the JSON.
24
- """
25
- tokenizer, model = load_model()
26
- inputs = tokenizer(prompt, return_tensors="pt")
27
- outputs = model.generate(**inputs, max_new_tokens=150)
28
- response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
- try:
30
- result = json.loads(response_text.strip())
31
- except Exception as e:
32
- result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
33
- return result
34
-
35
- # For direct testing from the command line
36
- if __name__ == "__main__":
37
- import sys
38
- if len(sys.argv) < 3:
39
- print(json.dumps({"error": "Please provide a question and code as arguments"}))
40
- sys.exit(1)
41
- question = sys.argv[1]
42
- code = sys.argv[2]
43
- result = evaluate_code(question, code)
44
- print(json.dumps(result))
 
1
+ import json
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+ def load_model():
6
+ # Use 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
+ # Construct a prompt for the AI evaluator.
14
+ prompt = f"""
15
+ You are an expert code evaluator.
16
+ Rate the user's solution to the following problem from 0-5 (0 = completely incorrect, 5 = excellent).
17
+ Also provide a concise "feedback" message.
18
+ Problem: "{question}"
19
+ Solution: "{code}"
20
+ Return ONLY valid JSON: {{"stars": number, "feedback": string}}
21
+ Do not include any extra text outside the JSON.
22
+ """
23
+ # Load the model and tokenizer.
24
+ tokenizer, model = load_model()
25
+ inputs = tokenizer(prompt, return_tensors="pt")
26
+ outputs = model.generate(**inputs, max_new_tokens=150)
27
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+ try:
29
+ result = json.loads(response_text.strip())
30
+ except Exception as e:
31
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
32
+ return result
33
+
34
+ # For direct testing from the command line
35
+ if __name__ == "__main__":
36
+ import sys
37
+ if len(sys.argv) < 3:
38
+ print(json.dumps({"error": "Please provide a question and code as arguments"}))
39
+ sys.exit(1)
40
+ question = sys.argv[1]
41
+ code = sys.argv[2]
42
+ result = evaluate_code(question, code)
43
+ print(json.dumps(result))