Download app.py from anomaly05/project: direct link, hf CLI and curl.
- Browser
- Download file 6.38 kB
-
https://huggingface.co/spaces/anomaly05/project/resolve/main/app.py
- Command line
-
hf download hf://spaces/anomaly05/project/app.py
-
curl -L -o app.py https://huggingface.co/spaces/anomaly05/project/resolve/main/app.py
6.38 kB
| import os | |
| import numpy as np | |
| from PyPDF2 import PdfReader | |
| import docx | |
| import faiss | |
| import torch | |
| from sentence_transformers import SentenceTransformer | |
| from transformers import pipeline, AutoTokenizer | |
| import gradio as gr | |
| ########################### | |
| # CONFIGURATION | |
| ########################### | |
| DATA_DIR = "." # Looks in the Space/project root | |
| MAX_CHUNKS = 10 # Conservative for Spaces, raises as needed | |
| MAX_RESPONSE_CHARS = 4000 # Prevents protocol errors by truncating huge responses | |
| ########################### | |
| # DATA EXTRACTION & CHUNKING | |
| ########################### | |
| def extract_text(file_path): | |
| ext = os.path.splitext(file_path)[1].lower() | |
| if ext == '.pdf': | |
| text = '' | |
| with open(file_path, 'rb') as f: | |
| reader = PdfReader(f) | |
| for page in reader.pages: | |
| t = page.extract_text() | |
| if t: text += t + ' ' | |
| return text | |
| elif ext == '.docx': | |
| docf = docx.Document(file_path) | |
| return ' '.join([p.text for p in docf.paragraphs]) | |
| elif ext == '.txt': | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| return '' | |
| def chunk_text(text, max_len=300): | |
| # Smaller chunks are safer for Spaces | |
| sents = text.split('. ') | |
| chunks, chunk = [], '' | |
| for s in sents: | |
| if len(chunk) + len(s) < max_len: | |
| chunk += s + '. ' | |
| else: | |
| if chunk.strip(): | |
| chunks.append(chunk.strip()) | |
| chunk = s + '. ' | |
| if chunk.strip(): | |
| chunks.append(chunk.strip()) | |
| return chunks | |
| all_chunks, all_meta = [], [] | |
| filelist = [f for f in os.listdir(DATA_DIR) if f.lower().endswith(('.pdf', '.docx', '.txt'))] | |
| if filelist: | |
| for fname in filelist: | |
| file_path = os.path.join(DATA_DIR, fname) | |
| subject = 'general' | |
| try: | |
| text = extract_text(file_path) | |
| c = chunk_text(text, max_len=300) | |
| for chunk in c: | |
| # Only index nonempty, reasonbly sized chunks | |
| if 40 < len(chunk) <= 1000: | |
| all_chunks.append(chunk) | |
| all_meta.append({'subject': subject, 'file': fname}) | |
| if len(all_chunks) >= MAX_CHUNKS: | |
| break | |
| if len(all_chunks) >= MAX_CHUNKS: | |
| break | |
| except Exception as e: | |
| print(f"Failed {file_path}: {e}") | |
| if len(all_chunks) >= MAX_CHUNKS: | |
| break | |
| print('Chunks loaded:', len(all_chunks)) | |
| else: | |
| print("No textbook files found in current directory.") | |
| ########################### | |
| # EMBEDDINGS & INDEX | |
| ########################### | |
| if all_chunks: | |
| encoder = SentenceTransformer('all-MiniLM-L6-v2') | |
| embeddings = encoder.encode(all_chunks, show_progress_bar=False, batch_size=8) | |
| faiss.normalize_L2(embeddings) | |
| index = faiss.IndexFlatIP(embeddings.shape[1]) | |
| index.add(np.array(embeddings).astype('float32')) | |
| else: | |
| encoder, embeddings, index = None, None, None | |
| ########################### | |
| # ANSWER FUNCTION | |
| ########################### | |
| def answer_query( | |
| question, | |
| subject=None, | |
| top_k=1, # One chunk per answer is safest! | |
| model_name="Qwen/Qwen2-0.5B" | |
| ): | |
| if encoder is None or index is None or not all_chunks: | |
| return "⚠️ No textbooks found or processed. Please check your file uploads." | |
| # Retrieval, only the most relevant chunk | |
| q_emb = encoder.encode([question]) | |
| faiss.normalize_L2(q_emb) | |
| D, I = index.search(np.asarray(q_emb, dtype='float32'), top_k*2) | |
| found = [] | |
| for idx in I[0]: | |
| if subject: | |
| if subject.lower() in all_meta[idx]['subject'].lower(): | |
| found.append(all_chunks[idx]) | |
| else: | |
| found.append(all_chunks[idx]) | |
| if len(found) >= top_k: break | |
| context = "\n".join(found) | |
| if not context.strip(): | |
| return "Sorry, I could not find an answer from the loaded textbooks." | |
| prompt = ( | |
| "You are an ICSE board AI tutor. Answer using ONLY the following textbook excerpt. " | |
| "Answer concisely and do not repeat the prompt or content.\n\n" | |
| f"Textbook excerpt:\n{context}\n\n" | |
| f"Student question: {question}\n" | |
| "Answer:" | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| pipe = pipeline( | |
| "text-generation", | |
| model=model_name, | |
| tokenizer=tokenizer, | |
| torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
| device=0 if torch.cuda.is_available() else -1, | |
| max_new_tokens=128, | |
| do_sample=False, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| output = pipe(prompt)[0]['generated_text'] | |
| # Clean output: only what's after the last "Answer:" line | |
| answer_part = output.split("Answer:")[-1].strip() | |
| # Truncate at first re-ask or echo marker | |
| for stop_word in ["Textbook", "Student question:", "Context:"]: | |
| if stop_word in answer_part: | |
| answer_part = answer_part.split(stop_word)[0].strip() | |
| # Remove duplicate lines and enforce length limit | |
| lines = answer_part.splitlines() | |
| seen, unique = set(), [] | |
| for l in lines: | |
| l = l.strip() | |
| if l and l not in seen: | |
| unique.append(l) | |
| seen.add(l) | |
| final_answer = "\n".join(unique) | |
| if not final_answer.strip(): | |
| return "Sorry, I could not generate an answer from the content." | |
| if len(final_answer) > MAX_RESPONSE_CHARS: | |
| final_answer = final_answer[:MAX_RESPONSE_CHARS] + "\n\n[Truncated]" | |
| return final_answer | |
| ########################### | |
| # GRADIO APP | |
| ########################### | |
| def gradio_chatbot(user_input, subject=""): | |
| subject_input = subject.strip() if subject.strip() else None | |
| return answer_query(user_input, subject=subject_input) | |
| TITLE = "ICSE Textbook Chatbot (Qwen2-0.5B)" | |
| DESC = "Ask any question using ONLY your uploaded ICSE textbooks. The AI answers strictly from your syllabus content. For demo use only." | |
| iface = gr.Interface( | |
| fn=gradio_chatbot, | |
| inputs=[ | |
| gr.Textbox(label="Your question", placeholder="Type your question here...", lines=2), | |
| gr.Textbox(label="Subject (optional)", placeholder="e.g., Physics, Geography", lines=1) | |
| ], | |
| outputs="text", | |
| title=TITLE, | |
| description=DESC, | |
| allow_flagging="never" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(share=True) |