ikraamkb commited on
Commit
2be14bd
·
verified ·
1 Parent(s): de0be90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py CHANGED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
+ from typing import List
3
+ import pdfplumber
4
+ import pytesseract
5
+ from PIL import Image
6
+ import easyocr
7
+ import docx
8
+ import openpyxl
9
+ from pptx import Presentation
10
+ from transformers import pipeline
11
+ import io
12
+
13
+ app = FastAPI()
14
+
15
+ # Load Hugging Face models
16
+ qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
17
+ vqa_pipeline = pipeline("image-to-text", model="Salesforce/blip-vqa-base") # For images
18
+
19
+ def extract_text_from_pdf(pdf_file):
20
+ text = ""
21
+ with pdfplumber.open(pdf_file) as pdf:
22
+ for page in pdf.pages:
23
+ text += page.extract_text() + "\n"
24
+ return text.strip()
25
+
26
+ def extract_text_from_docx(docx_file):
27
+ doc = docx.Document(docx_file)
28
+ return "\n".join([para.text for para in doc.paragraphs])
29
+
30
+ def extract_text_from_pptx(pptx_file):
31
+ ppt = Presentation(pptx_file)
32
+ text = []
33
+ for slide in ppt.slides:
34
+ for shape in slide.shapes:
35
+ if hasattr(shape, "text"):
36
+ text.append(shape.text)
37
+ return "\n".join(text)
38
+
39
+ def extract_text_from_excel(excel_file):
40
+ wb = openpyxl.load_workbook(excel_file)
41
+ text = []
42
+ for sheet in wb.worksheets:
43
+ for row in sheet.iter_rows(values_only=True):
44
+ text.append(" ".join(map(str, row)))
45
+ return "\n".join(text)
46
+
47
+ def extract_text_from_image(image_file):
48
+ reader = easyocr.Reader(["en"])
49
+ result = reader.readtext(image_file)
50
+ return " ".join([res[1] for res in result])
51
+
52
+ @app.post("/qa/document/")
53
+ async def qa_document(file: UploadFile = File(...), question: str = Form(...)):
54
+ file_ext = file.filename.split(".")[-1].lower()
55
+
56
+ if file_ext == "pdf":
57
+ text = extract_text_from_pdf(io.BytesIO(await file.read()))
58
+ elif file_ext == "docx":
59
+ text = extract_text_from_docx(io.BytesIO(await file.read()))
60
+ elif file_ext == "pptx":
61
+ text = extract_text_from_pptx(io.BytesIO(await file.read()))
62
+ elif file_ext == "xlsx":
63
+ text = extract_text_from_excel(io.BytesIO(await file.read()))
64
+ else:
65
+ return {"error": "Unsupported file format!"}
66
+
67
+ if not text:
68
+ return {"error": "No text extracted from the document."}
69
+
70
+ response = qa_pipeline(question=question, context=text)
71
+ return {"question": question, "answer": response["answer"]}
72
+
73
+ @app.post("/qa/image/")
74
+ async def qa_image(file: UploadFile = File(...), question: str = Form(...)):
75
+ image = Image.open(io.BytesIO(await file.read()))
76
+ image_text = extract_text_from_image(image)
77
+
78
+ if not image_text:
79
+ return {"error": "No text detected in the image."}
80
+
81
+ response = qa_pipeline(question=question, context=image_text)
82
+ return {"question": question, "answer": response["answer"]}
83
+