Garvitj commited on
Commit
3cb5eb1
·
verified ·
1 Parent(s): 0b8902f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -8,6 +8,7 @@ from typing import List
8
  import torch
9
  from transformers import BertTokenizer, BertModel
10
  import torch.nn.functional as F
 
11
 
12
  # Load pre-trained models
13
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
@@ -23,6 +24,9 @@ system_prompt = {
23
  "content": "You are a useful assistant. You reply with efficient answers."
24
  }
25
 
 
 
 
26
  async def chat_groq(message, history):
27
  messages = [system_prompt]
28
  for msg in history:
@@ -99,12 +103,26 @@ def calculate_sentence_similarity(text1, text2):
99
  embedding2 = sentence_model.encode(text2, convert_to_tensor=True)
100
  return util.pytorch_cos_sim(embedding1, embedding2).item()
101
 
 
 
 
 
 
 
 
 
 
102
  def compare_answers(student_answer, teacher_answer):
103
  bert_similarity = calculate_cosine_similarity(get_bert_embedding(student_answer), get_bert_embedding(teacher_answer))
104
  sentence_similarity = calculate_sentence_similarity(student_answer, teacher_answer)
105
-
106
  # Use a higher weight for BERT similarity
107
- final_similarity = (0.75 * bert_similarity + 0.25 * sentence_similarity)
 
 
 
 
 
108
  return final_similarity
109
 
110
  def extract_keywords(text):
 
8
  import torch
9
  from transformers import BertTokenizer, BertModel
10
  import torch.nn.functional as F
11
+ import language_tool_python # Import LanguageTool for grammar checking
12
 
13
  # Load pre-trained models
14
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 
24
  "content": "You are a useful assistant. You reply with efficient answers."
25
  }
26
 
27
+ # Initialize grammar checker
28
+ tool = language_tool_python.LanguageTool('en-US')
29
+
30
  async def chat_groq(message, history):
31
  messages = [system_prompt]
32
  for msg in history:
 
103
  embedding2 = sentence_model.encode(text2, convert_to_tensor=True)
104
  return util.pytorch_cos_sim(embedding1, embedding2).item()
105
 
106
+ def check_grammar(student_answer):
107
+ # Check grammar using LanguageTool
108
+ matches = tool.check(student_answer)
109
+ errors = len(matches)
110
+
111
+ # Apply a penalty based on the number of grammar errors
112
+ penalty = 1 - min(0.1 * errors, 0.5) # Maximum penalty is 50%
113
+ return penalty
114
+
115
  def compare_answers(student_answer, teacher_answer):
116
  bert_similarity = calculate_cosine_similarity(get_bert_embedding(student_answer), get_bert_embedding(teacher_answer))
117
  sentence_similarity = calculate_sentence_similarity(student_answer, teacher_answer)
118
+
119
  # Use a higher weight for BERT similarity
120
+ semantic_similarity = (0.75 * bert_similarity + 0.25 * sentence_similarity)
121
+
122
+ # Apply grammar penalty
123
+ grammar_penalty = check_grammar(student_answer)
124
+ final_similarity = semantic_similarity * grammar_penalty
125
+
126
  return final_similarity
127
 
128
  def extract_keywords(text):