Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
Jajabor – SEBA Assamese Class 10 Tutor
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
|
@@ -134,10 +135,11 @@ class SimpleTutor:
|
|
| 134 |
self.embedding_model = None
|
| 135 |
self.index = None
|
| 136 |
self.corpus_chunks = []
|
| 137 |
-
self.loaded = False
|
| 138 |
|
|
|
|
| 139 |
self._load_models()
|
| 140 |
self.load_pdfs()
|
|
|
|
| 141 |
|
| 142 |
def _load_models(self):
|
| 143 |
"""Load models with error handling"""
|
|
@@ -153,35 +155,41 @@ class SimpleTutor:
|
|
| 153 |
self.llm = pipeline(
|
| 154 |
"text2text-generation",
|
| 155 |
model="google/flan-t5-small",
|
| 156 |
-
device=-1
|
| 157 |
-
torch_dtype="auto"
|
| 158 |
)
|
| 159 |
print("✅ LLM loaded")
|
| 160 |
except Exception as e:
|
| 161 |
print(f"❌ Could not load LLM: {e}")
|
| 162 |
-
|
| 163 |
-
self.loaded = True
|
| 164 |
|
| 165 |
def load_pdfs(self):
|
| 166 |
"""Simple PDF loading"""
|
| 167 |
-
if not PDF_AVAILABLE
|
| 168 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
return
|
| 170 |
|
| 171 |
all_texts = []
|
| 172 |
-
for
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# Simple text splitting
|
| 187 |
self.corpus_chunks = []
|
|
@@ -189,11 +197,12 @@ class SimpleTutor:
|
|
| 189 |
chunks = self._split_text(text)
|
| 190 |
self.corpus_chunks.extend(chunks)
|
| 191 |
|
| 192 |
-
print(f"📚 Total chunks: {len(self.corpus_chunks)}")
|
| 193 |
|
| 194 |
# Build FAISS index if we have chunks and embedding model
|
| 195 |
if self.corpus_chunks and self.embedding_model and FAISS_AVAILABLE:
|
| 196 |
try:
|
|
|
|
| 197 |
embs = self.embedding_model.encode(self.corpus_chunks, show_progress_bar=False).astype("float32")
|
| 198 |
dim = embs.shape[1]
|
| 199 |
self.index = faiss.IndexFlatL2(dim)
|
|
@@ -245,10 +254,7 @@ class SimpleTutor:
|
|
| 245 |
)
|
| 246 |
|
| 247 |
if isinstance(response, list) and len(response) > 0:
|
| 248 |
-
|
| 249 |
-
answer = response[0].get('generated_text', 'উত্তৰ তৈয়াৰ কৰিব পৰা নগল।')
|
| 250 |
-
else:
|
| 251 |
-
answer = str(response[0])
|
| 252 |
else:
|
| 253 |
answer = str(response)
|
| 254 |
|
|
@@ -353,21 +359,24 @@ def extract_text_from_image(image_path):
|
|
| 353 |
print(f"OCR error: {e}")
|
| 354 |
return ""
|
| 355 |
|
| 356 |
-
# -------------------- GRADIO APP --------------------
|
| 357 |
-
def
|
| 358 |
-
"""
|
| 359 |
|
| 360 |
# Initialize components
|
|
|
|
| 361 |
init_db()
|
| 362 |
tutor = SimpleTutor()
|
| 363 |
|
| 364 |
-
# Store user state in
|
| 365 |
user_states = {}
|
| 366 |
|
| 367 |
def get_user_state(username):
|
| 368 |
"""Simple user state management"""
|
|
|
|
| 369 |
if not username:
|
| 370 |
return None
|
|
|
|
| 371 |
if username not in user_states:
|
| 372 |
user_id = get_or_create_user(username)
|
| 373 |
if user_id:
|
|
@@ -383,14 +392,14 @@ def main():
|
|
| 383 |
chat_history = []
|
| 384 |
|
| 385 |
# Check if user is logged in
|
| 386 |
-
user_state = get_user_state(username
|
| 387 |
if not user_state:
|
| 388 |
-
new_history = chat_history + [[message, "⚠️ প্ৰথমে নাম লিখি লগিন কৰক।"]]
|
| 389 |
return new_history, ""
|
| 390 |
|
| 391 |
# Combine text and image input
|
| 392 |
-
full_question = message.strip()
|
| 393 |
-
if image:
|
| 394 |
ocr_text = extract_text_from_image(image)
|
| 395 |
if ocr_text:
|
| 396 |
full_question += f"\n[ছবিৰ পাঠ: {ocr_text}]"
|
|
@@ -406,116 +415,101 @@ def main():
|
|
| 406 |
log_interaction(user_state["user_id"], full_question, answer)
|
| 407 |
|
| 408 |
# Update chat
|
| 409 |
-
display_question = message if message.strip() else "[ছবিৰ প্ৰশ্ন]"
|
| 410 |
new_history = chat_history + [[display_question, answer]]
|
| 411 |
return new_history, ""
|
| 412 |
|
| 413 |
def clear_chat():
|
| 414 |
"""Clear chat history"""
|
| 415 |
-
return [],
|
| 416 |
|
| 417 |
-
# Create Gradio interface
|
| 418 |
with gr.Blocks(
|
| 419 |
title=APP_NAME,
|
| 420 |
-
theme=gr.themes.Soft()
|
| 421 |
-
css="""
|
| 422 |
-
.container {
|
| 423 |
-
max-width: 1200px;
|
| 424 |
-
margin: auto;
|
| 425 |
-
padding: 20px;
|
| 426 |
-
}
|
| 427 |
-
.login-section {
|
| 428 |
-
background: #f8f9fa;
|
| 429 |
-
padding: 15px;
|
| 430 |
-
border-radius: 10px;
|
| 431 |
-
margin-bottom: 20px;
|
| 432 |
-
}
|
| 433 |
-
"""
|
| 434 |
) as demo:
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
-
with gr.
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
label="আপোনাৰ নাম",
|
| 446 |
-
placeholder="আপোনাৰ নাম লিখক...",
|
| 447 |
-
max_lines=1
|
| 448 |
-
)
|
| 449 |
-
gr.Markdown("""
|
| 450 |
-
### 💡 টিপছ
|
| 451 |
-
- নাম লিখি প্ৰশ্ন সোধক
|
| 452 |
-
- পাঠ্যপুথিৰ PDF ফাইলসমূহ `pdfs/class10` ফ'ল্ডাৰত ৰাখক
|
| 453 |
-
- ছবি আপলোড কৰিলে OCR ৰ সহায়ত পাঠ পঢ়িব
|
| 454 |
-
""")
|
| 455 |
|
| 456 |
-
with gr.
|
| 457 |
-
|
| 458 |
-
label="
|
| 459 |
-
|
| 460 |
-
|
| 461 |
)
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
fn=chat_function,
|
| 491 |
-
inputs=[message, image, chatbot, username],
|
| 492 |
-
outputs=[chatbot, message]
|
| 493 |
-
)
|
| 494 |
-
|
| 495 |
-
clear_btn.click(
|
| 496 |
-
fn=clear_chat,
|
| 497 |
-
outputs=[chatbot, message]
|
| 498 |
-
)
|
| 499 |
|
| 500 |
return demo
|
| 501 |
|
| 502 |
-
# --------------------
|
| 503 |
if __name__ == "__main__":
|
| 504 |
if not GRADIO_AVAILABLE:
|
| 505 |
-
print("Gradio not available. Please install gradio.")
|
| 506 |
exit(1)
|
| 507 |
|
| 508 |
-
demo = main()
|
| 509 |
-
|
| 510 |
-
# For Hugging Face Spaces, use share=False and don't specify server_name
|
| 511 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
demo.launch(
|
| 513 |
-
server_name="0.0.0.0"
|
| 514 |
server_port=7860,
|
| 515 |
-
share=False
|
| 516 |
-
show_error=True
|
| 517 |
)
|
| 518 |
except Exception as e:
|
| 519 |
-
print(f"
|
| 520 |
-
#
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
Jajabor – SEBA Assamese Class 10 Tutor
|
| 3 |
+
Simplified version for Hugging Face Spaces
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 135 |
self.embedding_model = None
|
| 136 |
self.index = None
|
| 137 |
self.corpus_chunks = []
|
|
|
|
| 138 |
|
| 139 |
+
print("🔄 Loading models...")
|
| 140 |
self._load_models()
|
| 141 |
self.load_pdfs()
|
| 142 |
+
print("✅ Tutor initialized")
|
| 143 |
|
| 144 |
def _load_models(self):
|
| 145 |
"""Load models with error handling"""
|
|
|
|
| 155 |
self.llm = pipeline(
|
| 156 |
"text2text-generation",
|
| 157 |
model="google/flan-t5-small",
|
| 158 |
+
device=-1
|
|
|
|
| 159 |
)
|
| 160 |
print("✅ LLM loaded")
|
| 161 |
except Exception as e:
|
| 162 |
print(f"❌ Could not load LLM: {e}")
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def load_pdfs(self):
|
| 165 |
"""Simple PDF loading"""
|
| 166 |
+
if not PDF_AVAILABLE:
|
| 167 |
+
print("📚 PDF reading not available")
|
| 168 |
+
return
|
| 169 |
+
|
| 170 |
+
if not os.path.exists(PDF_DIR):
|
| 171 |
+
print(f"📁 PDF directory not found: {PDF_DIR}")
|
| 172 |
return
|
| 173 |
|
| 174 |
all_texts = []
|
| 175 |
+
pdf_files = [f for f in os.listdir(PDF_DIR) if f.lower().endswith('.pdf')]
|
| 176 |
+
|
| 177 |
+
if not pdf_files:
|
| 178 |
+
print("📭 No PDF files found in directory")
|
| 179 |
+
return
|
| 180 |
+
|
| 181 |
+
for fname in pdf_files:
|
| 182 |
+
path = os.path.join(PDF_DIR, fname)
|
| 183 |
+
try:
|
| 184 |
+
reader = PdfReader(path)
|
| 185 |
+
text = ""
|
| 186 |
+
for page in reader.pages:
|
| 187 |
+
text += page.extract_text() or ""
|
| 188 |
+
if text.strip():
|
| 189 |
+
all_texts.append(text)
|
| 190 |
+
print(f"📖 Loaded {fname}")
|
| 191 |
+
except Exception as e:
|
| 192 |
+
print(f"❌ Error reading {fname}: {e}")
|
| 193 |
|
| 194 |
# Simple text splitting
|
| 195 |
self.corpus_chunks = []
|
|
|
|
| 197 |
chunks = self._split_text(text)
|
| 198 |
self.corpus_chunks.extend(chunks)
|
| 199 |
|
| 200 |
+
print(f"📚 Total text chunks: {len(self.corpus_chunks)}")
|
| 201 |
|
| 202 |
# Build FAISS index if we have chunks and embedding model
|
| 203 |
if self.corpus_chunks and self.embedding_model and FAISS_AVAILABLE:
|
| 204 |
try:
|
| 205 |
+
print("🔨 Building FAISS index...")
|
| 206 |
embs = self.embedding_model.encode(self.corpus_chunks, show_progress_bar=False).astype("float32")
|
| 207 |
dim = embs.shape[1]
|
| 208 |
self.index = faiss.IndexFlatL2(dim)
|
|
|
|
| 254 |
)
|
| 255 |
|
| 256 |
if isinstance(response, list) and len(response) > 0:
|
| 257 |
+
answer = response[0].get('generated_text', 'উত্তৰ তৈয়াৰ কৰিব পৰা নগল।')
|
|
|
|
|
|
|
|
|
|
| 258 |
else:
|
| 259 |
answer = str(response)
|
| 260 |
|
|
|
|
| 359 |
print(f"OCR error: {e}")
|
| 360 |
return ""
|
| 361 |
|
| 362 |
+
# -------------------- SIMPLE GRADIO APP --------------------
|
| 363 |
+
def create_app():
|
| 364 |
+
"""Create and return the Gradio app"""
|
| 365 |
|
| 366 |
# Initialize components
|
| 367 |
+
print("🚀 Starting application...")
|
| 368 |
init_db()
|
| 369 |
tutor = SimpleTutor()
|
| 370 |
|
| 371 |
+
# Store user state in memory (simple approach)
|
| 372 |
user_states = {}
|
| 373 |
|
| 374 |
def get_user_state(username):
|
| 375 |
"""Simple user state management"""
|
| 376 |
+
username = (username or "").strip()
|
| 377 |
if not username:
|
| 378 |
return None
|
| 379 |
+
|
| 380 |
if username not in user_states:
|
| 381 |
user_id = get_or_create_user(username)
|
| 382 |
if user_id:
|
|
|
|
| 392 |
chat_history = []
|
| 393 |
|
| 394 |
# Check if user is logged in
|
| 395 |
+
user_state = get_user_state(username)
|
| 396 |
if not user_state:
|
| 397 |
+
new_history = chat_history + [[message or "", "⚠️ প্ৰথমে নাম লিখি লগিন কৰক।"]]
|
| 398 |
return new_history, ""
|
| 399 |
|
| 400 |
# Combine text and image input
|
| 401 |
+
full_question = (message or "").strip()
|
| 402 |
+
if image is not None:
|
| 403 |
ocr_text = extract_text_from_image(image)
|
| 404 |
if ocr_text:
|
| 405 |
full_question += f"\n[ছবিৰ পাঠ: {ocr_text}]"
|
|
|
|
| 415 |
log_interaction(user_state["user_id"], full_question, answer)
|
| 416 |
|
| 417 |
# Update chat
|
| 418 |
+
display_question = message if message and message.strip() else "[ছবিৰ প্ৰশ্ন]"
|
| 419 |
new_history = chat_history + [[display_question, answer]]
|
| 420 |
return new_history, ""
|
| 421 |
|
| 422 |
def clear_chat():
|
| 423 |
"""Clear chat history"""
|
| 424 |
+
return [], None
|
| 425 |
|
| 426 |
+
# Create Gradio interface - SIMPLIFIED without problematic components
|
| 427 |
with gr.Blocks(
|
| 428 |
title=APP_NAME,
|
| 429 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
) as demo:
|
| 431 |
|
| 432 |
+
gr.Markdown(f"# 🧭 {APP_NAME}")
|
| 433 |
+
gr.Markdown("SEBA Class 10 AI Tutor - Ask questions in Assamese or English")
|
| 434 |
+
|
| 435 |
+
with gr.Row():
|
| 436 |
+
with gr.Column(scale=1):
|
| 437 |
+
gr.Markdown("### 👤 লগিন")
|
| 438 |
+
username = gr.Textbox(
|
| 439 |
+
label="আপোনাৰ নাম",
|
| 440 |
+
placeholder="আপোনাৰ নাম লিখক...",
|
| 441 |
+
max_lines=1
|
| 442 |
+
)
|
| 443 |
+
gr.Markdown("""
|
| 444 |
+
### 💡 টিপছ
|
| 445 |
+
- নাম লিখি প্ৰশ্ন সোধক
|
| 446 |
+
- পাঠ্যপুথিৰ PDF ফাইলসমূহ `pdfs/class10` ফ'ল্ডাৰত ৰাখক
|
| 447 |
+
- ছবি আপলোড কৰিলে OCR ৰ সহায়ত পাঠ পঢ়িব
|
| 448 |
+
""")
|
| 449 |
|
| 450 |
+
with gr.Column(scale=2):
|
| 451 |
+
chatbot = gr.Chatbot(
|
| 452 |
+
label="জাজাবৰ সৈতে কথোপকথন",
|
| 453 |
+
height=500
|
| 454 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
|
| 456 |
+
with gr.Row():
|
| 457 |
+
message = gr.Textbox(
|
| 458 |
+
label="প্ৰশ্ন",
|
| 459 |
+
placeholder="আপোনাৰ প্ৰশ্ন ইয়াত লিখক...",
|
| 460 |
+
lines=2
|
| 461 |
)
|
| 462 |
+
|
| 463 |
+
with gr.Row():
|
| 464 |
+
image = gr.Image(
|
| 465 |
+
label="ছবি আপলোড কৰক (ঐচ্ছিক)",
|
| 466 |
+
type="filepath"
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
+
with gr.Row():
|
| 470 |
+
submit_btn = gr.Button("📤 প্ৰশ্ন পঠিয়াওক", variant="primary")
|
| 471 |
+
clear_btn = gr.Button("🧹 পৰিষ্কাৰ কৰক", variant="secondary")
|
| 472 |
+
|
| 473 |
+
# Event handlers
|
| 474 |
+
submit_btn.click(
|
| 475 |
+
fn=chat_function,
|
| 476 |
+
inputs=[message, image, chatbot, username],
|
| 477 |
+
outputs=[chatbot, message]
|
| 478 |
+
)
|
| 479 |
+
|
| 480 |
+
message.submit(
|
| 481 |
+
fn=chat_function,
|
| 482 |
+
inputs=[message, image, chatbot, username],
|
| 483 |
+
outputs=[chatbot, message]
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
clear_btn.click(
|
| 487 |
+
fn=clear_chat,
|
| 488 |
+
outputs=[chatbot, image]
|
| 489 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
return demo
|
| 492 |
|
| 493 |
+
# -------------------- MAIN --------------------
|
| 494 |
if __name__ == "__main__":
|
| 495 |
if not GRADIO_AVAILABLE:
|
| 496 |
+
print("❌ Gradio not available. Please install gradio.")
|
| 497 |
exit(1)
|
| 498 |
|
|
|
|
|
|
|
|
|
|
| 499 |
try:
|
| 500 |
+
demo = create_app()
|
| 501 |
+
print("✅ App created successfully")
|
| 502 |
+
|
| 503 |
+
# Simple launch for Hugging Face Spaces
|
| 504 |
demo.launch(
|
| 505 |
+
server_name="0.0.0.0",
|
| 506 |
server_port=7860,
|
| 507 |
+
share=False # Critical: must be False for Spaces
|
|
|
|
| 508 |
)
|
| 509 |
except Exception as e:
|
| 510 |
+
print(f"❌ Error launching app: {e}")
|
| 511 |
+
# Final fallback
|
| 512 |
+
try:
|
| 513 |
+
demo.launch(share=False)
|
| 514 |
+
except:
|
| 515 |
+
print("💥 Failed to launch application")
|