sanjudebnath commited on
Commit
361c536
Β·
verified Β·
1 Parent(s): c5ed966

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -18
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import streamlit as st
2
- import numpy as np
3
  import torch
4
  from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering
5
 
@@ -11,35 +10,63 @@ def load_model():
11
  tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-distilled-squad")
12
  return model, tokenizer
13
 
 
 
 
 
 
 
 
 
 
14
  def get_answer(question, text, tokenizer, model):
15
- if "your name" or "who are you" or "about you" in question.lower():
 
16
  return "I am Numini, NativUttarMini, created by Sanju Debnath at University of Calcutta."
17
-
18
- inputs = tokenizer(question, text, return_tensors="pt", truncation=True, padding=True)
 
 
 
 
 
19
  with torch.no_grad():
20
  outputs = model(**inputs)
 
21
  start = torch.argmax(outputs.start_logits)
22
  end = torch.argmax(outputs.end_logits) + 1
23
- ans_tokens = inputs.input_ids[0][start:end]
24
- answer = tokenizer.decode(ans_tokens, skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
25
  return answer
26
 
27
  def main():
28
- st.write("# Question Answering Tool \n"
29
- "This tool will help you find answers to your questions about the text you provide. \n"
30
- "Please enter your question and the text you want to search in the boxes below.")
31
  model, tokenizer = load_model()
32
 
33
  with st.form("qa_form"):
34
- text = st.text_area("Enter your text here")
35
- question = st.text_input("Enter your question here")
36
-
37
- if st.form_submit_button("Submit"):
38
- data_load_state = st.text('Let me think about that...')
39
- answer = get_answer(question, text, tokenizer, model)
40
- if answer.strip() == "":
41
- data_load_state.text("Sorry but I don't know the answer to that question")
42
  else:
43
- data_load_state.text(answer)
 
 
44
 
45
  main()
 
 
1
  import streamlit as st
 
2
  import torch
3
  from transformers import DistilBertTokenizer, DistilBertForQuestionAnswering
4
 
 
10
  tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-distilled-squad")
11
  return model, tokenizer
12
 
13
+ def generate_prompt(question, text):
14
+ """Enhance the input prompt to guide the model better."""
15
+ return (
16
+ f"Context: {text}\n\n"
17
+ f"Instruction: Read the above context carefully and extract the most relevant answer.\n"
18
+ f"Question: {question}\n"
19
+ f"Answer:"
20
+ )
21
+
22
  def get_answer(question, text, tokenizer, model):
23
+ # Special case for bot identity
24
+ if any(phrase in question.lower() for phrase in ["your name", "who are you", "about you"]):
25
  return "I am Numini, NativUttarMini, created by Sanju Debnath at University of Calcutta."
26
+
27
+ # Enhance the input for better response
28
+ prompt_text = generate_prompt(question, text)
29
+
30
+ # Tokenize with truncation for better handling of large text
31
+ inputs = tokenizer(prompt_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
32
+
33
  with torch.no_grad():
34
  outputs = model(**inputs)
35
+
36
  start = torch.argmax(outputs.start_logits)
37
  end = torch.argmax(outputs.end_logits) + 1
38
+
39
+ # Handle cases where model is uncertain
40
+ if start >= end:
41
+ return "I couldn't find a clear answer in the given text."
42
+
43
+ answer = tokenizer.decode(inputs.input_ids[0][start:end], skip_special_tokens=True)
44
+
45
+ # Post-process answer for better readability
46
+ if len(answer.split()) < 3: # Model sometimes returns incomplete answers
47
+ return "I'm not sure about the exact answer. Can you try rephrasing the question?"
48
+
49
  return answer
50
 
51
  def main():
52
+ st.title("πŸ”Ž Advanced Question Answering Tool")
53
+ st.write("Ask a question based on the given text, and I'll extract the best possible answer.")
54
+
55
  model, tokenizer = load_model()
56
 
57
  with st.form("qa_form"):
58
+ text = st.text_area("πŸ“œ Enter the text/document:", height=200)
59
+ question = st.text_input("❓ Enter your question:")
60
+
61
+ if st.form_submit_button("πŸ” Get Answer"):
62
+ if not text.strip():
63
+ st.warning("⚠️ Please enter some text to analyze.")
64
+ elif not question.strip():
65
+ st.warning("⚠️ Please enter a question.")
66
  else:
67
+ st.text("πŸ€– Thinking...")
68
+ answer = get_answer(question, text, tokenizer, model)
69
+ st.success(f"βœ… Answer: {answer}")
70
 
71
  main()
72
+