Spaces:
Sleeping
Sleeping
Update qtAnswering/app.py
Browse files- qtAnswering/app.py +72 -73
qtAnswering/app.py
CHANGED
@@ -1,73 +1,72 @@
|
|
1 |
-
|
2 |
-
from fastapi import
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
9 |
-
from
|
10 |
-
|
11 |
-
import
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
doc
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
prs
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
wb
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
return f"Error generating answer: {e}", None
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
3 |
+
import fitz # PyMuPDF
|
4 |
+
import easyocr
|
5 |
+
import openpyxl
|
6 |
+
import pptx
|
7 |
+
import docx
|
8 |
+
from transformers import pipeline
|
9 |
+
from gtts import gTTS
|
10 |
+
import tempfile
|
11 |
+
import os
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
16 |
+
reader = easyocr.Reader(['en', 'fr'])
|
17 |
+
|
18 |
+
def extract_text_from_pdf(pdf_file):
|
19 |
+
try:
|
20 |
+
with fitz.open(pdf_file) as doc:
|
21 |
+
return "\n".join(page.get_text("text") for page in doc)
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error reading PDF: {e}"
|
24 |
+
|
25 |
+
def extract_text_from_docx(docx_file):
|
26 |
+
doc = docx.Document(docx_file)
|
27 |
+
return "\n".join(p.text for p in doc.paragraphs if p.text.strip())
|
28 |
+
|
29 |
+
def extract_text_from_pptx(pptx_file):
|
30 |
+
try:
|
31 |
+
prs = pptx.Presentation(pptx_file)
|
32 |
+
return "\n".join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text"))
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error reading PPTX: {e}"
|
35 |
+
|
36 |
+
def extract_text_from_xlsx(xlsx_file):
|
37 |
+
try:
|
38 |
+
wb = openpyxl.load_workbook(xlsx_file)
|
39 |
+
return "\n".join(" ".join(str(cell) for cell in row if cell) for sheet in wb.sheetnames for row in wb[sheet].iter_rows(values_only=True))
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error reading XLSX: {e}"
|
42 |
+
|
43 |
+
def answer_question_from_doc(file, question):
|
44 |
+
ext = file.filename.split(".")[-1].lower()
|
45 |
+
file_path = f"/tmp/{file.filename}"
|
46 |
+
|
47 |
+
with open(file_path, "wb") as f:
|
48 |
+
f.write(file.read())
|
49 |
+
|
50 |
+
if ext == "pdf":
|
51 |
+
context = extract_text_from_pdf(file_path)
|
52 |
+
elif ext == "docx":
|
53 |
+
context = extract_text_from_docx(file_path)
|
54 |
+
elif ext == "pptx":
|
55 |
+
context = extract_text_from_pptx(file_path)
|
56 |
+
elif ext == "xlsx":
|
57 |
+
context = extract_text_from_xlsx(file_path)
|
58 |
+
else:
|
59 |
+
return "Unsupported file format.", None
|
60 |
+
|
61 |
+
if not context.strip():
|
62 |
+
return "No text found in the document.", None
|
63 |
+
|
64 |
+
try:
|
65 |
+
result = qa_model({"question": question, "context": context})
|
66 |
+
answer = result["answer"]
|
67 |
+
tts = gTTS(answer)
|
68 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
69 |
+
tts.save(tmp.name)
|
70 |
+
return answer, tmp.name
|
71 |
+
except Exception as e:
|
72 |
+
return f"Error generating answer: {e}", None
|
|