Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,22 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
|
|
2 |
import fitz # PyMuPDF for PDF parsing
|
3 |
from tika import parser # Apache Tika for document parsing
|
4 |
import openpyxl
|
5 |
from pptx import Presentation
|
6 |
-
import
|
7 |
from transformers import pipeline
|
8 |
import gradio as gr
|
9 |
-
from PIL import Image
|
10 |
import numpy as np
|
11 |
|
12 |
-
# Initialize FastAPI
|
13 |
app = FastAPI()
|
14 |
|
15 |
print(f"π Loading models")
|
16 |
|
17 |
# Load Hugging Face Models
|
18 |
doc_qa_pipeline = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", device=-1)
|
19 |
-
|
20 |
|
21 |
print("β
Models loaded")
|
22 |
|
@@ -67,7 +67,6 @@ def extract_text_from_excel(excel_bytes):
|
|
67 |
except Exception as e:
|
68 |
return f"β Error reading Excel: {str(e)}"
|
69 |
|
70 |
-
# Function to process documents and answer questions
|
71 |
def answer_question_from_document(file: UploadFile, question: str):
|
72 |
print("π Processing document for QA...")
|
73 |
validation_error = validate_file_type(file)
|
@@ -95,17 +94,19 @@ def answer_question_from_document(file: UploadFile, question: str):
|
|
95 |
|
96 |
return response[0]["generated_text"]
|
97 |
|
98 |
-
# Function to process images and answer questions (NO OCR)
|
99 |
def answer_question_from_image(image, question):
|
100 |
try:
|
101 |
print("πΌοΈ Processing image for QA...")
|
102 |
if isinstance(image, np.ndarray): # If it's a NumPy array from Gradio
|
103 |
image = Image.fromarray(image) # Convert to PIL Image
|
104 |
|
105 |
-
print("
|
106 |
-
|
|
|
|
|
|
|
107 |
|
108 |
-
return response[0]["
|
109 |
except Exception as e:
|
110 |
return f"β Error processing image: {str(e)}"
|
111 |
|
@@ -121,15 +122,18 @@ img_interface = gr.Interface(
|
|
121 |
fn=answer_question_from_image,
|
122 |
inputs=[gr.Image(label="πΌοΈ Upload Image"), gr.Textbox(label="π¬ Ask a Question")],
|
123 |
outputs="text",
|
124 |
-
title="πΌοΈ AI Image Question Answering
|
125 |
)
|
126 |
|
127 |
-
#
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
)
|
|
|
|
|
132 |
|
133 |
-
# Run Gradio
|
134 |
if __name__ == "__main__":
|
135 |
-
|
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import RedirectResponse
|
3 |
import fitz # PyMuPDF for PDF parsing
|
4 |
from tika import parser # Apache Tika for document parsing
|
5 |
import openpyxl
|
6 |
from pptx import Presentation
|
7 |
+
from PIL import Image
|
8 |
from transformers import pipeline
|
9 |
import gradio as gr
|
|
|
10 |
import numpy as np
|
11 |
|
12 |
+
# Initialize FastAPI
|
13 |
app = FastAPI()
|
14 |
|
15 |
print(f"π Loading models")
|
16 |
|
17 |
# Load Hugging Face Models
|
18 |
doc_qa_pipeline = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", device=-1)
|
19 |
+
image_captioning_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
20 |
|
21 |
print("β
Models loaded")
|
22 |
|
|
|
67 |
except Exception as e:
|
68 |
return f"β Error reading Excel: {str(e)}"
|
69 |
|
|
|
70 |
def answer_question_from_document(file: UploadFile, question: str):
|
71 |
print("π Processing document for QA...")
|
72 |
validation_error = validate_file_type(file)
|
|
|
94 |
|
95 |
return response[0]["generated_text"]
|
96 |
|
|
|
97 |
def answer_question_from_image(image, question):
|
98 |
try:
|
99 |
print("πΌοΈ Processing image for QA...")
|
100 |
if isinstance(image, np.ndarray): # If it's a NumPy array from Gradio
|
101 |
image = Image.fromarray(image) # Convert to PIL Image
|
102 |
|
103 |
+
print("πΌοΈ Generating caption for image...")
|
104 |
+
caption = image_captioning_pipeline(image)[0]['generated_text']
|
105 |
+
|
106 |
+
print("π€ Answering question based on caption...")
|
107 |
+
response = doc_qa_pipeline(f"Question: {question}\nContext: {caption}")
|
108 |
|
109 |
+
return response[0]["generated_text"]
|
110 |
except Exception as e:
|
111 |
return f"β Error processing image: {str(e)}"
|
112 |
|
|
|
122 |
fn=answer_question_from_image,
|
123 |
inputs=[gr.Image(label="πΌοΈ Upload Image"), gr.Textbox(label="π¬ Ask a Question")],
|
124 |
outputs="text",
|
125 |
+
title="πΌοΈ AI Image Question Answering"
|
126 |
)
|
127 |
|
128 |
+
# Mount Gradio Interfaces
|
129 |
+
demo = gr.TabbedInterface([doc_interface, img_interface], ["π Document QA", "πΌοΈ Image QA"])
|
130 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
131 |
+
|
132 |
+
@app.get("/")
|
133 |
+
def home():
|
134 |
+
return RedirectResponse(url="/")
|
135 |
|
136 |
+
# Run FastAPI + Gradio together
|
137 |
if __name__ == "__main__":
|
138 |
+
import uvicorn
|
139 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|