Spaces:
Sleeping
Sleeping
Added Calculate Score Functionality
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import re
|
| 4 |
from collections import Counter
|
|
|
|
| 5 |
|
| 6 |
@st.cache_resource
|
| 7 |
def load_qa_pipeline():
|
|
@@ -11,17 +12,13 @@ def normalize_answer(s):
|
|
| 11 |
"""Lower text and remove punctuation, articles and extra whitespace."""
|
| 12 |
def remove_articles(text):
|
| 13 |
return re.sub(r'\b(a|an|the)\b', ' ', text)
|
| 14 |
-
|
| 15 |
def white_space_fix(text):
|
| 16 |
return ' '.join(text.split())
|
| 17 |
-
|
| 18 |
def remove_punc(text):
|
| 19 |
exclude = set(string.punctuation)
|
| 20 |
return ''.join(ch for ch in text if ch not in exclude)
|
| 21 |
-
|
| 22 |
def lower(text):
|
| 23 |
return text.lower()
|
| 24 |
-
|
| 25 |
return white_space_fix(remove_articles(remove_punc(lower(s))))
|
| 26 |
|
| 27 |
def compute_exact_match(prediction, ground_truth):
|
|
@@ -47,9 +44,25 @@ def main():
|
|
| 47 |
|
| 48 |
# User input for context
|
| 49 |
context = st.text_area("Enter the context:", height=200)
|
|
|
|
| 50 |
|
| 51 |
# User input for question
|
| 52 |
question = st.text_input("Enter your question:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if st.button("Get Answer"):
|
| 55 |
if context and question:
|
|
@@ -61,22 +74,13 @@ def main():
|
|
| 61 |
st.write(result['answer'])
|
| 62 |
st.write(f"Confidence: {result['score']:.2f}")
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if st.button("Calculate Scores"):
|
| 72 |
-
if actual_answer:
|
| 73 |
-
em_score = compute_exact_match(result['answer'], actual_answer)
|
| 74 |
-
f1_score = compute_f1(result['answer'], actual_answer)
|
| 75 |
-
st.subheader("Scores:")
|
| 76 |
-
st.write(f"Exact Match: {em_score}")
|
| 77 |
-
st.write(f"F1 Score: {f1_score:.4f}")
|
| 78 |
-
else:
|
| 79 |
-
st.warning("Please enter the actual answer.")
|
| 80 |
else:
|
| 81 |
st.warning("Please provide both context and question.")
|
| 82 |
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import re
|
| 4 |
from collections import Counter
|
| 5 |
+
import string
|
| 6 |
|
| 7 |
@st.cache_resource
|
| 8 |
def load_qa_pipeline():
|
|
|
|
| 12 |
"""Lower text and remove punctuation, articles and extra whitespace."""
|
| 13 |
def remove_articles(text):
|
| 14 |
return re.sub(r'\b(a|an|the)\b', ' ', text)
|
|
|
|
| 15 |
def white_space_fix(text):
|
| 16 |
return ' '.join(text.split())
|
|
|
|
| 17 |
def remove_punc(text):
|
| 18 |
exclude = set(string.punctuation)
|
| 19 |
return ''.join(ch for ch in text if ch not in exclude)
|
|
|
|
| 20 |
def lower(text):
|
| 21 |
return text.lower()
|
|
|
|
| 22 |
return white_space_fix(remove_articles(remove_punc(lower(s))))
|
| 23 |
|
| 24 |
def compute_exact_match(prediction, ground_truth):
|
|
|
|
| 44 |
|
| 45 |
# User input for context
|
| 46 |
context = st.text_area("Enter the context:", height=200)
|
| 47 |
+
context = context.strip() # Remove leading/trailing whitespace
|
| 48 |
|
| 49 |
# User input for question
|
| 50 |
question = st.text_input("Enter your question:")
|
| 51 |
+
question = question.strip() # Remove leading/trailing whitespace
|
| 52 |
+
|
| 53 |
+
# Check for context and question length
|
| 54 |
+
if len(context) > 1500:
|
| 55 |
+
st.warning("Context should not exceed 1500 characters.")
|
| 56 |
+
return
|
| 57 |
+
if len(question) > 150:
|
| 58 |
+
st.warning("Question should not exceed 150 characters.")
|
| 59 |
+
return
|
| 60 |
+
|
| 61 |
+
# Option to calculate scores
|
| 62 |
+
calculate_scores = st.checkbox("Calculate scores")
|
| 63 |
+
|
| 64 |
+
if calculate_scores:
|
| 65 |
+
actual_answer = st.text_input("Enter the actual answer:")
|
| 66 |
|
| 67 |
if st.button("Get Answer"):
|
| 68 |
if context and question:
|
|
|
|
| 74 |
st.write(result['answer'])
|
| 75 |
st.write(f"Confidence: {result['score']:.2f}")
|
| 76 |
|
| 77 |
+
# Calculate and display scores if option is selected
|
| 78 |
+
if calculate_scores and actual_answer:
|
| 79 |
+
em_score = compute_exact_match(result['answer'], actual_answer)
|
| 80 |
+
f1_score = compute_f1(result['answer'], actual_answer)
|
| 81 |
+
st.subheader("Scores:")
|
| 82 |
+
st.write(f"Exact Match: {em_score}")
|
| 83 |
+
st.write(f"F1 Score: {f1_score:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
st.warning("Please provide both context and question.")
|
| 86 |
|