Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion backend/Generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import fitz
import mammoth




class MCQGenerator:

def __init__(self):
Expand Down Expand Up @@ -251,6 +254,10 @@ def __init__(self):
self.nli_tokenizer = AutoTokenizer.from_pretrained(self.nli_model_name)
self.nli_model = AutoModelForSequenceClassification.from_pretrained(self.nli_model_name)

# Explicitly push the NLI model to the detected hardware (GPU or CPU)
self.nli_model.to(self.device)
self.nli_model.eval()

self.set_seed(42)

def set_seed(self, seed):
Expand Down Expand Up @@ -286,7 +293,8 @@ def predict_answer(self, payload):
torch.cuda.empty_cache()

return answers


@torch.no_grad()
def predict_boolean_answer(self, payload):
input_text = payload.get("input_text", "")
input_questions = payload.get("input_question", [])
Expand All @@ -296,6 +304,10 @@ def predict_boolean_answer(self, payload):
for question in input_questions:
hypothesis = question
inputs = self.nli_tokenizer.encode_plus(input_text, hypothesis, return_tensors="pt")

# Push the input tensors to the same device as the model
inputs = {key: value.to(self.device) for key, value in inputs.items()}

outputs = self.nli_model(**inputs)
logits = outputs.logits
probabilities = torch.softmax(logits, dim=1)
Expand Down