Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
-
# app.py
|
2 |
import fitz # PyMuPDF for PDFs
|
3 |
import easyocr # OCR for images
|
4 |
import openpyxl # XLSX processing
|
5 |
import pptx # PPTX processing
|
6 |
import docx # DOCX processing
|
7 |
from transformers import pipeline
|
|
|
|
|
8 |
|
9 |
# Initialize AI Models
|
10 |
qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
@@ -62,13 +63,18 @@ def answer_question_from_doc(file, question):
|
|
62 |
elif ext == "xlsx":
|
63 |
context = extract_text_from_xlsx(file.name)
|
64 |
else:
|
65 |
-
return "Unsupported file format."
|
66 |
|
67 |
if not context.strip():
|
68 |
-
return "No text found in the document."
|
69 |
|
70 |
try:
|
71 |
result = qa_model({"question": question, "context": context})
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
73 |
except Exception as e:
|
74 |
-
return f"Error generating answer: {e}"
|
|
|
|
|
1 |
import fitz # PyMuPDF for PDFs
|
2 |
import easyocr # OCR for images
|
3 |
import openpyxl # XLSX processing
|
4 |
import pptx # PPTX processing
|
5 |
import docx # DOCX processing
|
6 |
from transformers import pipeline
|
7 |
+
from gtts import gTTS
|
8 |
+
import tempfile
|
9 |
|
10 |
# Initialize AI Models
|
11 |
qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
|
|
63 |
elif ext == "xlsx":
|
64 |
context = extract_text_from_xlsx(file.name)
|
65 |
else:
|
66 |
+
return "Unsupported file format.", None
|
67 |
|
68 |
if not context.strip():
|
69 |
+
return "No text found in the document.", None
|
70 |
|
71 |
try:
|
72 |
result = qa_model({"question": question, "context": context})
|
73 |
+
answer = result["answer"]
|
74 |
+
tts = gTTS(text=answer)
|
75 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
76 |
+
tts.save(tmp.name)
|
77 |
+
audio_path = tmp.name
|
78 |
+
return answer, audio_path
|
79 |
except Exception as e:
|
80 |
+
return f"Error generating answer: {e}", None
|