pranavinani commited on
Commit
e2e039a
·
1 Parent(s): 0ae63a7

fixed pdf not showing bug

Browse files
Files changed (2) hide show
  1. Man Ki baat.txt +0 -24
  2. app.py +97 -19
Man Ki baat.txt DELETED
@@ -1,24 +0,0 @@
1
- मन की बात - प्रधानमंत्री नरेंद्र मोदी के रेडियो कार्यक्रम से
2
-
3
- मेरे प्यारे देशवासियो, नमस्कार। आज फिर एक बार मन की बात के माध्यम से आप सभी से जुड़ने का अवसर मिला है।
4
-
5
- यह कार्यक्रम देश के कोने-कोने से आने वाली आवाजों को एक मंच प्रदान करता है। आज मैं कुछ ऐसी बातें आपके साथ साझा करना चाहता हूं जो हमारे समाज और देश के विकास से जुड़ी हुई हैं।
6
-
7
- शिक्षा का महत्व:
8
- शिक्षा हमारे जीवन की आधारशिला है। यह न केवल व्यक्तिगत विकास के लिए आवश्यक है बल्कि राष्ट्रीय प्रगति के लिए भी अत्यंत महत्वपूर्ण है। हमें अपने बच्चों को न केवल पुस्तकीय ज्ञान देना चाहिए बल्कि व्यावहारिक शिक्षा भी प्रदान करनी चाहिए।
9
-
10
- युवाओं की शक्ति:
11
- हमारे देश की सबसे बड़ी संपत्ति हमारे युवा हैं। वे नवाचार, उद्यमिता और तकनीकी प्रगति के क्षेत्र में अपनी छाप छोड़ रहे हैं। हमें उनका समर्थन करना चाहिए और उन्हें आगे बढ़ने के लिए प्रेरित करना चाहिए।
12
-
13
- पर्यावरण संरक्षण:
14
- पर्यावरण का संरक्षण आज के समय की सबसे बड़ी आवश्यकता है। हमें प्रकृति के साथ सामंजस्य बिठाकर विकास करना होगा। वृक्षारोपण, जल संरक्षण, और स्वच्छता के माध्यम से हम एक बेहतर भविष्य का निर्माण कर सकते हैं।
15
-
16
- सामाजिक एकता:
17
- हमारे देश की विविधता में एकता ही हमारी सबसे बड़ी ताकत है। हमें सभी धर्मों, जातियों और भाषाओं का सम्मान करते हुए एक साथ आगे बढ़ना चाहिए।
18
-
19
- तकनीकी प्रगति:
20
- डिजिटल इंडिया की दिशा में हमारे कदम तेजी से आगे बढ़ रहे हैं। तकनीक का उपयोग करके हम न केवल अपना जीवन आसान बना सकते हैं बल्कि राष्ट्रीय विकास में भी योगदान दे सकते हैं।
21
-
22
- अंत में, मैं आप सभी से कहना चाहूंगा कि हम सभी मिलकर एक नए भारत का निर्माण कर सकते हैं। एक ऐसा भारत जो आत्मनिर्भर हो, स्वच्छ हो, और जहां हर व्यक्ति को आगे बढ़ने का समान अवसर मिले।
23
-
24
- धन्यवाद। जय हिंद!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -433,7 +433,7 @@ def reset_session():
433
 
434
  # Book management functions
435
  def get_available_books():
436
- """Get list of available books with their thumbnails and text files"""
437
  books = []
438
 
439
  try:
@@ -447,16 +447,16 @@ def get_available_books():
447
  else:
448
  thumbnail_files = []
449
 
450
- # Get all text files from OCR directory
451
  if os.path.exists(ocr_dir):
452
- text_files = [f for f in os.listdir(ocr_dir)
453
- if f.lower().endswith('.txt')]
454
  else:
455
- text_files = []
456
 
457
- # Create book entries
458
- for text_file in text_files:
459
- book_name = os.path.splitext(text_file)[0]
460
 
461
  # Look for matching thumbnail
462
  thumbnail_path = None
@@ -475,7 +475,7 @@ def get_available_books():
475
  books.append({
476
  'name': book_name,
477
  'display_name': book_name.replace('_', ' ').title(),
478
- 'text_file': os.path.join(ocr_dir, text_file),
479
  'thumbnail': thumbnail_path
480
  })
481
 
@@ -516,19 +516,19 @@ def create_text_placeholder(book_name):
516
  print(f"Error creating placeholder: {str(e)}")
517
  return None
518
 
519
- def load_book_text(book_info):
520
- """Load text content from a pre-existing book"""
521
  try:
522
- with open(book_info['text_file'], 'r', encoding='utf-8') as file:
523
- content = file.read()
524
 
525
- if not content.strip():
526
- return "Error: Empty text file"
527
 
528
- return content
529
 
530
  except Exception as e:
531
- return f"Error loading book text: {str(e)}"
532
 
533
  def process_selected_book(selected_book_name):
534
  """Process a pre-selected book"""
@@ -549,8 +549,8 @@ def process_selected_book(selected_book_name):
549
  if not selected_book:
550
  return "चुनी गई पुस्तक नहीं मिली।", "", "", gr.update(visible=False)
551
 
552
- # Load text content
553
- text_content = load_book_text(selected_book)
554
 
555
  if not text_content.strip() or "Error" in text_content:
556
  return text_content, "", "", gr.update(visible=False)
@@ -636,6 +636,80 @@ def handle_gallery_selection(evt: gr.SelectData):
636
 
637
  return "None"
638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639
  # Create Gradio interface
640
  def create_interface():
641
  """Create the Gradio interface"""
@@ -840,6 +914,10 @@ def create_interface():
840
  def main():
841
  """Main function to launch the application"""
842
  print("🚀 Starting Hindi RAG Voice Demo (Groq Whisper API Version)...")
 
 
 
 
843
  print("📋 Loading AI models (this may take a moment)...")
844
 
845
  # Pre-load models
 
433
 
434
  # Book management functions
435
  def get_available_books():
436
+ """Get list of available books with their thumbnails and PDF files"""
437
  books = []
438
 
439
  try:
 
447
  else:
448
  thumbnail_files = []
449
 
450
+ # Get all PDF files from OCR directory
451
  if os.path.exists(ocr_dir):
452
+ pdf_files = [f for f in os.listdir(ocr_dir)
453
+ if f.lower().endswith('.pdf')]
454
  else:
455
+ pdf_files = []
456
 
457
+ # Create book entries for PDF files
458
+ for pdf_file in pdf_files:
459
+ book_name = os.path.splitext(pdf_file)[0]
460
 
461
  # Look for matching thumbnail
462
  thumbnail_path = None
 
475
  books.append({
476
  'name': book_name,
477
  'display_name': book_name.replace('_', ' ').title(),
478
+ 'pdf_file': os.path.join(ocr_dir, pdf_file),
479
  'thumbnail': thumbnail_path
480
  })
481
 
 
516
  print(f"Error creating placeholder: {str(e)}")
517
  return None
518
 
519
+ def load_book_pdf(book_info):
520
+ """Load text content from a pre-existing PDF book"""
521
  try:
522
+ # Extract text from PDF using the existing function
523
+ text_content = extract_text_from_pdf(book_info['pdf_file'])
524
 
525
+ if not text_content.strip() or "Error" in text_content:
526
+ return text_content
527
 
528
+ return text_content
529
 
530
  except Exception as e:
531
+ return f"Error loading PDF book: {str(e)}"
532
 
533
  def process_selected_book(selected_book_name):
534
  """Process a pre-selected book"""
 
549
  if not selected_book:
550
  return "चुनी गई पुस्तक नहीं मिली।", "", "", gr.update(visible=False)
551
 
552
+ # Load PDF content and extract text
553
+ text_content = load_book_pdf(selected_book)
554
 
555
  if not text_content.strip() or "Error" in text_content:
556
  return text_content, "", "", gr.update(visible=False)
 
636
 
637
  return "None"
638
 
639
+ # LFS file handling for Hugging Face Spaces
640
+ def ensure_lfs_files_downloaded():
641
+ """Ensure LFS files are downloaded in Hugging Face Spaces environment"""
642
+ try:
643
+ # Check if we're in a Hugging Face Spaces environment
644
+ if os.getenv('SPACE_ID') or os.getenv('HUGGINGFACE_HUB_CACHE'):
645
+ print("🔄 Detected Hugging Face Spaces environment, checking LFS files...")
646
+
647
+ # Check if PDF files exist and are not LFS pointers
648
+ ocr_dir = CONFIG['OCR_BOOKS_DIR']
649
+ if os.path.exists(ocr_dir):
650
+ pdf_files = [f for f in os.listdir(ocr_dir) if f.lower().endswith('.pdf')]
651
+
652
+ for pdf_file in pdf_files:
653
+ pdf_path = os.path.join(ocr_dir, pdf_file)
654
+
655
+ # Check if file is an LFS pointer (small text file)
656
+ if os.path.exists(pdf_path):
657
+ file_size = os.path.getsize(pdf_path)
658
+
659
+ # LFS pointer files are typically very small (< 200 bytes)
660
+ if file_size < 200:
661
+ print(f"📁 {pdf_file} appears to be an LFS pointer, attempting download...")
662
+
663
+ # Try to download using git lfs pull for this specific file
664
+ try:
665
+ result = subprocess.run(
666
+ ['git', 'lfs', 'pull', '--include', f"ocr_books/{pdf_file}"],
667
+ cwd=os.getcwd(),
668
+ capture_output=True,
669
+ text=True,
670
+ timeout=60
671
+ )
672
+
673
+ if result.returncode == 0:
674
+ print(f"✅ Successfully downloaded {pdf_file}")
675
+ else:
676
+ print(f"⚠️ Could not download {pdf_file}: {result.stderr}")
677
+
678
+ except subprocess.TimeoutExpired:
679
+ print(f"⏰ Timeout downloading {pdf_file}")
680
+ except Exception as e:
681
+ print(f"❌ Error downloading {pdf_file}: {str(e)}")
682
+ else:
683
+ print(f"✅ {pdf_file} already downloaded ({file_size:,} bytes)")
684
+
685
+ # Also check thumbnails
686
+ thumbnail_dir = CONFIG['BOOK_THUMBNAILS_DIR']
687
+ if os.path.exists(thumbnail_dir):
688
+ image_files = [f for f in os.listdir(thumbnail_dir)
689
+ if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
690
+
691
+ for img_file in image_files:
692
+ img_path = os.path.join(thumbnail_dir, img_file)
693
+ if os.path.exists(img_path):
694
+ file_size = os.path.getsize(img_path)
695
+ if file_size < 200: # Likely an LFS pointer
696
+ print(f"📁 {img_file} appears to be an LFS pointer, attempting download...")
697
+ try:
698
+ result = subprocess.run(
699
+ ['git', 'lfs', 'pull', '--include', f"book_thumbnails/{img_file}"],
700
+ cwd=os.getcwd(),
701
+ capture_output=True,
702
+ text=True,
703
+ timeout=30
704
+ )
705
+ if result.returncode == 0:
706
+ print(f"✅ Successfully downloaded {img_file}")
707
+ except Exception as e:
708
+ print(f"❌ Error downloading {img_file}: {str(e)}")
709
+
710
+ except Exception as e:
711
+ print(f"⚠️ Error checking LFS files: {str(e)}")
712
+
713
  # Create Gradio interface
714
  def create_interface():
715
  """Create the Gradio interface"""
 
914
  def main():
915
  """Main function to launch the application"""
916
  print("🚀 Starting Hindi RAG Voice Demo (Groq Whisper API Version)...")
917
+
918
+ # Ensure LFS files are available (important for Hugging Face Spaces)
919
+ ensure_lfs_files_downloaded()
920
+
921
  print("📋 Loading AI models (this may take a moment)...")
922
 
923
  # Pre-load models