Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
16 |
return "I am Numini, NativUttarMini, created by Sanju Debnath at University of Calcutta."
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return answer
|
26 |
|
27 |
def main():
|
28 |
-
st.
|
29 |
-
|
30 |
-
|
31 |
model, tokenizer = load_model()
|
32 |
|
33 |
with st.form("qa_form"):
|
34 |
-
text = st.text_area("Enter
|
35 |
-
question = st.text_input("Enter your question
|
36 |
-
|
37 |
-
if st.form_submit_button("
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
else:
|
43 |
-
|
|
|
|
|
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 |
+
|