Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,9 @@ import re
|
|
| 13 |
# CONFIGURATION
|
| 14 |
###########################
|
| 15 |
|
| 16 |
-
DATA_DIR = "." #
|
| 17 |
-
MAX_CHUNKS =
|
| 18 |
-
MAX_RESPONSE_CHARS = 600
|
| 19 |
|
| 20 |
###########################
|
| 21 |
# DATA EXTRACTION & CHUNKING
|
|
@@ -93,21 +93,29 @@ else:
|
|
| 93 |
encoder, embeddings, index = None, None, None
|
| 94 |
|
| 95 |
###########################
|
| 96 |
-
#
|
| 97 |
###########################
|
| 98 |
|
| 99 |
-
def
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
seen, unique = set(), []
|
| 104 |
for s in sentences:
|
| 105 |
st = s.strip()
|
| 106 |
-
|
| 107 |
-
if st and
|
| 108 |
unique.append(st)
|
| 109 |
-
seen.add(
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
def answer_query(
|
| 113 |
question,
|
|
@@ -118,7 +126,6 @@ def answer_query(
|
|
| 118 |
if encoder is None or index is None:
|
| 119 |
return "No textbooks found or processed."
|
| 120 |
|
| 121 |
-
# Retrieval: use only the single best chunk, as repetition gets worse with many
|
| 122 |
q_emb = encoder.encode([question])
|
| 123 |
faiss.normalize_L2(q_emb)
|
| 124 |
D, I = index.search(np.asarray(q_emb, dtype='float32'), top_k)
|
|
@@ -134,7 +141,8 @@ def answer_query(
|
|
| 134 |
|
| 135 |
prompt = (
|
| 136 |
"You are an ICSE board AI tutor. Answer ONLY using the textbook excerpt below. "
|
| 137 |
-
"
|
|
|
|
| 138 |
f"Textbook excerpt:\n{context}\n\n"
|
| 139 |
f"Student question: {question}\n"
|
| 140 |
"Answer:"
|
|
@@ -147,7 +155,7 @@ def answer_query(
|
|
| 147 |
tokenizer=tokenizer,
|
| 148 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 149 |
device=0 if torch.cuda.is_available() else -1,
|
| 150 |
-
max_new_tokens=64,
|
| 151 |
do_sample=False,
|
| 152 |
pad_token_id=tokenizer.eos_token_id
|
| 153 |
)
|
|
@@ -157,8 +165,7 @@ def answer_query(
|
|
| 157 |
for stop_word in ["Textbook", "Student question:", "Context:"]:
|
| 158 |
if stop_word in answer_part:
|
| 159 |
answer_part = answer_part.split(stop_word)[0].strip()
|
| 160 |
-
|
| 161 |
-
clean = remove_duplicate_sentences(answer_part)
|
| 162 |
if len(clean) > MAX_RESPONSE_CHARS:
|
| 163 |
clean = clean[:MAX_RESPONSE_CHARS] + "\n\n[truncated]"
|
| 164 |
if not clean.strip():
|
|
|
|
| 13 |
# CONFIGURATION
|
| 14 |
###########################
|
| 15 |
|
| 16 |
+
DATA_DIR = "." # Look in the root directory of the Space
|
| 17 |
+
MAX_CHUNKS = 10 # Smaller batches = faster loading & inference
|
| 18 |
+
MAX_RESPONSE_CHARS = 600 # Hard cutoff to prevent runaway generation
|
| 19 |
|
| 20 |
###########################
|
| 21 |
# DATA EXTRACTION & CHUNKING
|
|
|
|
| 93 |
encoder, embeddings, index = None, None, None
|
| 94 |
|
| 95 |
###########################
|
| 96 |
+
# CLEAN-UP FUNCTION
|
| 97 |
###########################
|
| 98 |
|
| 99 |
+
def clean_and_remove_incomplete_last_sentence(text):
|
| 100 |
+
"""
|
| 101 |
+
Removes duplicate sentences and deletes the last (possibly incomplete) sentence
|
| 102 |
+
if it doesn't end with a period (or ! or ?).
|
| 103 |
+
"""
|
| 104 |
+
sentences = re.split(r'(?<=[.!?]) +', text)
|
| 105 |
seen, unique = set(), []
|
| 106 |
for s in sentences:
|
| 107 |
st = s.strip()
|
| 108 |
+
s_key = st.lower()
|
| 109 |
+
if st and s_key not in seen:
|
| 110 |
unique.append(st)
|
| 111 |
+
seen.add(s_key)
|
| 112 |
+
if unique and not unique[-1].endswith(('.', '!', '?')):
|
| 113 |
+
unique = unique[:-1]
|
| 114 |
+
return ' '.join(unique).strip()
|
| 115 |
+
|
| 116 |
+
###########################
|
| 117 |
+
# ANSWER FUNCTION
|
| 118 |
+
###########################
|
| 119 |
|
| 120 |
def answer_query(
|
| 121 |
question,
|
|
|
|
| 126 |
if encoder is None or index is None:
|
| 127 |
return "No textbooks found or processed."
|
| 128 |
|
|
|
|
| 129 |
q_emb = encoder.encode([question])
|
| 130 |
faiss.normalize_L2(q_emb)
|
| 131 |
D, I = index.search(np.asarray(q_emb, dtype='float32'), top_k)
|
|
|
|
| 141 |
|
| 142 |
prompt = (
|
| 143 |
"You are an ICSE board AI tutor. Answer ONLY using the textbook excerpt below. "
|
| 144 |
+
"Give a COMPLETE, single, concise answer, ending with proper punctuation. "
|
| 145 |
+
"Do NOT repeat yourself or echo the prompt. End your answer with a full stop.\n\n"
|
| 146 |
f"Textbook excerpt:\n{context}\n\n"
|
| 147 |
f"Student question: {question}\n"
|
| 148 |
"Answer:"
|
|
|
|
| 155 |
tokenizer=tokenizer,
|
| 156 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 157 |
device=0 if torch.cuda.is_available() else -1,
|
| 158 |
+
max_new_tokens=64, # or even 80 for slightly longer output
|
| 159 |
do_sample=False,
|
| 160 |
pad_token_id=tokenizer.eos_token_id
|
| 161 |
)
|
|
|
|
| 165 |
for stop_word in ["Textbook", "Student question:", "Context:"]:
|
| 166 |
if stop_word in answer_part:
|
| 167 |
answer_part = answer_part.split(stop_word)[0].strip()
|
| 168 |
+
clean = clean_and_remove_incomplete_last_sentence(answer_part)
|
|
|
|
| 169 |
if len(clean) > MAX_RESPONSE_CHARS:
|
| 170 |
clean = clean[:MAX_RESPONSE_CHARS] + "\n\n[truncated]"
|
| 171 |
if not clean.strip():
|