ikraamkb commited on
Commit
68df520
·
verified ·
1 Parent(s): 0000b07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -93
app.py CHANGED
@@ -1,78 +1,4 @@
1
 
2
- """
3
- from fastapi import FastAPI
4
- from fastapi.responses import RedirectResponse
5
- import gradio as gr
6
-
7
- from transformers import pipeline, ViltProcessor, ViltForQuestionAnswering, AutoTokenizer, AutoModelForCausalLM
8
- from PIL import Image
9
- import torch
10
- import fitz # PyMuPDF for PDF
11
-
12
-
13
-
14
- app = FastAPI()
15
-
16
-
17
-
18
- # ========== Document QA Setup ==========
19
- doc_tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
20
- doc_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
21
-
22
- def read_pdf(file):
23
- doc = fitz.open(stream=file.read(), filetype="pdf")
24
- text = ""
25
- for page in doc:
26
- text += page.get_text()
27
- return text
28
-
29
- def answer_question_from_doc(file, question):
30
- if file is None or not question.strip():
31
- return "Please upload a document and ask a question."
32
- text = read_pdf(file)
33
- prompt = f"Context: {text}\nQuestion: {question}\nAnswer:"
34
- inputs = doc_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048)
35
- with torch.no_grad():
36
- outputs = doc_model.generate(**inputs, max_new_tokens=100)
37
- answer = doc_tokenizer.decode(outputs[0], skip_special_tokens=True)
38
- return answer.split("Answer:")[-1].strip()
39
-
40
- # ========== Image QA Setup ==========
41
- vqa_processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
42
- vqa_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
43
-
44
- def answer_question_from_image(image, question):
45
- if image is None or not question.strip():
46
- return "Please upload an image and ask a question."
47
- inputs = vqa_processor(image, question, return_tensors="pt")
48
- with torch.no_grad():
49
- outputs = vqa_model(**inputs)
50
- predicted_id = outputs.logits.argmax(-1).item()
51
- return vqa_model.config.id2label[predicted_id]
52
-
53
- # ========== Gradio Interfaces ==========
54
- doc_interface = gr.Interface(
55
- fn=answer_question_from_doc,
56
- inputs=[gr.File(label="Upload Document (PDF)"), gr.Textbox(label="Ask a Question")],
57
- outputs="text",
58
- title="Document Question Answering"
59
- )
60
-
61
- img_interface = gr.Interface(
62
- fn=answer_question_from_image,
63
- inputs=[gr.Image(label="Upload Image"), gr.Textbox(label="Ask a Question")],
64
- outputs="text",
65
- title="Image Question Answering"
66
- )
67
-
68
- # ========== Combine and Mount ==========
69
- demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
70
- app = gr.mount_gradio_app(app, demo, path="/")
71
-
72
- @app.get("/")
73
- def root():
74
- return RedirectResponse(url="/")
75
- """
76
  import gradio as gr
77
  import fitz # PyMuPDF for PDFs
78
  import easyocr # OCR for images
@@ -136,10 +62,6 @@ def extract_text_from_xlsx(xlsx_file):
136
  return f"Error reading XLSX: {e}"
137
  return "\n".join(text)
138
 
139
- def extract_text_from_image(image_path):
140
- """Extract text from an image using EasyOCR."""
141
- result = reader.readtext(image_path, detail=0)
142
- return " ".join(result) # Return text as a single string
143
 
144
  # ---- MAIN PROCESSING FUNCTIONS ----
145
  def answer_question_from_doc(file, question):
@@ -167,26 +89,12 @@ def answer_question_from_doc(file, question):
167
  except Exception as e:
168
  return f"Error generating answer: {e}"
169
 
170
- def answer_question_from_image(image, question):
171
- """Process an image, extract text, and answer a question."""
172
- img_text = extract_text_from_image(image)
173
- if not img_text.strip():
174
- return """No readable text found in the image."""
175
-
176
  try:
177
  result = qa_model({"question": question, "context": img_text})
178
  return result["answer"]
179
  except Exception as e:
180
  return f"Error generating answer: {e}"
181
 
182
- # ---- GRADIO INTERFACES ----
183
- with gr.Blocks() as doc_interface:
184
- gr.Markdown("## 📄 Document Question Answering")
185
- file_input = gr.File(label="Upload DOCX, PPTX, XLSX, or PDF")
186
- question_input = gr.Textbox(label="Ask a question")
187
- answer_output = gr.Textbox(label="Answer")
188
- file_submit = gr.Button("Get Answer")
189
- file_submit.click(answer_question_from_doc, inputs=[file_input, question_input], outputs=answer_output)
190
 
191
  with gr.Blocks() as img_interface:
192
  gr.Markdown("## 🖼️ Image Question Answering")
@@ -197,7 +105,7 @@ with gr.Blocks() as img_interface:
197
  image_submit.click(answer_question_from_image, inputs=[image_input, img_question_input], outputs=img_answer_output)
198
 
199
  # ---- MOUNT GRADIO APP ----
200
- demo = gr.TabbedInterface([doc_interface, img_interface], ["Document QA", "Image QA"])
201
  app = gr.mount_gradio_app(app, demo, path="/")
202
 
203
  @app.get("/")
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import fitz # PyMuPDF for PDFs
4
  import easyocr # OCR for images
 
62
  return f"Error reading XLSX: {e}"
63
  return "\n".join(text)
64
 
 
 
 
 
65
 
66
  # ---- MAIN PROCESSING FUNCTIONS ----
67
  def answer_question_from_doc(file, question):
 
89
  except Exception as e:
90
  return f"Error generating answer: {e}"
91
 
 
 
 
 
 
 
92
  try:
93
  result = qa_model({"question": question, "context": img_text})
94
  return result["answer"]
95
  except Exception as e:
96
  return f"Error generating answer: {e}"
97
 
 
 
 
 
 
 
 
 
98
 
99
  with gr.Blocks() as img_interface:
100
  gr.Markdown("## 🖼️ Image Question Answering")
 
105
  image_submit.click(answer_question_from_image, inputs=[image_input, img_question_input], outputs=img_answer_output)
106
 
107
  # ---- MOUNT GRADIO APP ----
108
+ demo = gr.TabbedInterface(img_interface, "Image QA")
109
  app = gr.mount_gradio_app(app, demo, path="/")
110
 
111
  @app.get("/")