Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,9 @@ import gradio as gr
|
|
| 12 |
# CONFIGURATION
|
| 13 |
###########################
|
| 14 |
|
| 15 |
-
DATA_DIR = "." #
|
| 16 |
-
MAX_CHUNKS =
|
|
|
|
| 17 |
|
| 18 |
###########################
|
| 19 |
# DATA EXTRACTION & CHUNKING
|
|
@@ -37,7 +38,8 @@ def extract_text(file_path):
|
|
| 37 |
return f.read()
|
| 38 |
return ''
|
| 39 |
|
| 40 |
-
def chunk_text(text, max_len=
|
|
|
|
| 41 |
sents = text.split('. ')
|
| 42 |
chunks, chunk = [], ''
|
| 43 |
for s in sents:
|
|
@@ -52,17 +54,17 @@ def chunk_text(text, max_len=400):
|
|
| 52 |
return chunks
|
| 53 |
|
| 54 |
all_chunks, all_meta = [], []
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
continue
|
| 59 |
file_path = os.path.join(DATA_DIR, fname)
|
| 60 |
subject = 'general'
|
| 61 |
try:
|
| 62 |
text = extract_text(file_path)
|
| 63 |
-
c = chunk_text(text, max_len=
|
| 64 |
for chunk in c:
|
| 65 |
-
|
|
|
|
| 66 |
all_chunks.append(chunk)
|
| 67 |
all_meta.append({'subject': subject, 'file': fname})
|
| 68 |
if len(all_chunks) >= MAX_CHUNKS:
|
|
@@ -75,15 +77,15 @@ if os.path.exists(DATA_DIR) and len(os.listdir(DATA_DIR)) > 0:
|
|
| 75 |
break
|
| 76 |
print('Chunks loaded:', len(all_chunks))
|
| 77 |
else:
|
| 78 |
-
print(
|
| 79 |
|
| 80 |
###########################
|
| 81 |
# EMBEDDINGS & INDEX
|
| 82 |
###########################
|
| 83 |
|
| 84 |
-
if
|
| 85 |
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 86 |
-
embeddings = encoder.encode(all_chunks, show_progress_bar=False, batch_size=
|
| 87 |
faiss.normalize_L2(embeddings)
|
| 88 |
index = faiss.IndexFlatIP(embeddings.shape[1])
|
| 89 |
index.add(np.array(embeddings).astype('float32'))
|
|
@@ -97,13 +99,13 @@ else:
|
|
| 97 |
def answer_query(
|
| 98 |
question,
|
| 99 |
subject=None,
|
| 100 |
-
top_k=
|
| 101 |
model_name="Qwen/Qwen2-0.5B"
|
| 102 |
):
|
| 103 |
-
if encoder is None or index is None:
|
| 104 |
-
return "No textbooks found or processed."
|
| 105 |
|
| 106 |
-
# Retrieval
|
| 107 |
q_emb = encoder.encode([question])
|
| 108 |
faiss.normalize_L2(q_emb)
|
| 109 |
D, I = index.search(np.asarray(q_emb, dtype='float32'), top_k*2)
|
|
@@ -116,9 +118,12 @@ def answer_query(
|
|
| 116 |
found.append(all_chunks[idx])
|
| 117 |
if len(found) >= top_k: break
|
| 118 |
context = "\n".join(found)
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
prompt = (
|
| 121 |
-
"You are an ICSE board AI tutor. Answer
|
|
|
|
| 122 |
f"Textbook excerpt:\n{context}\n\n"
|
| 123 |
f"Student question: {question}\n"
|
| 124 |
"Answer:"
|
|
@@ -130,18 +135,20 @@ def answer_query(
|
|
| 130 |
tokenizer=tokenizer,
|
| 131 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 132 |
device=0 if torch.cuda.is_available() else -1,
|
| 133 |
-
max_new_tokens=
|
| 134 |
do_sample=False,
|
| 135 |
pad_token_id=tokenizer.eos_token_id
|
| 136 |
)
|
| 137 |
output = pipe(prompt)[0]['generated_text']
|
| 138 |
|
| 139 |
-
# Clean output: only
|
| 140 |
answer_part = output.split("Answer:")[-1].strip()
|
|
|
|
| 141 |
for stop_word in ["Textbook", "Student question:", "Context:"]:
|
| 142 |
if stop_word in answer_part:
|
| 143 |
answer_part = answer_part.split(stop_word)[0].strip()
|
| 144 |
|
|
|
|
| 145 |
lines = answer_part.splitlines()
|
| 146 |
seen, unique = set(), []
|
| 147 |
for l in lines:
|
|
@@ -151,7 +158,9 @@ def answer_query(
|
|
| 151 |
seen.add(l)
|
| 152 |
final_answer = "\n".join(unique)
|
| 153 |
if not final_answer.strip():
|
| 154 |
-
return "Sorry, I could not
|
|
|
|
|
|
|
| 155 |
return final_answer
|
| 156 |
|
| 157 |
###########################
|
|
@@ -163,13 +172,13 @@ def gradio_chatbot(user_input, subject=""):
|
|
| 163 |
return answer_query(user_input, subject=subject_input)
|
| 164 |
|
| 165 |
TITLE = "ICSE Textbook Chatbot (Qwen2-0.5B)"
|
| 166 |
-
DESC = "Ask any question using ONLY your uploaded ICSE textbooks. The AI answers strictly from your syllabus."
|
| 167 |
|
| 168 |
iface = gr.Interface(
|
| 169 |
fn=gradio_chatbot,
|
| 170 |
inputs=[
|
| 171 |
gr.Textbox(label="Your question", placeholder="Type your question here...", lines=2),
|
| 172 |
-
gr.Textbox(label="Subject (optional)", placeholder="e.g., Physics, Geography", lines=1)
|
| 173 |
],
|
| 174 |
outputs="text",
|
| 175 |
title=TITLE,
|
|
@@ -178,4 +187,4 @@ iface = gr.Interface(
|
|
| 178 |
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
-
iface.launch()
|
|
|
|
| 12 |
# CONFIGURATION
|
| 13 |
###########################
|
| 14 |
|
| 15 |
+
DATA_DIR = "." # Looks in the Space/project root
|
| 16 |
+
MAX_CHUNKS = 10 # Conservative for Spaces, raises as needed
|
| 17 |
+
MAX_RESPONSE_CHARS = 4000 # Prevents protocol errors by truncating huge responses
|
| 18 |
|
| 19 |
###########################
|
| 20 |
# DATA EXTRACTION & CHUNKING
|
|
|
|
| 38 |
return f.read()
|
| 39 |
return ''
|
| 40 |
|
| 41 |
+
def chunk_text(text, max_len=300):
|
| 42 |
+
# Smaller chunks are safer for Spaces
|
| 43 |
sents = text.split('. ')
|
| 44 |
chunks, chunk = [], ''
|
| 45 |
for s in sents:
|
|
|
|
| 54 |
return chunks
|
| 55 |
|
| 56 |
all_chunks, all_meta = [], []
|
| 57 |
+
filelist = [f for f in os.listdir(DATA_DIR) if f.lower().endswith(('.pdf', '.docx', '.txt'))]
|
| 58 |
+
if filelist:
|
| 59 |
+
for fname in filelist:
|
|
|
|
| 60 |
file_path = os.path.join(DATA_DIR, fname)
|
| 61 |
subject = 'general'
|
| 62 |
try:
|
| 63 |
text = extract_text(file_path)
|
| 64 |
+
c = chunk_text(text, max_len=300)
|
| 65 |
for chunk in c:
|
| 66 |
+
# Only index nonempty, reasonbly sized chunks
|
| 67 |
+
if 40 < len(chunk) <= 1000:
|
| 68 |
all_chunks.append(chunk)
|
| 69 |
all_meta.append({'subject': subject, 'file': fname})
|
| 70 |
if len(all_chunks) >= MAX_CHUNKS:
|
|
|
|
| 77 |
break
|
| 78 |
print('Chunks loaded:', len(all_chunks))
|
| 79 |
else:
|
| 80 |
+
print("No textbook files found in current directory.")
|
| 81 |
|
| 82 |
###########################
|
| 83 |
# EMBEDDINGS & INDEX
|
| 84 |
###########################
|
| 85 |
|
| 86 |
+
if all_chunks:
|
| 87 |
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 88 |
+
embeddings = encoder.encode(all_chunks, show_progress_bar=False, batch_size=8)
|
| 89 |
faiss.normalize_L2(embeddings)
|
| 90 |
index = faiss.IndexFlatIP(embeddings.shape[1])
|
| 91 |
index.add(np.array(embeddings).astype('float32'))
|
|
|
|
| 99 |
def answer_query(
|
| 100 |
question,
|
| 101 |
subject=None,
|
| 102 |
+
top_k=1, # One chunk per answer is safest!
|
| 103 |
model_name="Qwen/Qwen2-0.5B"
|
| 104 |
):
|
| 105 |
+
if encoder is None or index is None or not all_chunks:
|
| 106 |
+
return "⚠️ No textbooks found or processed. Please check your file uploads."
|
| 107 |
|
| 108 |
+
# Retrieval, only the most relevant chunk
|
| 109 |
q_emb = encoder.encode([question])
|
| 110 |
faiss.normalize_L2(q_emb)
|
| 111 |
D, I = index.search(np.asarray(q_emb, dtype='float32'), top_k*2)
|
|
|
|
| 118 |
found.append(all_chunks[idx])
|
| 119 |
if len(found) >= top_k: break
|
| 120 |
context = "\n".join(found)
|
| 121 |
+
if not context.strip():
|
| 122 |
+
return "Sorry, I could not find an answer from the loaded textbooks."
|
| 123 |
+
|
| 124 |
prompt = (
|
| 125 |
+
"You are an ICSE board AI tutor. Answer using ONLY the following textbook excerpt. "
|
| 126 |
+
"Answer concisely and do not repeat the prompt or content.\n\n"
|
| 127 |
f"Textbook excerpt:\n{context}\n\n"
|
| 128 |
f"Student question: {question}\n"
|
| 129 |
"Answer:"
|
|
|
|
| 135 |
tokenizer=tokenizer,
|
| 136 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 137 |
device=0 if torch.cuda.is_available() else -1,
|
| 138 |
+
max_new_tokens=128,
|
| 139 |
do_sample=False,
|
| 140 |
pad_token_id=tokenizer.eos_token_id
|
| 141 |
)
|
| 142 |
output = pipe(prompt)[0]['generated_text']
|
| 143 |
|
| 144 |
+
# Clean output: only what's after the last "Answer:" line
|
| 145 |
answer_part = output.split("Answer:")[-1].strip()
|
| 146 |
+
# Truncate at first re-ask or echo marker
|
| 147 |
for stop_word in ["Textbook", "Student question:", "Context:"]:
|
| 148 |
if stop_word in answer_part:
|
| 149 |
answer_part = answer_part.split(stop_word)[0].strip()
|
| 150 |
|
| 151 |
+
# Remove duplicate lines and enforce length limit
|
| 152 |
lines = answer_part.splitlines()
|
| 153 |
seen, unique = set(), []
|
| 154 |
for l in lines:
|
|
|
|
| 158 |
seen.add(l)
|
| 159 |
final_answer = "\n".join(unique)
|
| 160 |
if not final_answer.strip():
|
| 161 |
+
return "Sorry, I could not generate an answer from the content."
|
| 162 |
+
if len(final_answer) > MAX_RESPONSE_CHARS:
|
| 163 |
+
final_answer = final_answer[:MAX_RESPONSE_CHARS] + "\n\n[Truncated]"
|
| 164 |
return final_answer
|
| 165 |
|
| 166 |
###########################
|
|
|
|
| 172 |
return answer_query(user_input, subject=subject_input)
|
| 173 |
|
| 174 |
TITLE = "ICSE Textbook Chatbot (Qwen2-0.5B)"
|
| 175 |
+
DESC = "Ask any question using ONLY your uploaded ICSE textbooks. The AI answers strictly from your syllabus content. For demo use only."
|
| 176 |
|
| 177 |
iface = gr.Interface(
|
| 178 |
fn=gradio_chatbot,
|
| 179 |
inputs=[
|
| 180 |
gr.Textbox(label="Your question", placeholder="Type your question here...", lines=2),
|
| 181 |
+
gr.Textbox(label="Subject (optional)", placeholder="e.g., Physics, Geography", lines=1)
|
| 182 |
],
|
| 183 |
outputs="text",
|
| 184 |
title=TITLE,
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
iface.launch(share=True)
|