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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
import docx
|
| 5 |
+
import faiss
|
| 6 |
+
import torch
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from transformers import pipeline, AutoTokenizer
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
###########################
|
| 12 |
+
# CONFIGURATION
|
| 13 |
+
###########################
|
| 14 |
+
|
| 15 |
+
DATA_DIR = "my_icse_textbooks" # <= Put your textbook folder in the Space's root with this name, or change as needed
|
| 16 |
+
MAX_CHUNKS = 1000 # Increase if you have more RAM!
|
| 17 |
+
|
| 18 |
+
###########################
|
| 19 |
+
# DATA EXTRACTION & CHUNKING
|
| 20 |
+
###########################
|
| 21 |
+
|
| 22 |
+
def extract_text(file_path):
|
| 23 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 24 |
+
if ext == '.pdf':
|
| 25 |
+
text = ''
|
| 26 |
+
with open(file_path, 'rb') as f:
|
| 27 |
+
reader = PdfReader(f)
|
| 28 |
+
for page in reader.pages:
|
| 29 |
+
t = page.extract_text()
|
| 30 |
+
if t: text += t + ' '
|
| 31 |
+
return text
|
| 32 |
+
elif ext == '.docx':
|
| 33 |
+
docf = docx.Document(file_path)
|
| 34 |
+
return ' '.join([p.text for p in docf.paragraphs])
|
| 35 |
+
elif ext == '.txt':
|
| 36 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 37 |
+
return f.read()
|
| 38 |
+
return ''
|
| 39 |
+
|
| 40 |
+
def chunk_text(text, max_len=400):
|
| 41 |
+
sents = text.split('. ')
|
| 42 |
+
chunks, chunk = [], ''
|
| 43 |
+
for s in sents:
|
| 44 |
+
if len(chunk) + len(s) < max_len:
|
| 45 |
+
chunk += s + '. '
|
| 46 |
+
else:
|
| 47 |
+
if chunk.strip():
|
| 48 |
+
chunks.append(chunk.strip())
|
| 49 |
+
chunk = s + '. '
|
| 50 |
+
if chunk.strip():
|
| 51 |
+
chunks.append(chunk.strip())
|
| 52 |
+
return chunks
|
| 53 |
+
|
| 54 |
+
all_chunks, all_meta = [], []
|
| 55 |
+
if os.path.exists(DATA_DIR) and len(os.listdir(DATA_DIR)) > 0:
|
| 56 |
+
for fname in os.listdir(DATA_DIR):
|
| 57 |
+
if not fname.lower().endswith(('.pdf', '.docx', '.txt')):
|
| 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=400)
|
| 64 |
+
for chunk in c:
|
| 65 |
+
if len(chunk) > 40:
|
| 66 |
+
all_chunks.append(chunk)
|
| 67 |
+
all_meta.append({'subject': subject, 'file': fname})
|
| 68 |
+
if len(all_chunks) >= MAX_CHUNKS:
|
| 69 |
+
break
|
| 70 |
+
if len(all_chunks) >= MAX_CHUNKS:
|
| 71 |
+
break
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Failed {file_path}: {e}")
|
| 74 |
+
if len(all_chunks) >= MAX_CHUNKS:
|
| 75 |
+
break
|
| 76 |
+
print('Chunks loaded:', len(all_chunks))
|
| 77 |
+
else:
|
| 78 |
+
print(f"DATA_DIR '{DATA_DIR}' not found or empty! Please upload your textbook files.")
|
| 79 |
+
|
| 80 |
+
###########################
|
| 81 |
+
# EMBEDDINGS & INDEX
|
| 82 |
+
###########################
|
| 83 |
+
|
| 84 |
+
if len(all_chunks) > 0:
|
| 85 |
+
encoder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 86 |
+
embeddings = encoder.encode(all_chunks, show_progress_bar=False, batch_size=16)
|
| 87 |
+
faiss.normalize_L2(embeddings)
|
| 88 |
+
index = faiss.IndexFlatIP(embeddings.shape[1])
|
| 89 |
+
index.add(np.array(embeddings).astype('float32'))
|
| 90 |
+
else:
|
| 91 |
+
encoder, embeddings, index = None, None, None
|
| 92 |
+
|
| 93 |
+
###########################
|
| 94 |
+
# ANSWER FUNCTION
|
| 95 |
+
###########################
|
| 96 |
+
|
| 97 |
+
def answer_query(
|
| 98 |
+
question,
|
| 99 |
+
subject=None,
|
| 100 |
+
top_k=2,
|
| 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)
|
| 110 |
+
found = []
|
| 111 |
+
for idx in I[0]:
|
| 112 |
+
if subject:
|
| 113 |
+
if subject.lower() in all_meta[idx]['subject'].lower():
|
| 114 |
+
found.append(all_chunks[idx])
|
| 115 |
+
else:
|
| 116 |
+
found.append(all_chunks[idx])
|
| 117 |
+
if len(found) >= top_k: break
|
| 118 |
+
context = "\n".join(found)
|
| 119 |
+
# Prompt for Qwen, non-repeating
|
| 120 |
+
prompt = (
|
| 121 |
+
"You are an ICSE board AI tutor. Answer ONLY using the textbook excerpts below. Do NOT repeat the answer or echo the prompt.\n\n"
|
| 122 |
+
f"Textbook excerpt:\n{context}\n\n"
|
| 123 |
+
f"Student question: {question}\n"
|
| 124 |
+
"Answer:"
|
| 125 |
+
)
|
| 126 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 127 |
+
pipe = pipeline(
|
| 128 |
+
"text-generation",
|
| 129 |
+
model=model_name,
|
| 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=256,
|
| 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 lines after "Answer:" before any echo,
|
| 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:
|
| 148 |
+
l = l.strip()
|
| 149 |
+
if l and l not in seen:
|
| 150 |
+
unique.append(l)
|
| 151 |
+
seen.add(l)
|
| 152 |
+
final_answer = "\n".join(unique)
|
| 153 |
+
if not final_answer.strip():
|
| 154 |
+
return "Sorry, I could not find an answer from the textbook."
|
| 155 |
+
return final_answer
|
| 156 |
+
|
| 157 |
+
###########################
|
| 158 |
+
# GRADIO APP
|
| 159 |
+
###########################
|
| 160 |
+
|
| 161 |
+
def gradio_chatbot(user_input, subject=""):
|
| 162 |
+
subject_input = subject.strip() if subject.strip() else None
|
| 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, optional=True),
|
| 173 |
+
],
|
| 174 |
+
outputs="text",
|
| 175 |
+
title=TITLE,
|
| 176 |
+
description=DESC,
|
| 177 |
+
allow_flagging="never"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
sentence-transformers
|
| 4 |
+
faiss-cpu
|
| 5 |
+
PyPDF2
|
| 6 |
+
python-docx
|
| 7 |
+
gradio
|