ikraamkb's picture
Update app.py
4be6e3a verified
from fastapi import FastAPI, Form, File, UploadFile
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from transformers import pipeline
import os
from PIL import Image
import io
import pdfplumber
import docx
import openpyxl
import pytesseract
from io import BytesIO
import fitz # PyMuPDF
import easyocr
from fastapi.templating import Jinja2Templates
from starlette.requests import Request
# Initialize the app
app = FastAPI()
# Mount the static directory to serve HTML, CSS, JS files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Initialize transformers pipelines
qa_pipeline = pipeline("question-answering", model="microsoft/phi-2", tokenizer="microsoft/phi-2")
image_qa_pipeline = pipeline("image-question-answering", model="Salesforce/blip-vqa-base", tokenizer="Salesforce/blip-vqa-base")
# Initialize EasyOCR for image-based text extraction
reader = easyocr.Reader(['en'])
# Define a template for rendering HTML
templates = Jinja2Templates(directory="templates")
# Function to process PDFs
def extract_pdf_text(file_path: str):
with pdfplumber.open(file_path) as pdf:
text = ""
for page in pdf.pages:
text += page.extract_text()
return text
# Function to process DOCX files
def extract_docx_text(file_path: str):
doc = docx.Document(file_path)
text = ""
for para in doc.paragraphs:
text += para.text
return text
# Function to process PPTX files
def extract_pptx_text(file_path: str):
from pptx import Presentation
prs = Presentation(file_path)
text = ""
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
text += shape.text
return text
# Function to extract text from images using OCR
def extract_text_from_image(image: Image):
text = pytesseract.image_to_string(image)
return text
# Home route
@app.get("/")
def home():
return RedirectResponse(url="/docs")
# Function to answer questions based on document content
@app.post("/question-answering-doc")
async def question_answering_doc(question: str = Form(...), file: UploadFile = File(...)):
# Save the uploaded file temporarily
file_path = f"temp_files/{file.filename}"
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "wb") as f:
f.write(await file.read())
# Extract text based on file type
if file.filename.endswith(".pdf"):
text = extract_pdf_text(file_path)
elif file.filename.endswith(".docx"):
text = extract_docx_text(file_path)
elif file.filename.endswith(".pptx"):
text = extract_pptx_text(file_path)
else:
return {"error": "Unsupported file format"}
# Use the model for question answering
qa_result = qa_pipeline(question=question, context=text)
return {"answer": qa_result['answer']}
# Function to answer questions based on images
@app.post("/question-answering-image")
async def question_answering_image(question: str = Form(...), image_file: UploadFile = File(...)):
# Open the uploaded image
image = Image.open(BytesIO(await image_file.read()))
# Use EasyOCR to extract text if the image has textual content
image_text = extract_text_from_image(image)
# Use the BLIP VQA model for question answering on the image
image_qa_result = image_qa_pipeline(image=image, question=question)
return {"answer": image_qa_result['answer'], "image_text": image_text}
# Serve the application in Hugging Face space
@app.get("/docs")
async def get_docs(request: Request):
return templates.TemplateResponse("index.html", {"request": request})