|
import torch |
|
import sys |
|
import os |
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) |
|
from all_models import models |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
try: |
|
models.flan_model.to(device) |
|
except Exception as e: |
|
print(f"Warning: Could not move model to device {device}: {e}") |
|
|
|
def llm_score(correct_answers, answer): |
|
try: |
|
|
|
if isinstance(correct_answers, str): |
|
correct_answers = [correct_answers] |
|
|
|
score = [] |
|
|
|
|
|
model = models.get_flan_model() |
|
tokenizer = models.flan_tokenizer |
|
|
|
|
|
for correct_answer in correct_answers: |
|
try: |
|
|
|
input_text = f"Compare these answers and give a similarity score between 0 and 1:\nCorrect: {correct_answer}\nStudent: {answer}" |
|
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True) |
|
inputs = {k: v.to(models.device) for k, v in inputs.items()} |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model.generate( |
|
**inputs, |
|
max_length=50, |
|
num_return_sequences=1, |
|
temperature=0.7, |
|
do_sample=True |
|
) |
|
|
|
|
|
score_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
try: |
|
|
|
score_value = float(score_text.split()[-1]) |
|
score.append(min(max(score_value, 0.0), 1.0)) |
|
except (ValueError, IndexError): |
|
|
|
score.append(0.5) |
|
|
|
except Exception as e: |
|
logger.error(f"Error processing answer: {str(e)}") |
|
score.append(0.5) |
|
|
|
|
|
del inputs |
|
del outputs |
|
torch.cuda.empty_cache() |
|
|
|
return score |
|
|
|
except Exception as e: |
|
logger.error(f"Error in llm_score: {str(e)}") |
|
return [0.5] |
|
finally: |
|
|
|
models.release_flan_model() |
|
|