Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -86,9 +86,8 @@ app = FastAPI()
|
|
86 |
|
87 |
# Initialize AI Models
|
88 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
89 |
-
|
90 |
image_captioning = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
91 |
-
reader = easyocr.Reader(['en', 'fr']) #
|
92 |
|
93 |
# ---- TEXT EXTRACTION FUNCTIONS ----
|
94 |
def extract_text_from_pdf(pdf_file):
|
@@ -157,9 +156,12 @@ def answer_question_from_doc(file, question):
|
|
157 |
if not context.strip():
|
158 |
return "No text found in the document."
|
159 |
|
160 |
-
# Generate answer using
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
163 |
|
164 |
def answer_question_from_image(image, question):
|
165 |
"""Process an image, extract text, and answer a question."""
|
@@ -167,13 +169,15 @@ def answer_question_from_image(image, question):
|
|
167 |
if not img_text.strip():
|
168 |
return "No readable text found in the image."
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
173 |
|
174 |
# ---- GRADIO INTERFACES ----
|
175 |
with gr.Blocks() as doc_interface:
|
176 |
-
gr.Markdown("## Document Question Answering")
|
177 |
file_input = gr.File(label="Upload DOCX, PPTX, XLSX, or PDF")
|
178 |
question_input = gr.Textbox(label="Ask a question")
|
179 |
answer_output = gr.Textbox(label="Answer")
|
@@ -181,7 +185,7 @@ with gr.Blocks() as doc_interface:
|
|
181 |
file_submit.click(answer_question_from_doc, inputs=[file_input, question_input], outputs=answer_output)
|
182 |
|
183 |
with gr.Blocks() as img_interface:
|
184 |
-
gr.Markdown("## Image Question Answering")
|
185 |
image_input = gr.Image(label="Upload an Image")
|
186 |
img_question_input = gr.Textbox(label="Ask a question")
|
187 |
img_answer_output = gr.Textbox(label="Answer")
|
|
|
86 |
|
87 |
# Initialize AI Models
|
88 |
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
|
89 |
image_captioning = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
90 |
+
reader = easyocr.Reader(['en', 'fr']) # OCR for English & French
|
91 |
|
92 |
# ---- TEXT EXTRACTION FUNCTIONS ----
|
93 |
def extract_text_from_pdf(pdf_file):
|
|
|
156 |
if not context.strip():
|
157 |
return "No text found in the document."
|
158 |
|
159 |
+
# Generate answer using QA pipeline correctly
|
160 |
+
try:
|
161 |
+
result = qa_model({"question": question, "context": context})
|
162 |
+
return result["answer"]
|
163 |
+
except Exception as e:
|
164 |
+
return f"Error generating answer: {e}"
|
165 |
|
166 |
def answer_question_from_image(image, question):
|
167 |
"""Process an image, extract text, and answer a question."""
|
|
|
169 |
if not img_text.strip():
|
170 |
return "No readable text found in the image."
|
171 |
|
172 |
+
try:
|
173 |
+
result = qa_model({"question": question, "context": img_text})
|
174 |
+
return result["answer"]
|
175 |
+
except Exception as e:
|
176 |
+
return f"Error generating answer: {e}"
|
177 |
|
178 |
# ---- GRADIO INTERFACES ----
|
179 |
with gr.Blocks() as doc_interface:
|
180 |
+
gr.Markdown("## 📄 Document Question Answering")
|
181 |
file_input = gr.File(label="Upload DOCX, PPTX, XLSX, or PDF")
|
182 |
question_input = gr.Textbox(label="Ask a question")
|
183 |
answer_output = gr.Textbox(label="Answer")
|
|
|
185 |
file_submit.click(answer_question_from_doc, inputs=[file_input, question_input], outputs=answer_output)
|
186 |
|
187 |
with gr.Blocks() as img_interface:
|
188 |
+
gr.Markdown("## 🖼️ Image Question Answering")
|
189 |
image_input = gr.Image(label="Upload an Image")
|
190 |
img_question_input = gr.Textbox(label="Ask a question")
|
191 |
img_answer_output = gr.Textbox(label="Answer")
|