Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,6 @@ from transformers import pipeline
|
|
11 |
import gradio as gr
|
12 |
from fastapi.responses import RedirectResponse
|
13 |
import numpy as np
|
14 |
-
import easyocr
|
15 |
|
16 |
# Initialize FastAPI
|
17 |
print("π FastAPI server is starting...")
|
@@ -30,10 +29,6 @@ weights = FasterRCNN_ResNet50_FPN_Weights.DEFAULT
|
|
30 |
model = fasterrcnn_resnet50_fpn(weights=weights)
|
31 |
model.eval()
|
32 |
|
33 |
-
# Initialize OCR Model (Lazy Load)
|
34 |
-
print("π Initializing OCR Model...")
|
35 |
-
reader = easyocr.Reader(["en"], gpu=True)
|
36 |
-
|
37 |
# Image Transformations
|
38 |
transform = transforms.Compose([
|
39 |
transforms.ToTensor()
|
@@ -103,16 +98,6 @@ def extract_text_from_excel(excel_file):
|
|
103 |
except Exception as e:
|
104 |
return f"β Error reading Excel: {str(e)}"
|
105 |
|
106 |
-
def extract_text_from_image(image_file):
|
107 |
-
print("πΌοΈ Extracting text from image...")
|
108 |
-
image = Image.open(image_file).convert("RGB")
|
109 |
-
if np.array(image).std() < 10:
|
110 |
-
print("β οΈ Low contrast detected. No meaningful content.")
|
111 |
-
return "β οΈ No meaningful content detected in the image."
|
112 |
-
result = reader.readtext(np.array(image))
|
113 |
-
print("β
Image text extraction completed.")
|
114 |
-
return " ".join([res[1] for res in result]) if result else "β οΈ No text found."
|
115 |
-
|
116 |
def answer_question_from_document(file, question):
|
117 |
print("π Processing document for QA...")
|
118 |
validation_error = validate_file_type(file)
|
@@ -135,23 +120,11 @@ def answer_question_from_document(file, question):
|
|
135 |
print("β
AI response generated.")
|
136 |
return response[0]["generated_text"]
|
137 |
|
138 |
-
def answer_question_from_image(image, question):
|
139 |
-
print("πΌοΈ Processing image for QA...")
|
140 |
-
image_text = extract_text_from_image(image)
|
141 |
-
if not image_text:
|
142 |
-
return "β οΈ No meaningful content detected in the image."
|
143 |
-
truncated_text = truncate_text(image_text)
|
144 |
-
print("π€ Generating response...")
|
145 |
-
response = qa_pipeline(f"Question: {question}\nContext: {truncated_text}")
|
146 |
-
print("β
AI response generated.")
|
147 |
-
return response[0]["generated_text"]
|
148 |
-
|
149 |
print("β
Models loaded successfully.")
|
150 |
|
151 |
doc_interface = gr.Interface(fn=answer_question_from_document, inputs=[gr.File(), gr.Textbox()], outputs="text")
|
152 |
-
img_interface = gr.Interface(fn=answer_question_from_image, inputs=[gr.Image(), gr.Textbox()], outputs="text")
|
153 |
|
154 |
-
demo = gr.TabbedInterface([doc_interface
|
155 |
app = gr.mount_gradio_app(app, demo, path="/")
|
156 |
|
157 |
@app.get("/")
|
|
|
11 |
import gradio as gr
|
12 |
from fastapi.responses import RedirectResponse
|
13 |
import numpy as np
|
|
|
14 |
|
15 |
# Initialize FastAPI
|
16 |
print("π FastAPI server is starting...")
|
|
|
29 |
model = fasterrcnn_resnet50_fpn(weights=weights)
|
30 |
model.eval()
|
31 |
|
|
|
|
|
|
|
|
|
32 |
# Image Transformations
|
33 |
transform = transforms.Compose([
|
34 |
transforms.ToTensor()
|
|
|
98 |
except Exception as e:
|
99 |
return f"β Error reading Excel: {str(e)}"
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
def answer_question_from_document(file, question):
|
102 |
print("π Processing document for QA...")
|
103 |
validation_error = validate_file_type(file)
|
|
|
120 |
print("β
AI response generated.")
|
121 |
return response[0]["generated_text"]
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
print("β
Models loaded successfully.")
|
124 |
|
125 |
doc_interface = gr.Interface(fn=answer_question_from_document, inputs=[gr.File(), gr.Textbox()], outputs="text")
|
|
|
126 |
|
127 |
+
demo = gr.TabbedInterface([doc_interface], ["Document QA"])
|
128 |
app = gr.mount_gradio_app(app, demo, path="/")
|
129 |
|
130 |
@app.get("/")
|