Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,18 +148,12 @@ def get_working_model(key):
|
|
| 148 |
except: return None
|
| 149 |
return None
|
| 150 |
|
|
|
|
| 151 |
def get_book_text(user_details):
|
| 152 |
try:
|
| 153 |
-
# 1.
|
| 154 |
-
if os.path.exists("books/books")
|
| 155 |
-
|
| 156 |
-
elif os.path.exists("books"):
|
| 157 |
-
base_path = "books" # சிங்கிள் ஃபோல்டர் இருந்தால்
|
| 158 |
-
else:
|
| 159 |
-
print("❌ Error: 'books' folder not found!")
|
| 160 |
-
return None
|
| 161 |
-
|
| 162 |
-
# 2. Path Construction
|
| 163 |
if user_details.get("type") == "school":
|
| 164 |
std = user_details.get("standard", "").lower()
|
| 165 |
sub = user_details.get("subject", "").lower()
|
|
@@ -168,31 +162,35 @@ def get_book_text(user_details):
|
|
| 168 |
dept = user_details.get("dept", "").lower()
|
| 169 |
sub = user_details.get("subject", "").lower()
|
| 170 |
path = os.path.join(base_path, "college", dept, f"{sub}.pdf")
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
print(f"🔍 Searching for file at: {path}")
|
| 174 |
|
| 175 |
if os.path.exists(path):
|
| 176 |
text = ""
|
| 177 |
with open(path, 'rb') as f:
|
| 178 |
reader = PyPDF2.PdfReader(f)
|
| 179 |
|
| 180 |
-
|
| 181 |
-
for i, page in enumerate(reader.pages[:50]): # Limit for speed
|
| 182 |
content = page.extract_text()
|
| 183 |
if content:
|
| 184 |
lines = content.strip().split('\n')
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
if lines:
|
| 187 |
last_line = lines[-1].strip()
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
| 191 |
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
return text
|
| 194 |
else:
|
| 195 |
-
print(f"❌ File not found: {path}")
|
| 196 |
return None
|
| 197 |
except Exception as e:
|
| 198 |
print(f"❌ Error reading PDF: {e}")
|
|
|
|
| 148 |
except: return None
|
| 149 |
return None
|
| 150 |
|
| 151 |
+
# 👇 REPLACED get_book_text (Hybrid: Prints Real Page No if found, else PDF Index) 👇
|
| 152 |
def get_book_text(user_details):
|
| 153 |
try:
|
| 154 |
+
# 1. Path Construction
|
| 155 |
+
base_path = "books/books" if os.path.exists("books/books") else "books"
|
| 156 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if user_details.get("type") == "school":
|
| 158 |
std = user_details.get("standard", "").lower()
|
| 159 |
sub = user_details.get("subject", "").lower()
|
|
|
|
| 162 |
dept = user_details.get("dept", "").lower()
|
| 163 |
sub = user_details.get("subject", "").lower()
|
| 164 |
path = os.path.join(base_path, "college", dept, f"{sub}.pdf")
|
| 165 |
+
|
| 166 |
+
print(f"🔍 Searching: {path}")
|
|
|
|
| 167 |
|
| 168 |
if os.path.exists(path):
|
| 169 |
text = ""
|
| 170 |
with open(path, 'rb') as f:
|
| 171 |
reader = PyPDF2.PdfReader(f)
|
| 172 |
|
| 173 |
+
for i, page in enumerate(reader.pages[:50]): # Speed Limit
|
|
|
|
| 174 |
content = page.extract_text()
|
| 175 |
if content:
|
| 176 |
lines = content.strip().split('\n')
|
| 177 |
+
|
| 178 |
+
# Default: PDF வரிசை எண் (எ.கா: PDF-5)
|
| 179 |
+
page_label = f"PDF-{i+1}"
|
| 180 |
+
|
| 181 |
+
# 👇 SMART CHECK: கீழே ஒரிஜினல் நம்பர் இருக்கான்னு பார்க்கிறோம் 👇
|
| 182 |
if lines:
|
| 183 |
last_line = lines[-1].strip()
|
| 184 |
+
# கடைசி வரி நம்பராக இருந்தால் (எ.கா: "5" or "12")
|
| 185 |
+
# அதுவும் 4 இலக்கத்திற்கு குறைவாக இருந்தால் (வருஷம் 2024 வராமல் இருக்க)
|
| 186 |
+
if last_line.isdigit() and len(last_line) < 4:
|
| 187 |
+
page_label = f"Page {last_line}"
|
| 188 |
|
| 189 |
+
# Marker சேர்க்கிறோம்
|
| 190 |
+
text += f"\n\n--- [[{page_label} START]] ---\n{content}\n--- [[{page_label} END]] ---\n"
|
| 191 |
+
|
| 192 |
return text
|
| 193 |
else:
|
|
|
|
| 194 |
return None
|
| 195 |
except Exception as e:
|
| 196 |
print(f"❌ Error reading PDF: {e}")
|