harishkumarkotte commited on
Commit
089f363
·
verified ·
1 Parent(s): 7f9cd5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -38
app.py CHANGED
@@ -1,16 +1,8 @@
1
  import gradio as gr
2
  from transformers import pipeline , AutoTokenizer ,AutoModelForQuestionAnswering
3
- import string , re ,nltk
 
4
  from collections import Counter
5
- from nltk.corpus import stopwords
6
- from nltk.stem import WordNetLemmatizer
7
-
8
- lemmatizer = WordNetLemmatizer()
9
- nltk.download('stopwords')
10
- nltk.download('wordnet')
11
-
12
- stop_words = set(stopwords.words('english'))
13
-
14
  # Path to your custom-trained model
15
  model_path = "model/customTrained_Distilbert_Squad"
16
 
@@ -42,38 +34,41 @@ def normalize_answer(s):
42
  def exact_match_score(prediction, ground_truth):
43
  return normalize_answer(prediction) == normalize_answer(ground_truth)
44
 
45
- '''
46
- Return the F1 score, precision, and recall of the candidate answer given the reference answer
47
- '''
48
- def f1_score_with_precision_recall(reference, candidate):
49
- # Split the strings into sets of words
50
- reference = lemmatizer.lemmatize(normalize_answer(str(reference)))
51
- candidate = lemmatizer.lemmatize(normalize_answer(str(candidate)))
52
- words_reference = set(reference.split())
53
- words_candidate = set(candidate.split())
54
 
55
- # Calculate true positives, false positives, and false negatives
56
- tp = len(words_reference.intersection(words_candidate))
57
- fp = len(words_reference - words_candidate)
58
- fn = len(words_candidate - words_reference)
59
 
60
- # Calculate precision and recall
61
- precision = tp / (tp + fp) if (tp + fp) > 0 else 0
62
- recall = tp / (tp + fn) if (tp + fn) > 0 else 0
63
 
64
- # Calculate F1 score
65
- f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
66
 
67
- return {'f1': f1_score, 'precision': precision, 'recall': recall}
68
- '''
69
- Return the F1 score of the candidate answer given the reference answer
70
- '''
71
- def f1_score(reference, candidate):
72
- f1_stats = f1_score_with_precision_recall(reference, candidate)
73
- return f1_stats['f1']
74
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- def result(context,question,goldAnswer=""):
 
 
 
 
 
 
77
  # Perform question-answering
78
  predicted_result = qa_pipeline({
79
  'question': question,
@@ -88,11 +83,11 @@ def result(context,question,goldAnswer=""):
88
  predicted_answer = predicted_result['answer']
89
  # Compute Exact Match and F1 Score
90
  em_score = exact_match_score(predicted_answer, ground_truth)
91
- f1 = f1_score(predicted_answer, predicted_answer)
92
  return(f"'Answer': {predicted_result['answer']}"),(f"'Machine Answer': {predicted_result['answer']}"+" Vs 'Human Answer':"+ground_truth), (f"Exact Match: {em_score}"), (f"F1 Score: {f1}")
93
 
94
  demo = gr.Interface(
95
- fn=result,
96
  inputs=["text", "text","text"],
97
  outputs=["text","text","text","text"],
98
 
 
1
  import gradio as gr
2
  from transformers import pipeline , AutoTokenizer ,AutoModelForQuestionAnswering
3
+ import string
4
+ import re
5
  from collections import Counter
 
 
 
 
 
 
 
 
 
6
  # Path to your custom-trained model
7
  model_path = "model/customTrained_Distilbert_Squad"
8
 
 
34
  def exact_match_score(prediction, ground_truth):
35
  return normalize_answer(prediction) == normalize_answer(ground_truth)
36
 
37
+ def f1_score(prediction, ground_truth):
38
+ pred_tokens = normalize_answer(prediction).split()
39
+ truth_tokens = normalize_answer(ground_truth).split()
 
 
 
 
 
 
40
 
41
+ common_tokens = Counter(pred_tokens) & Counter(truth_tokens)
42
+ num_common = sum(common_tokens.values())
 
 
43
 
44
+ if num_common == 0:
45
+ return 0.0
 
46
 
47
+ precision = num_common / len(pred_tokens)
48
+ recall = num_common / len(truth_tokens)
49
 
50
+ f1 = 2 * (precision * recall) / (precision + recall)
51
+ return f1
 
 
 
 
 
52
 
53
+ def EM_ScoreF1(context,question,goldAnswer=""):
54
+ # Perform question-answering
55
+ predicted_result = qa_pipeline({
56
+ 'question': question,
57
+ 'context': context
58
+ })
59
+ # Ground truth (the correct answer)
60
+ if goldAnswer=="":
61
+ ground_truth = "Answer Unavailable"
62
+ else:
63
+ ground_truth = goldAnswer
64
 
65
+ # Get the predicted answer
66
+ predicted_answer = predicted_result['answer']
67
+ # Compute Exact Match and F1 Score
68
+ em_score = exact_match_score(predicted_answer, ground_truth)
69
+ f1 = f1_score(predicted_answer, ground_truth)
70
+ return(f"Machine Answer: {predicted_result['answer']}"+" Vs 'Human Answer':"+ground_truth), (f"Exact Match: {em_score}"), (f"F1 Score: {f1}")
71
+ def EM_ScoreF1(context,question,goldAnswer=""):
72
  # Perform question-answering
73
  predicted_result = qa_pipeline({
74
  'question': question,
 
83
  predicted_answer = predicted_result['answer']
84
  # Compute Exact Match and F1 Score
85
  em_score = exact_match_score(predicted_answer, ground_truth)
86
+ f1 = f1_score(predicted_answer, ground_truth)
87
  return(f"'Answer': {predicted_result['answer']}"),(f"'Machine Answer': {predicted_result['answer']}"+" Vs 'Human Answer':"+ground_truth), (f"Exact Match: {em_score}"), (f"F1 Score: {f1}")
88
 
89
  demo = gr.Interface(
90
+ fn=EM_ScoreF1,
91
  inputs=["text", "text","text"],
92
  outputs=["text","text","text","text"],
93