ikraamkb commited on
Commit
79c8ea7
Β·
verified Β·
1 Parent(s): 0540355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  import uvicorn
3
  import numpy as np
4
  import fitz # PyMuPDF
@@ -111,6 +111,137 @@ with gr.Blocks() as demo:
111
  # βœ… Mount Gradio with FastAPI
112
  app = gr.mount_gradio_app(app, demo, path="/")
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  @app.get("/")
115
  def home():
116
  return RedirectResponse(url="/")
 
1
+ """import gradio as gr
2
  import uvicorn
3
  import numpy as np
4
  import fitz # PyMuPDF
 
111
  # βœ… Mount Gradio with FastAPI
112
  app = gr.mount_gradio_app(app, demo, path="/")
113
 
114
+ @app.get("/")
115
+ def home():
116
+ return RedirectResponse(url="/")
117
+
118
+ # βœ… Run FastAPI + Gradio
119
+ if __name__ == "__main__":
120
+ uvicorn.run(app, host="0.0.0.0", port=7860)
121
+ """ import gradio as gr
122
+ import uvicorn
123
+ import numpy as np
124
+ import fitz # PyMuPDF
125
+ import tika
126
+ import torch
127
+ from fastapi import FastAPI
128
+ from transformers import pipeline
129
+ from PIL import Image
130
+ from io import BytesIO
131
+ from starlette.responses import RedirectResponse
132
+ from tika import parser
133
+ from openpyxl import load_workbook
134
+
135
+ # Initialize Tika for DOCX & PPTX parsing
136
+ tika.initVM()
137
+
138
+ # Initialize FastAPI
139
+ app = FastAPI()
140
+
141
+ # Load models
142
+ device = "cuda" if torch.cuda.is_available() else "cpu"
143
+ qa_pipeline = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", device=device)
144
+ image_captioning_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
145
+
146
+ ALLOWED_EXTENSIONS = {"pdf", "docx", "pptx", "xlsx"}
147
+
148
+ # βœ… Function to Validate File Type
149
+ def validate_file_type(file):
150
+ if file is None:
151
+ return "❌ No file uploaded!"
152
+ if isinstance(file, str): # Text-based input (NamedString)
153
+ return None
154
+ if hasattr(file, "name"):
155
+ ext = file.name.split(".")[-1].lower()
156
+ if ext not in ALLOWED_EXTENSIONS:
157
+ return f"❌ Unsupported file format: {ext}"
158
+ return None
159
+ return "❌ Invalid file format!"
160
+
161
+ # βœ… Extract Text from PDF
162
+ def extract_text_from_pdf(file):
163
+ try:
164
+ doc = fitz.open(stream=file, filetype="pdf")
165
+ return "\n".join([page.get_text() for page in doc])
166
+ except Exception:
167
+ return None
168
+
169
+ # βœ… Extract Text from DOCX & PPTX using Tika
170
+ def extract_text_with_tika(file):
171
+ try:
172
+ return parser.from_buffer(file)["content"]
173
+ except Exception:
174
+ return None
175
+
176
+ # βœ… Extract Text from Excel
177
+ def extract_text_from_excel(file):
178
+ try:
179
+ wb = load_workbook(BytesIO(file), data_only=True)
180
+ text = []
181
+ for sheet in wb.worksheets:
182
+ for row in sheet.iter_rows(values_only=True):
183
+ text.append(" ".join(str(cell) for cell in row if cell))
184
+ return "\n".join(text)
185
+ except Exception:
186
+ return None
187
+
188
+ # βœ… Truncate Long Text for Model
189
+ def truncate_text(text, max_length=2048):
190
+ return text[:max_length] if len(text) > max_length else text
191
+
192
+ # βœ… Answer Questions from Image or Document
193
+ def answer_question(file, question: str):
194
+ # Image Processing (Gradio sends images as NumPy arrays)
195
+ if isinstance(file, np.ndarray):
196
+ image = Image.fromarray(file)
197
+ caption = image_captioning_pipeline(image)[0]['generated_text']
198
+ response = qa_pipeline(f"Question: {question}\nContext: {caption}")
199
+ return response[0]["generated_text"]
200
+
201
+ # Validate File
202
+ validation_error = validate_file_type(file)
203
+ if validation_error:
204
+ return validation_error
205
+
206
+ # βœ… Read File Bytes Properly
207
+ file_ext = file.name.split(".")[-1].lower() if hasattr(file, "name") else None
208
+ file_bytes = file.read() if hasattr(file, "read") else None
209
+ if not file_bytes:
210
+ return "❌ Could not read file content!"
211
+
212
+ # Extract Text from Supported Documents
213
+ text = None
214
+ if file_ext == "pdf":
215
+ text = extract_text_from_pdf(file_bytes)
216
+ elif file_ext in ["docx", "pptx"]:
217
+ text = extract_text_with_tika(file_bytes)
218
+ elif file_ext == "xlsx":
219
+ text = extract_text_from_excel(file_bytes)
220
+
221
+ if not text:
222
+ return "⚠️ No text extracted from the document."
223
+
224
+ truncated_text = truncate_text(text)
225
+ response = qa_pipeline(f"Question: {question}\nContext: {truncated_text}")
226
+
227
+ return response[0]["generated_text"]
228
+
229
+ # βœ… Gradio Interface (Unified for Images & Documents)
230
+ with gr.Blocks() as demo:
231
+ gr.Markdown("## πŸ“„ AI-Powered Document & Image QA")
232
+
233
+ with gr.Row():
234
+ file_input = gr.File(label="Upload Document / Image")
235
+ question_input = gr.Textbox(label="Ask a Question", placeholder="What is this document about?")
236
+
237
+ answer_output = gr.Textbox(label="Answer")
238
+
239
+ submit_btn = gr.Button("Get Answer")
240
+ submit_btn.click(answer_question, inputs=[file_input, question_input], outputs=answer_output)
241
+
242
+ # βœ… Mount Gradio with FastAPI
243
+ app = gr.mount_gradio_app(app, demo, path="/")
244
+
245
  @app.get("/")
246
  def home():
247
  return RedirectResponse(url="/")