Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -90,7 +90,8 @@ def chunk_text(text, max_chunk_size=1500):
|
|
| 90 |
current_chunk += " " + sent
|
| 91 |
current_tokens += sent_tokens
|
| 92 |
else:
|
| 93 |
-
|
|
|
|
| 94 |
current_chunk = sent
|
| 95 |
current_tokens = sent_tokens
|
| 96 |
if current_chunk:
|
|
@@ -163,13 +164,26 @@ def extract_text_from_pdf(pdf_file) -> str:
|
|
| 163 |
# Try native extraction
|
| 164 |
extracted = page.extract_text()
|
| 165 |
if not extracted or is_page_scanned(extracted):
|
| 166 |
-
# Convert to image and OCR
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
if extracted:
|
| 174 |
all_text.append(extracted)
|
| 175 |
return "\n".join(all_text)
|
|
@@ -344,6 +358,7 @@ def retrieve_relevant_chunks(session_id, question):
|
|
| 344 |
block_words = set(block.lower().split())
|
| 345 |
score = len(question_words.intersection(block_words))
|
| 346 |
block_scores.append((score, fname, block))
|
|
|
|
| 347 |
block_scores.sort(key=lambda x: x[0], reverse=True)
|
| 348 |
|
| 349 |
# Take top 3 chunks
|
|
@@ -412,7 +427,7 @@ def transcribe_audio(audio):
|
|
| 412 |
"""
|
| 413 |
if audio is None:
|
| 414 |
return ""
|
| 415 |
-
# audio is
|
| 416 |
filepath = audio
|
| 417 |
# Whisper pipeline approach:
|
| 418 |
import torchaudio
|
|
@@ -430,16 +445,16 @@ def tts_speak(text):
|
|
| 430 |
"""
|
| 431 |
with io.BytesIO() as buf:
|
| 432 |
# Save TTS to a WAV in memory
|
| 433 |
-
|
|
|
|
| 434 |
tts_engine.runAndWait()
|
| 435 |
# Now read that file
|
| 436 |
-
if os.path.exists(
|
| 437 |
-
with open(
|
| 438 |
buf.write(f.read())
|
| 439 |
-
os.remove(
|
| 440 |
audio_data = buf.getvalue()
|
| 441 |
-
#
|
| 442 |
-
# Gradio can handle raw bytes if we do ("audio/wav", base64_string)
|
| 443 |
base64_wav = base64.b64encode(audio_data).decode("utf-8")
|
| 444 |
return f"data:audio/wav;base64,{base64_wav}"
|
| 445 |
|
|
|
|
| 90 |
current_chunk += " " + sent
|
| 91 |
current_tokens += sent_tokens
|
| 92 |
else:
|
| 93 |
+
if current_chunk:
|
| 94 |
+
chunks.append(current_chunk.strip())
|
| 95 |
current_chunk = sent
|
| 96 |
current_tokens = sent_tokens
|
| 97 |
if current_chunk:
|
|
|
|
| 164 |
# Try native extraction
|
| 165 |
extracted = page.extract_text()
|
| 166 |
if not extracted or is_page_scanned(extracted):
|
| 167 |
+
# Convert page to image and apply OCR
|
| 168 |
+
try:
|
| 169 |
+
# Extract single page as a separate PDF
|
| 170 |
+
writer = PyPDF2.PdfWriter()
|
| 171 |
+
writer.add_page(page)
|
| 172 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 173 |
+
writer.write(temp_pdf)
|
| 174 |
+
temp_pdf_path = temp_pdf.name
|
| 175 |
+
|
| 176 |
+
# Convert PDF to image using pdf2image
|
| 177 |
+
from pdf2image import convert_from_path
|
| 178 |
+
images = convert_from_path(temp_pdf_path)
|
| 179 |
+
if images:
|
| 180 |
+
ocr_text = pytesseract.image_to_string(images[0])
|
| 181 |
+
all_text.append(ocr_text)
|
| 182 |
+
else:
|
| 183 |
+
all_text.append("")
|
| 184 |
+
os.remove(temp_pdf_path)
|
| 185 |
+
except Exception as e:
|
| 186 |
+
all_text.append(f"[OCR Error on page {page_index + 1}]: {e}")
|
| 187 |
if extracted:
|
| 188 |
all_text.append(extracted)
|
| 189 |
return "\n".join(all_text)
|
|
|
|
| 358 |
block_words = set(block.lower().split())
|
| 359 |
score = len(question_words.intersection(block_words))
|
| 360 |
block_scores.append((score, fname, block))
|
| 361 |
+
|
| 362 |
block_scores.sort(key=lambda x: x[0], reverse=True)
|
| 363 |
|
| 364 |
# Take top 3 chunks
|
|
|
|
| 427 |
"""
|
| 428 |
if audio is None:
|
| 429 |
return ""
|
| 430 |
+
# audio is filepath
|
| 431 |
filepath = audio
|
| 432 |
# Whisper pipeline approach:
|
| 433 |
import torchaudio
|
|
|
|
| 445 |
"""
|
| 446 |
with io.BytesIO() as buf:
|
| 447 |
# Save TTS to a WAV in memory
|
| 448 |
+
temp_audio_path = "output.wav"
|
| 449 |
+
tts_engine.save_to_file(text, temp_audio_path)
|
| 450 |
tts_engine.runAndWait()
|
| 451 |
# Now read that file
|
| 452 |
+
if os.path.exists(temp_audio_path):
|
| 453 |
+
with open(temp_audio_path, "rb") as f:
|
| 454 |
buf.write(f.read())
|
| 455 |
+
os.remove(temp_audio_path)
|
| 456 |
audio_data = buf.getvalue()
|
| 457 |
+
# Encode to base64
|
|
|
|
| 458 |
base64_wav = base64.b64encode(audio_data).decode("utf-8")
|
| 459 |
return f"data:audio/wav;base64,{base64_wav}"
|
| 460 |
|