addyjeddy commited on
Commit
c70f2a9
·
verified ·
1 Parent(s): e2fd1c4

Add main application files

Browse files
Files changed (4) hide show
  1. README.md +57 -6
  2. app.py +99 -0
  3. gitignore +5 -0
  4. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,63 @@
1
  ---
2
- title: Book Chatbot Better
3
- emoji: 🐠
4
- colorFrom: green
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 6.3.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: NCTB Textbook Q&A Assistant
3
+ emoji: 📚
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 5.9.1
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
+ # 🎓 NCTB Textbook Q&A Assistant
14
+
15
+ An intelligent question-answering system for NCTB Class 11-12 textbooks powered by RAG (Retrieval Augmented Generation).
16
+
17
+ ## 📖 Available Books
18
+ - **English** (Class 11-12)
19
+ - **Bangla** (বাংলা বই)
20
+
21
+ ## ✨ Features
22
+ - ✅ Answers strictly from textbook content
23
+ - ✅ Retrieves relevant passages from the books
24
+ - ✅ Supports both English and Bangla
25
+ - ✅ Educational and contextually accurate responses
26
+ - ✅ Powered by Llama 3.1 via Groq
27
+
28
+ ## 🚀 Setup Instructions
29
+
30
+ ### For Hugging Face Spaces:
31
+ 1. Upload all files to your Space
32
+ 2. Go to **Settings** → **Repository Secrets**
33
+ 3. Add a new secret:
34
+ - **Name:** `GROQ_API_KEY`
35
+ - **Value:** Your Groq API key (get free at https://console.groq.com/)
36
+ 4. Save and wait for the Space to build (~2-3 minutes)
37
+
38
+ ## 🔧 Technology Stack
39
+ - **Framework:** Gradio
40
+ - **Embeddings:** sentence-transformers (all-MiniLM-L6-v2, bangla-bert-base)
41
+ - **Vector Store:** FAISS
42
+ - **LLM:** Llama 3.1 8B (via Groq)
43
+ - **RAG Chain:** LangChain
44
+
45
+ ## 📝 Example Questions
46
+
47
+ **English:**
48
+ - What is literature?
49
+ - What are the elements of a short story?
50
+ - Explain the theme of the poem
51
+
52
+ **Bangla:**
53
+ - সাহিত্য কী?
54
+ - কবিতার বৈশিষ্ট্য কী?
55
+ - গল্পের উপাদান কী কী?
56
+
57
+ ## ⚠️ Important Notes
58
+ - The system only answers from textbook content
59
+ - If information is not in the book, it will say so
60
+ - Responses are educational and curriculum-aligned
61
+
62
+ ## 📜 License
63
+ MIT License - Free to use for educational purposes
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+ from langchain_groq import ChatGroq
6
+
7
+ print("Loading English index...")
8
+ english_embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
9
+ english_db = FAISS.load_local("nctb_english_index", english_embeddings, allow_dangerous_deserialization=True)
10
+
11
+ print("Loading Bangla index...")
12
+ bangla_embeddings = HuggingFaceEmbeddings(model_name="sagorsarker/bangla-bert-base")
13
+ bangla_db = FAISS.load_local("nctb_bangla_index", bangla_embeddings, allow_dangerous_deserialization=True)
14
+
15
+ llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, api_key=os.getenv("GROQ_API_KEY"))
16
+
17
+ def chat(question, book_choice):
18
+ if book_choice == "English (Class 11-12)":
19
+ db = english_db
20
+ system_instruction = """You are an expert teacher assistant for NCTB Class 11-12 English textbook.
21
+
22
+ STRICT RULES:
23
+ 1. Answer ONLY using information from the provided context
24
+ 2. If the answer is not in the context, say "I couldn't find this information in the NCTB textbook."
25
+ 3. Quote relevant parts from the book when possible
26
+ 4. Keep answers clear and educational
27
+ 5. Do NOT use external knowledge - stick to the textbook content only"""
28
+
29
+ else:
30
+ db = bangla_db
31
+ system_instruction = """তুমি একজন দক্ষ শিক্ষক সহায়ক যিনি এনসিটিবি বাংলা পাঠ্যপুস্তক থেকে প্রশ্নের উত্তর দাও।
32
+
33
+ কঠোর নিয়ম:
34
+ 1. শুধুমাত্র প্রদত্ত প্রসঙ্গ (context) থেকে উত্তর দিতে হবে
35
+ 2. বইয়ের বাইরে কোনো তথ্য ব্যবহার করা যাবে না
36
+ 3. প্রাসঙ্গিক অংশ উদ্ধৃত করো
37
+ 4. উত্তর স্পষ্ট ও শিক্ষামূলক হতে হবে
38
+ 5. যদি উত্তর প্রসঙ্গে না থাকে, বলো "এই তথ্য পাঠ্যপুস্তকে পাওয়া যায়নি।" """
39
+
40
+ # Retrieve relevant passages
41
+ retriever = db.as_retriever(search_kwargs={"k": 5})
42
+ docs = retriever.invoke(question)
43
+
44
+ if not docs:
45
+ return "No relevant information found in the textbook."
46
+
47
+ context = "\n\n---\n\n".join([f"[Passage {i+1}]\n{doc.page_content}" for i, doc in enumerate(docs)])
48
+
49
+ prompt = f"""{system_instruction}
50
+
51
+ Context from NCTB Textbook:
52
+ {context}
53
+
54
+ Student Question: {question}
55
+
56
+ Answer (based strictly on the textbook):"""
57
+
58
+ response = llm.invoke(prompt)
59
+
60
+ if hasattr(response, 'content'):
61
+ return response.content
62
+ elif isinstance(response, str):
63
+ return response
64
+ else:
65
+ return str(response)
66
+
67
+ # Enhanced Gradio interface
68
+ iface = gr.Interface(
69
+ fn=chat,
70
+ inputs=[
71
+ gr.Textbox(
72
+ label="📝 Your Question",
73
+ placeholder="Example: What is the definition of literature? / সাহিত্য কী?",
74
+ lines=3
75
+ ),
76
+ gr.Dropdown(
77
+ choices=["English (Class 11-12)", "Bangla (বাংলা বই)"],
78
+ value="English (Class 11-12)",
79
+ label="📚 Select Textbook"
80
+ )
81
+ ],
82
+ outputs=gr.Textbox(label="💡 Answer from Textbook", lines=10),
83
+ title="🎓 NCTB Textbook Q&A Assistant",
84
+ description="""Ask questions from NCTB Class 11-12 textbooks and get answers directly from the book content.
85
+
86
+ **Features:**
87
+ - ✅ Answers strictly from textbook material
88
+ - ✅ Supports both English and Bangla books
89
+ - ✅ Educational and accurate responses""",
90
+ examples=[
91
+ ["What is literature?", "English (Class 11-12)"],
92
+ ["What are the elements of a short story?", "English (Class 11-12)"],
93
+ ["সাহিত্য কী?", "Bangla (বাংলা বই)"],
94
+ ["কবিতার বৈশিষ্ট্য কী?", "Bangla (বাংলা বই)"]
95
+ ],
96
+ theme="soft"
97
+ )
98
+
99
+ iface.launch()
gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.py[cod]
3
+ .env
4
+ *.log
5
+ .DS_Store
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain
2
+ langchain-community
3
+ langchain-groq
4
+ sentence-transformers
5
+ faiss-cpu
6
+ gradio