Sazid2 commited on
Commit
018deef
·
verified ·
1 Parent(s): 5246c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +220 -34
app.py CHANGED
@@ -1,39 +1,225 @@
1
- """
2
- Jajabor - Absolute Minimal Working Version
3
- """
 
 
 
 
 
 
 
 
 
 
4
 
5
- import gradio as gr
 
 
 
 
 
6
 
7
- def chat_fn(message, history):
8
- if history is None:
9
- history = []
10
-
11
- if not message.strip():
12
- history.append(("", "অনুগ্ৰহ কৰি প্ৰশ্ন লিখক।"))
13
- return history
14
-
15
- # Simple echo with Assamese response
16
- response = f"ধন্যবাদ! আপোনাৰ প্ৰশ্ন: '{message}'\n\nমই জাজাবৰ, আপোনাৰ SEBA Class 10 AI টিউটাৰ। PDF আপলোড কৰি RAG চিস্টেম সক্ৰিয় কৰক।"
17
- history.append((message, response))
18
- return history
19
 
20
- # Minimal interface
21
- with gr.Blocks() as demo:
22
- gr.Markdown("# 🧭 জাজাবৰ - SEBA Class 10 Tutor")
23
- chatbot = gr.Chatbot()
24
- msg = gr.Textbox(label="প্ৰশ্ন লিখক")
25
- clear = gr.Button("পৰিষ্কাৰ কৰক")
26
-
27
- def user(user_message, history):
28
- return "", history + [[user_message, None]]
29
-
30
- def bot(history):
31
- response = "জাজাবৰই আপোনাক সহায় কৰিবলৈ সাজু! PDF পাঠ্যপুথি আপলোড কৰি সম্পূৰ্ণ RAG চিস্টেম সক্ৰিয় কৰক।"
32
- history[-1][1] = response
33
- return history
34
-
35
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
36
- clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  if __name__ == "__main__":
39
- demo.launch(share=False)
 
1
+ import streamlit as st
2
+ import os
3
+ import sqlite3
4
+ from datetime import datetime
5
+ import PyPDF2
6
+ from sentence_transformers import SentenceTransformer
7
+ import faiss
8
+ import numpy as np
9
+ from transformers import pipeline
10
+ import pytesseract
11
+ from PIL import Image
12
+ import sympy as sp
13
+ import io
14
 
15
+ # App configuration
16
+ st.set_page_config(
17
+ page_title="Jajabor – SEBA Class 10 Tutor",
18
+ page_icon="🧭",
19
+ layout="wide"
20
+ )
21
 
22
+ # Initialize session state
23
+ if 'chat_history' not in st.session_state:
24
+ st.session_state.chat_history = []
25
+ if 'username' not in st.session_state:
26
+ st.session_state.username = ""
27
+ if 'tutor' not in st.session_state:
28
+ st.session_state.tutor = None
 
 
 
 
 
29
 
30
+ class SimpleTutor:
31
+ def __init__(self):
32
+ self.llm = None
33
+ self.embedding_model = None
34
+ self.index = None
35
+ self.corpus_chunks = []
36
+ self._load_models()
37
+ self.load_pdfs()
38
+
39
+ def _load_models(self):
40
+ try:
41
+ self.embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
42
+ self.llm = pipeline("text2text-generation", model="google/flan-t5-small", device=-1)
43
+ except Exception as e:
44
+ st.error(f"Model loading error: {e}")
45
+
46
+ def load_pdfs(self):
47
+ pdf_dir = "pdfs/class10"
48
+ if not os.path.exists(pdf_dir):
49
+ return
50
+
51
+ all_texts = []
52
+ for fname in os.listdir(pdf_dir):
53
+ if fname.lower().endswith('.pdf'):
54
+ path = os.path.join(pdf_dir, fname)
55
+ try:
56
+ reader = PyPDF2.PdfReader(path)
57
+ text = ""
58
+ for page in reader.pages:
59
+ text += page.extract_text() or ""
60
+ if text.strip():
61
+ all_texts.append(text)
62
+ except Exception as e:
63
+ st.error(f"Error reading {fname}: {e}")
64
+
65
+ self.corpus_chunks = []
66
+ for text in all_texts:
67
+ chunks = self._split_text(text)
68
+ self.corpus_chunks.extend(chunks)
69
+
70
+ if self.corpus_chunks and self.embedding_model:
71
+ try:
72
+ embs = self.embedding_model.encode(self.corpus_chunks).astype("float32")
73
+ dim = embs.shape[1]
74
+ self.index = faiss.IndexFlatL2(dim)
75
+ self.index.add(embs)
76
+ except Exception as e:
77
+ st.error(f"FAISS error: {e}")
78
+
79
+ def _split_text(self, text, chunk_size=400):
80
+ if not text:
81
+ return []
82
+ return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size) if text[i:i+chunk_size].strip()]
83
+
84
+ def answer_question(self, question):
85
+ if not question.strip():
86
+ return "অনুগ্ৰহ কৰি এটা প্ৰশ্ন সোধক।"
87
+
88
+ if self._is_math_question(question):
89
+ return self._solve_math(question)
90
+
91
+ context = ""
92
+ if self.index and self.corpus_chunks:
93
+ relevant_chunks = self._find_relevant_chunks(question)
94
+ if relevant_chunks:
95
+ context = "\n".join(relevant_chunks[:2])
96
+
97
+ if self.llm:
98
+ try:
99
+ prompt = f"প্ৰশ্ন: {question}\n\nসংদৰ্ভ: {context}\n\nসহায়ক উত্তৰ:" if context else f"প্ৰশ্ন: {question}\n\nউত্তৰ:"
100
+ response = self.llm(prompt, max_new_tokens=150, temperature=0.3)
101
+ return response[0]['generated_text']
102
+ except Exception as e:
103
+ return f"উত্তৰ তৈয়াৰ কৰোঁতে সমস্যা: {e}"
104
+ else:
105
+ return "মই আপোনাৰ প্ৰশ্নটো বুজিলোঁ। অধ্যয়নৰ বাবে শুভেচ্ছা!"
106
+
107
+ def _is_math_question(self, text):
108
+ math_indicators = ['+', '-', '*', '/', '=', 'x', 'y', 'গণিত', 'সমীকৰণ']
109
+ return any(indicator in text.lower() for indicator in math_indicators)
110
+
111
+ def _solve_math(self, expr):
112
+ try:
113
+ expr = expr.strip().replace('^', '**')
114
+ if '=' in expr:
115
+ parts = expr.split('=')
116
+ if len(parts) == 2:
117
+ left = sp.sympify(parts[0].strip())
118
+ right = sp.sympify(parts[1].strip())
119
+ equation = sp.Eq(left, right)
120
+ solutions = sp.solve(equation)
121
+ if solutions:
122
+ return f"সমীকৰণ: {equation}\n\nসমাধান: {solutions}"
123
+ else:
124
+ expr_sym = sp.sympify(expr)
125
+ simplified = sp.simplify(expr_sym)
126
+ return f"প্ৰকাশ: {expr}\n\nসৰলীকৃত: {simplified}"
127
+ except Exception as e:
128
+ return f"গণিত সমাধানত সমস্যা: {e}"
129
+
130
+ def _find_relevant_chunks(self, question, k=3):
131
+ if not self.corpus_chunks:
132
+ return []
133
+
134
+ if self.index and self.embedding_model:
135
+ try:
136
+ q_vec = self.embedding_model.encode([question]).astype("float32")
137
+ D, I = self.index.search(q_vec, k)
138
+ return [self.corpus_chunks[i] for i in I[0] if 0 <= i < len(self.corpus_chunks)]
139
+ except Exception:
140
+ pass
141
+ return []
142
+
143
+ def extract_text_from_image(uploaded_file):
144
+ try:
145
+ image = Image.open(uploaded_file)
146
+ text = pytesseract.image_to_string(image)
147
+ return text.strip()
148
+ except Exception as e:
149
+ return ""
150
+
151
+ # Main app
152
+ def main():
153
+ st.title("🧭 জাজাবৰ – SEBA Class 10 AI Tutor")
154
+
155
+ # Sidebar
156
+ with st.sidebar:
157
+ st.header("👤 লগিন")
158
+ username = st.text_input("আপোনাৰ নাম", value=st.session_state.username)
159
+ if username and username != st.session_state.username:
160
+ st.session_state.username = username
161
+ st.success(f"লগিন successful: {username}")
162
+
163
+ st.header("📷 ছবিৰ পৰা পাঠ")
164
+ uploaded_image = st.file_uploader("ছবি আপলোড কৰক", type=['png', 'jpg', 'jpeg'])
165
+
166
+ st.header("💡 টিপছ")
167
+ st.info("""
168
+ - নাম লিখি প্ৰশ্ন সোধক
169
+ - ছবি আপলোড কৰিলে OCR ৰ সহায়ত পাঠ পঢ়িব
170
+ - বিষয়সমূহ:
171
+ - অসমীয়া
172
+ - ইংৰাজী
173
+ - গণিত
174
+ - বিজ্ঞান
175
+ - সামাজিক বিজ্ঞান
176
+ """)
177
+
178
+ # Initialize tutor
179
+ if st.session_state.tutor is None:
180
+ with st.spinner('জাজাবৰক সাজু কৰি থকা হৈছে...'):
181
+ st.session_state.tutor = SimpleTutor()
182
+
183
+ # Main chat area
184
+ st.header("💬 জাজাবৰৰ সৈতে কথোপকথন")
185
+
186
+ # Display chat history
187
+ for i, (question, answer) in enumerate(st.session_state.chat_history):
188
+ with st.chat_message("user"):
189
+ st.write(question)
190
+ with st.chat_message("assistant"):
191
+ st.write(answer)
192
+
193
+ # Chat input
194
+ if prompt := st.chat_input("আপোনাৰ প্ৰশ্ন ইয়াত লিখক..."):
195
+ if not st.session_state.username:
196
+ st.error("⚠️ প্ৰথমে আপোনাৰ নাম লিখক")
197
+ st.stop()
198
+
199
+ # Add user message to chat
200
+ st.session_state.chat_history.append((prompt, ""))
201
+
202
+ # Process OCR if image uploaded
203
+ full_question = prompt
204
+ if uploaded_image:
205
+ ocr_text = extract_text_from_image(uploaded_image)
206
+ if ocr_text:
207
+ full_question += f"\n[ছবিৰ পাঠ: {ocr_text}]"
208
+
209
+ # Get AI response
210
+ with st.spinner('জাজাবৰে চিন্তা কৰি আছে...'):
211
+ response = st.session_state.tutor.answer_question(full_question)
212
+
213
+ # Update chat history
214
+ st.session_state.chat_history[-1] = (prompt, response)
215
+
216
+ # Rerun to update display
217
+ st.rerun()
218
+
219
+ # Clear chat button
220
+ if st.button("🧹 কথোপকথন পৰিষ্কাৰ কৰক"):
221
+ st.session_state.chat_history = []
222
+ st.rerun()
223
 
224
  if __name__ == "__main__":
225
+ main()