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

the paid version of chatgbt of the app

Browse files
Files changed (1) hide show
  1. app.py +57 -108
app.py CHANGED
@@ -100,129 +100,78 @@ async def question_answering_image(question: str = Form(...), image_file: Upload
100
  async def get_docs(request: Request):
101
  return templates.TemplateResponse("index.html", {"request": request})
102
  """
103
- from fastapi import FastAPI, Form, File, UploadFile, HTTPException
104
- from fastapi.responses import RedirectResponse
105
- from fastapi.staticfiles import StaticFiles
106
- from pydantic import BaseModel
107
  from transformers import pipeline
108
- import os
 
109
  from PIL import Image
110
- import io
111
- import pdfplumber
112
- import docx
113
  import pytesseract
114
- from io import BytesIO
115
- import fitz # PyMuPDF
116
  import easyocr
117
- from fastapi.templating import Jinja2Templates
118
- from starlette.requests import Request
119
 
120
- # Initialize the app
121
  app = FastAPI()
122
 
123
- # Mount the static directory to serve HTML, CSS, JS files
124
- app.mount("/static", StaticFiles(directory="static"), name="static")
125
-
126
- # Define a template for rendering HTML
127
- templates = Jinja2Templates(directory="templates")
128
-
129
- # Initialize transformers pipelines
130
  qa_pipeline = pipeline("question-answering", model="microsoft/phi-2", tokenizer="microsoft/phi-2")
131
  image_qa_pipeline = pipeline("vqa", model="Salesforce/blip-vqa-base")
132
-
133
- # Initialize EasyOCR for image-based text extraction
134
  reader = easyocr.Reader(['en'])
135
 
136
- # Maximum allowed file size in bytes (e.g., 5 MB)
137
- MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB
 
 
138
 
139
- # Function to process PDFs
140
- def extract_pdf_text(file_path: str):
141
- with pdfplumber.open(file_path) as pdf:
142
- text = ""
143
- for page in pdf.pages:
144
- text += page.extract_text()
145
- return text
146
 
147
- # Function to process DOCX files
148
- def extract_docx_text(file_path: str):
149
- doc = docx.Document(file_path)
150
- text = "\n".join([para.text for para in doc.paragraphs])
151
- return text
152
-
153
- # Function to process PPTX files
154
- def extract_pptx_text(file_path: str):
155
- from pptx import Presentation
156
- prs = Presentation(file_path)
157
- text = "\n".join([shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text")])
158
- return text
159
 
160
- # Function to extract text from images using OCR
161
- def extract_text_from_image(image: Image):
162
  return pytesseract.image_to_string(image)
163
 
164
- # Home route - Render the index page
165
- @app.get("/")
166
- async def home(request: Request):
167
- return templates.TemplateResponse("index.html", {"request": request})
168
-
169
- # Function to answer questions based on document content
170
- @app.post("/question-answering-doc")
171
- async def question_answering_doc(request: Request, question: str = Form(...), file: UploadFile = File(...)):
172
- # Validate file size
173
- if file.spool_max_size > MAX_FILE_SIZE:
174
- raise HTTPException(status_code=400, detail=f"File size exceeds the {MAX_FILE_SIZE / (1024 * 1024)} MB limit.")
175
-
176
- try:
177
- # Read the file content into memory
178
- file_content = await file.read()
179
-
180
- # Extract text based on the file type
181
- if file.filename.endswith(".pdf"):
182
- file_path = "/tmp/tempfile.pdf"
183
- with open(file_path, "wb") as f:
184
- f.write(file_content)
185
- text = extract_pdf_text(file_path)
186
- os.remove(file_path)
187
- elif file.filename.endswith(".docx"):
188
- file_path = "/tmp/tempfile.docx"
189
- with open(file_path, "wb") as f:
190
- f.write(file_content)
191
- text = extract_docx_text(file_path)
192
- os.remove(file_path)
193
- elif file.filename.endswith(".pptx"):
194
- file_path = "/tmp/tempfile.pptx"
195
- with open(file_path, "wb") as f:
196
- f.write(file_content)
197
- text = extract_pptx_text(file_path)
198
- os.remove(file_path)
199
- else:
200
- raise HTTPException(status_code=400, detail="Unsupported file format")
201
- except Exception as e:
202
- raise HTTPException(status_code=500, detail=f"An error occurred while processing the file: {str(e)}")
203
-
204
- qa_result = qa_pipeline(question=question, context=text)
205
-
206
- return templates.TemplateResponse("index.html", {"request": request, "answer": qa_result['answer']})
207
-
208
- # Function to answer questions based on images
209
- @app.post("/question-answering-image")
210
- async def question_answering_image(request: Request, question: str = Form(...), image_file: UploadFile = File(...)):
211
- # Validate file size
212
- if image_file.spool_max_size > MAX_FILE_SIZE:
213
- raise HTTPException(status_code=400, detail=f"File size exceeds the {MAX_FILE_SIZE / (1024 * 1024)} MB limit.")
214
-
215
- image = Image.open(BytesIO(await image_file.read()))
216
- image_text = extract_text_from_image(image)
217
-
218
- image_qa_result = image_qa_pipeline({"image": image, "question": question})
219
 
220
- # Clean up the temporary image file
221
- del image
222
-
223
- return templates.TemplateResponse("index.html", {"request": request, "answer": image_qa_result[0]['answer'], "image_text": image_text})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
- # Serve the application in Hugging Face space
226
- @app.get("/question-answering-doc")
227
- async def get_docs(request: Request):
228
- return templates.TemplateResponse("index.html", {"request": request})
 
100
  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
 
114
+ # Initialize FastAPI app
115
  app = FastAPI()
116
 
117
+ # Load models
 
 
 
 
 
 
118
  qa_pipeline = pipeline("question-answering", model="microsoft/phi-2", tokenizer="microsoft/phi-2")
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())
126
 
127
+ def extract_text_from_docx(file):
128
+ doc = docx.Document(file)
129
+ return "\n".join(para.text for para in doc.paragraphs)
 
 
 
 
130
 
131
+ def extract_text_from_pptx(file):
132
+ prs = Presentation(file)
133
+ return "\n".join(shape.text for slide in prs.slides for shape in slide.shapes if hasattr(shape, "text"))
 
 
 
 
 
 
 
 
 
134
 
135
+ 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":
146
+ context = extract_text_from_docx(file)
147
+ elif file_ext == ".pptx":
148
+ context = extract_text_from_pptx(file)
149
+ elif file_ext in [".png", ".jpg", ".jpeg", ".bmp"]:
150
+ context = extract_text_from_image(file)
151
+ else:
152
+ return "❌ Unsupported file format."
153
+
154
+ if not context.strip():
155
+ return "⚠️ No readable text found in the document."
156
+
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")