ikraamkb commited on
Commit
df1ed5e
·
verified ·
1 Parent(s): 9325c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -101,13 +101,13 @@ async def get_docs(request: Request):
101
  return templates.TemplateResponse("index.html", {"request": request})
102
  """
103
  from fastapi import FastAPI
 
104
  import gradio as gr
105
  from transformers import pipeline
106
  import pdfplumber, docx
107
  from pptx import Presentation
108
  from PIL import Image
109
  import pytesseract
110
- import fitz
111
  import easyocr
112
  import os
113
 
@@ -119,7 +119,7 @@ qa_pipeline = pipeline("question-answering", model="microsoft/phi-2", tokenizer=
119
  image_qa_pipeline = pipeline("vqa", model="Salesforce/blip-vqa-base")
120
  reader = easyocr.Reader(['en'])
121
 
122
- # File parsing
123
  def extract_text_from_pdf(file):
124
  with pdfplumber.open(file) as pdf:
125
  return "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
@@ -136,10 +136,10 @@ def extract_text_from_image(file):
136
  image = Image.open(file)
137
  return pytesseract.image_to_string(image)
138
 
139
- # Main QA logic
140
  def answer_question(question, file):
141
  file_ext = os.path.splitext(file.name)[-1].lower()
142
-
143
  if file_ext == ".pdf":
144
  context = extract_text_from_pdf(file)
145
  elif file_ext == ".docx":
@@ -157,21 +157,36 @@ def answer_question(question, file):
157
  result = qa_pipeline(question=question, context=context)
158
  return result["answer"]
159
 
160
- # Gradio interface
161
- gr_interface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
162
  fn=answer_question,
163
  inputs=[
164
  gr.Textbox(label="Ask a question"),
165
- gr.File(label="Upload a document or image")
166
  ],
167
  outputs=gr.Textbox(label="Answer"),
168
- title="AI Question Answering (Text & Image)",
169
- description="Upload a file (PDF, DOCX, PPTX, Image) and ask a question. Get instant answers from document content.",
170
  )
171
 
 
 
 
172
  # Mount Gradio app in FastAPI
173
- @app.get("/")
174
- def redirect_root():
175
- return {"message": "Visit /gradio for the interface."}
176
 
177
- app = gr.mount_gradio_app(app, gr_interface, path="/gradio")
 
 
 
 
101
  return templates.TemplateResponse("index.html", {"request": request})
102
  """
103
  from fastapi import FastAPI
104
+ from fastapi.responses import RedirectResponse
105
  import gradio as gr
106
  from transformers import pipeline
107
  import pdfplumber, docx
108
  from pptx import Presentation
109
  from PIL import Image
110
  import pytesseract
 
111
  import easyocr
112
  import os
113
 
 
119
  image_qa_pipeline = pipeline("vqa", model="Salesforce/blip-vqa-base")
120
  reader = easyocr.Reader(['en'])
121
 
122
+ # File parsing functions
123
  def extract_text_from_pdf(file):
124
  with pdfplumber.open(file) as pdf:
125
  return "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
 
136
  image = Image.open(file)
137
  return pytesseract.image_to_string(image)
138
 
139
+ # Main QA logic for documents and images
140
  def answer_question(question, file):
141
  file_ext = os.path.splitext(file.name)[-1].lower()
142
+
143
  if file_ext == ".pdf":
144
  context = extract_text_from_pdf(file)
145
  elif file_ext == ".docx":
 
157
  result = qa_pipeline(question=question, context=context)
158
  return result["answer"]
159
 
160
+ # Create Gradio interfaces for both document and image QA
161
+ doc_interface = gr.Interface(
162
+ fn=answer_question,
163
+ inputs=[
164
+ gr.Textbox(label="Ask a question"),
165
+ gr.File(label="Upload a document (PDF, DOCX, PPTX)")
166
+ ],
167
+ outputs=gr.Textbox(label="Answer"),
168
+ title="Document Question Answering",
169
+ description="Upload a document and ask a question. Get answers from the document content.",
170
+ )
171
+
172
+ img_interface = gr.Interface(
173
  fn=answer_question,
174
  inputs=[
175
  gr.Textbox(label="Ask a question"),
176
+ gr.File(label="Upload an image (PNG, JPG, etc.)")
177
  ],
178
  outputs=gr.Textbox(label="Answer"),
179
+ title="Image Question Answering",
180
+ description="Upload an image and ask a question. Get answers from the text extracted from the image.",
181
  )
182
 
183
+ # Create a Tabbed Interface to switch between document and image QA
184
+ demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
185
+
186
  # Mount Gradio app in FastAPI
187
+ app = gr.mount_gradio_app(app, demo, path="/")
 
 
188
 
189
+ # Redirect to Gradio interface
190
+ @app.get("/")
191
+ def home():
192
+ return RedirectResponse(url="/")