Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -110,7 +110,6 @@ from PIL import Image
|
|
110 |
import io
|
111 |
import pdfplumber
|
112 |
import docx
|
113 |
-
import openpyxl
|
114 |
import pytesseract
|
115 |
from io import BytesIO
|
116 |
import fitz # PyMuPDF
|
@@ -170,7 +169,7 @@ def home():
|
|
170 |
|
171 |
# Function to answer questions based on document content
|
172 |
@app.post("/question-answering-doc")
|
173 |
-
async def question_answering_doc(question: str = Form(...), file: UploadFile = File(...)):
|
174 |
file_path = os.path.join(temp_dir, file.filename)
|
175 |
with open(file_path, "wb") as f:
|
176 |
f.write(await file.read())
|
@@ -185,17 +184,17 @@ async def question_answering_doc(question: str = Form(...), file: UploadFile = F
|
|
185 |
return {"error": "Unsupported file format"}
|
186 |
|
187 |
qa_result = qa_pipeline(question=question, context=text)
|
188 |
-
return templates.TemplateResponse("index.html", {"request":
|
189 |
|
190 |
# Function to answer questions based on images
|
191 |
@app.post("/question-answering-image")
|
192 |
-
async def question_answering_image(question: str = Form(...), image_file: UploadFile = File(...)):
|
193 |
image = Image.open(BytesIO(await image_file.read()))
|
194 |
image_text = extract_text_from_image(image)
|
195 |
|
196 |
image_qa_result = image_qa_pipeline({"image": image, "question": question})
|
197 |
|
198 |
-
return templates.TemplateResponse("index.html", {"request":
|
199 |
|
200 |
# Serve the application in Hugging Face space
|
201 |
@app.get("/docs")
|
|
|
110 |
import io
|
111 |
import pdfplumber
|
112 |
import docx
|
|
|
113 |
import pytesseract
|
114 |
from io import BytesIO
|
115 |
import fitz # PyMuPDF
|
|
|
169 |
|
170 |
# Function to answer questions based on document content
|
171 |
@app.post("/question-answering-doc")
|
172 |
+
async def question_answering_doc(request: Request, question: str = Form(...), file: UploadFile = File(...)):
|
173 |
file_path = os.path.join(temp_dir, file.filename)
|
174 |
with open(file_path, "wb") as f:
|
175 |
f.write(await file.read())
|
|
|
184 |
return {"error": "Unsupported file format"}
|
185 |
|
186 |
qa_result = qa_pipeline(question=question, context=text)
|
187 |
+
return templates.TemplateResponse("index.html", {"request": request, "answer": qa_result['answer']})
|
188 |
|
189 |
# Function to answer questions based on images
|
190 |
@app.post("/question-answering-image")
|
191 |
+
async def question_answering_image(request: Request, question: str = Form(...), image_file: UploadFile = File(...)):
|
192 |
image = Image.open(BytesIO(await image_file.read()))
|
193 |
image_text = extract_text_from_image(image)
|
194 |
|
195 |
image_qa_result = image_qa_pipeline({"image": image, "question": question})
|
196 |
|
197 |
+
return templates.TemplateResponse("index.html", {"request": request, "answer": image_qa_result[0]['answer'], "image_text": image_text})
|
198 |
|
199 |
# Serve the application in Hugging Face space
|
200 |
@app.get("/docs")
|