ikraamkb commited on
Commit
04626e2
·
verified ·
1 Parent(s): b36b2d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -100
app.py CHANGED
@@ -1,104 +1,4 @@
1
- """from fastapi import FastAPI, Form, File, UploadFile
2
- from fastapi.responses import RedirectResponse
3
- from fastapi.staticfiles import StaticFiles
4
- from pydantic import BaseModel
5
- from transformers import pipeline
6
- import os
7
- from PIL import Image
8
- import io
9
- import pdfplumber
10
- import docx
11
- import openpyxl
12
- import pytesseract
13
- from io import BytesIO
14
- import fitz # PyMuPDF
15
- import easyocr
16
- from fastapi.templating import Jinja2Templates
17
- from starlette.requests import Request
18
-
19
- # Initialize the app
20
- app = FastAPI()
21
-
22
- # Mount the static directory to serve HTML, CSS, JS files
23
- app.mount("/static", StaticFiles(directory="static"), name="static")
24
-
25
- # Initialize transformers pipelines
26
- qa_pipeline = pipeline("question-answering", model="microsoft/phi-2", tokenizer="microsoft/phi-2")
27
- image_qa_pipeline = pipeline("vqa", model="Salesforce/blip-vqa-base")
28
-
29
- # Initialize EasyOCR for image-based text extraction
30
- reader = easyocr.Reader(['en'])
31
-
32
- # Define a template for rendering HTML
33
- templates = Jinja2Templates(directory="templates")
34
 
35
- # Ensure temp_files directory exists
36
- temp_dir = "temp_files"
37
- os.makedirs(temp_dir, exist_ok=True)
38
-
39
- # Function to process PDFs
40
- def extract_pdf_text(file_path: str):
41
- with pdfplumber.open(file_path) as pdf:
42
- text = ""
43
- for page in pdf.pages:
44
- text += page.extract_text()
45
- return text
46
-
47
- # Function to process DOCX files
48
- def extract_docx_text(file_path: str):
49
- doc = docx.Document(file_path)
50
- text = "\n".join([para.text for para in doc.paragraphs])
51
- return text
52
-
53
- # Function to process PPTX files
54
- def extract_pptx_text(file_path: str):
55
- from pptx import Presentation
56
- prs = Presentation(file_path)
57
- text = "\n".join([shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text")])
58
- return text
59
-
60
- # Function to extract text from images using OCR
61
- def extract_text_from_image(image: Image):
62
- return pytesseract.image_to_string(image)
63
-
64
- # Home route
65
- @app.get("/")
66
- def home():
67
- return RedirectResponse(url="/docs")
68
-
69
- # Function to answer questions based on document content
70
- @app.post("/question-answering-doc")
71
- async def question_answering_doc(question: str = Form(...), file: UploadFile = File(...)):
72
- file_path = os.path.join(temp_dir, file.filename)
73
- with open(file_path, "wb") as f:
74
- f.write(await file.read())
75
-
76
- if file.filename.endswith(".pdf"):
77
- text = extract_pdf_text(file_path)
78
- elif file.filename.endswith(".docx"):
79
- text = extract_docx_text(file_path)
80
- elif file.filename.endswith(".pptx"):
81
- text = extract_pptx_text(file_path)
82
- else:
83
- return {"error": "Unsupported file format"}
84
-
85
- qa_result = qa_pipeline(question=question, context=text)
86
- return {"answer": qa_result['answer']}
87
-
88
- # Function to answer questions based on images
89
- @app.post("/question-answering-image")
90
- async def question_answering_image(question: str = Form(...), image_file: UploadFile = File(...)):
91
- image = Image.open(BytesIO(await image_file.read()))
92
- image_text = extract_text_from_image(image)
93
-
94
- image_qa_result = image_qa_pipeline({"image": image, "question": question})
95
-
96
- return {"answer": image_qa_result[0]['answer'], "image_text": image_text}
97
-
98
- # Serve the application in Hugging Face space
99
- @app.get("/docs")
100
- async def get_docs(request: Request):
101
- return templates.TemplateResponse("index.html", {"request": request})
102
  """
103
  from fastapi import FastAPI
104
  from fastapi.responses import RedirectResponse
@@ -165,6 +65,129 @@ img_interface = gr.Interface(
165
  demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
166
  app = gr.mount_gradio_app(app, demo, path="/")
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  @app.get("/")
169
  def root():
170
  return RedirectResponse(url="/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  """
3
  from fastapi import FastAPI
4
  from fastapi.responses import RedirectResponse
 
65
  demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
66
  app = gr.mount_gradio_app(app, demo, path="/")
67
 
68
+ @app.get("/")
69
+ def root():
70
+ return RedirectResponse(url="/")
71
+ """
72
+ from fastapi import FastAPI
73
+ from fastapi.responses import RedirectResponse
74
+ import gradio as gr
75
+ import pytesseract
76
+ from PIL import Image
77
+ import fitz # PyMuPDF
78
+ import pdfplumber
79
+ import easyocr
80
+ import docx
81
+ import openpyxl
82
+ from pptx import Presentation
83
+ from transformers import pipeline
84
+ from deep_translator import GoogleTranslator
85
+ import json
86
+ import os
87
+
88
+ app = FastAPI()
89
+
90
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
91
+ reader = easyocr.Reader(['en'])
92
+
93
+ # Utility functions
94
+
95
+ def extract_text_from_pdf(pdf_file):
96
+ with pdfplumber.open(pdf_file) as pdf:
97
+ return "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
98
+
99
+ def extract_text_from_docx(docx_file):
100
+ doc = docx.Document(docx_file)
101
+ return "\n".join([para.text for para in doc.paragraphs])
102
+
103
+ def extract_text_from_pptx(pptx_file):
104
+ prs = Presentation(pptx_file)
105
+ text = []
106
+ for slide in prs.slides:
107
+ for shape in slide.shapes:
108
+ if hasattr(shape, "text"):
109
+ text.append(shape.text)
110
+ return "\n".join(text)
111
+
112
+ def extract_text_from_xlsx(xlsx_file):
113
+ wb = openpyxl.load_workbook(xlsx_file)
114
+ text = []
115
+ for sheet in wb.worksheets:
116
+ for row in sheet.iter_rows():
117
+ text.extend([str(cell.value) for cell in row if cell.value is not None])
118
+ return "\n".join(text)
119
+
120
+ def extract_text(file):
121
+ ext = os.path.splitext(file.name)[1].lower()
122
+ if ext == ".pdf":
123
+ return extract_text_from_pdf(file)
124
+ elif ext == ".docx":
125
+ return extract_text_from_docx(file)
126
+ elif ext == ".pptx":
127
+ return extract_text_from_pptx(file)
128
+ elif ext == ".xlsx":
129
+ return extract_text_from_xlsx(file)
130
+ else:
131
+ return "Unsupported file type"
132
+
133
+ def answer_question_from_doc(file, question, translate_to="en"):
134
+ context = extract_text(file)
135
+ result = qa_pipeline(question=question, context=context)
136
+ translated = GoogleTranslator(source='auto', target=translate_to).translate(result["answer"])
137
+ return {
138
+ "answer": translated,
139
+ "score": result["score"],
140
+ "original": result["answer"]
141
+ }
142
+
143
+ def answer_question_from_image(image, question, translate_to="en"):
144
+ img_text = pytesseract.image_to_string(image)
145
+ if not img_text.strip():
146
+ img_text = "\n".join([line[1] for line in reader.readtext(image)])
147
+ result = qa_pipeline(question=question, context=img_text)
148
+ translated = GoogleTranslator(source='auto', target=translate_to).translate(result["answer"])
149
+ return {
150
+ "answer": translated,
151
+ "score": result["score"],
152
+ "original": result["answer"]
153
+ }
154
+
155
+ # Gradio Interfaces
156
+
157
+ doc_interface = gr.Interface(
158
+ fn=answer_question_from_doc,
159
+ inputs=[
160
+ gr.File(label="Upload Document (PDF, DOCX, PPTX, XLSX)"),
161
+ gr.Textbox(label="Ask a Question"),
162
+ gr.Textbox(label="Translate Answer To (e.g., en, fr, ar)", value="en")
163
+ ],
164
+ outputs=[
165
+ gr.Textbox(label="Translated Answer"),
166
+ gr.Number(label="Confidence Score"),
167
+ gr.Textbox(label="Original Answer")
168
+ ],
169
+ title="📄 Document QA + Translation + Export"
170
+ )
171
+
172
+ img_interface = gr.Interface(
173
+ fn=answer_question_from_image,
174
+ inputs=[
175
+ gr.Image(label="Upload Image"),
176
+ gr.Textbox(label="Ask a Question"),
177
+ gr.Textbox(label="Translate Answer To (e.g., en, fr, ar)", value="en")
178
+ ],
179
+ outputs=[
180
+ gr.Textbox(label="Translated Answer"),
181
+ gr.Number(label="Confidence Score"),
182
+ gr.Textbox(label="Original Answer")
183
+ ],
184
+ title="🖼️ Image QA + OCR + Translation + Export"
185
+ )
186
+
187
+ # Combine interfaces
188
+ demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
189
+ app = gr.mount_gradio_app(app, demo, path="/")
190
+
191
  @app.get("/")
192
  def root():
193
  return RedirectResponse(url="/")