adinarayana commited on
Commit
9be9f60
·
verified ·
1 Parent(s): 68de083

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -2
app.py CHANGED
@@ -33,7 +33,7 @@ def preprocess_text(element):
33
  return ""
34
 
35
 
36
- def answer_question(text, question):
37
  """Answers a question using the provided text and a pre-trained model.
38
 
39
  Args:
@@ -43,11 +43,15 @@ def answer_question(text, question):
43
  Returns:
44
  The answer extracted from the text using the model.
45
  """
46
- qa_model_name = "bert-base-uncased" # Replace with your chosen TensorFlow QA model
47
 
48
  qa_model = TFBertForQuestionAnswering.from_pretrained(qa_model_name)
49
  tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
50
 
 
 
 
 
51
  inputs = tokenizer(question, text, return_tensors="tf") # Tokenize inputs for TensorFlow
52
 
53
  start_logits, end_logits = qa_model(inputs)
 
33
  return ""
34
 
35
 
36
+ def answer_question(text, question, max_length=512):
37
  """Answers a question using the provided text and a pre-trained model.
38
 
39
  Args:
 
43
  Returns:
44
  The answer extracted from the text using the model.
45
  """
46
+ qa_model_name = "bert-base-uncased" # Replace with your chosen model
47
 
48
  qa_model = TFBertForQuestionAnswering.from_pretrained(qa_model_name)
49
  tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
50
 
51
+ # Truncate text if necessary
52
+ if len(text) > max_length:
53
+ text = text[:max_length]
54
+
55
  inputs = tokenizer(question, text, return_tensors="tf") # Tokenize inputs for TensorFlow
56
 
57
  start_logits, end_logits = qa_model(inputs)