Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,7 @@ import pdfplumber
|
|
3 |
import docx
|
4 |
import openpyxl
|
5 |
from pptx import Presentation
|
6 |
-
import
|
7 |
-
from torchvision import transforms
|
8 |
-
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
9 |
-
from PIL import Image
|
10 |
from transformers import pipeline
|
11 |
import gradio as gr
|
12 |
from fastapi.responses import RedirectResponse
|
@@ -17,15 +14,6 @@ app = FastAPI()
|
|
17 |
# Load AI Model for Question Answering
|
18 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large", tokenizer="google/flan-t5-large", use_fast=True)
|
19 |
|
20 |
-
# Load Pretrained Object Detection Model (Torchvision)
|
21 |
-
model = fasterrcnn_resnet50_fpn(pretrained=True)
|
22 |
-
model.eval()
|
23 |
-
|
24 |
-
# Image Transformations
|
25 |
-
transform = transforms.Compose([
|
26 |
-
transforms.ToTensor()
|
27 |
-
])
|
28 |
-
|
29 |
# Function to truncate text to 450 tokens
|
30 |
def truncate_text(text, max_tokens=450):
|
31 |
words = text.split()
|
@@ -60,20 +48,10 @@ def extract_text_from_excel(excel_file):
|
|
60 |
text.append(" ".join(map(str, row)))
|
61 |
return "\n".join(text)
|
62 |
|
63 |
-
# Function to perform object detection using Torchvision
|
64 |
def extract_text_from_image(image_file):
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
with torch.no_grad():
|
69 |
-
predictions = model(image_tensor)
|
70 |
-
|
71 |
-
detected_objects = []
|
72 |
-
for label, score in zip(predictions[0]['labels'], predictions[0]['scores']):
|
73 |
-
if score > 0.7:
|
74 |
-
detected_objects.append(f"Object {label.item()} detected with confidence {score.item():.2f}")
|
75 |
-
|
76 |
-
return "\n".join(detected_objects) if detected_objects else "No objects detected."
|
77 |
|
78 |
# Function to answer questions based on document content
|
79 |
def answer_question_from_document(file, question):
|
@@ -103,7 +81,7 @@ def answer_question_from_document(file, question):
|
|
103 |
def answer_question_from_image(image, question):
|
104 |
image_text = extract_text_from_image(image)
|
105 |
if not image_text:
|
106 |
-
return "No
|
107 |
|
108 |
truncated_text = truncate_text(image_text)
|
109 |
input_text = f"Question: {question} Context: {truncated_text}"
|
@@ -132,4 +110,4 @@ app = gr.mount_gradio_app(app, demo, path="/")
|
|
132 |
|
133 |
@app.get("/")
|
134 |
def home():
|
135 |
-
return RedirectResponse(url="/")
|
|
|
3 |
import docx
|
4 |
import openpyxl
|
5 |
from pptx import Presentation
|
6 |
+
import easyocr
|
|
|
|
|
|
|
7 |
from transformers import pipeline
|
8 |
import gradio as gr
|
9 |
from fastapi.responses import RedirectResponse
|
|
|
14 |
# Load AI Model for Question Answering
|
15 |
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large", tokenizer="google/flan-t5-large", use_fast=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Function to truncate text to 450 tokens
|
18 |
def truncate_text(text, max_tokens=450):
|
19 |
words = text.split()
|
|
|
48 |
text.append(" ".join(map(str, row)))
|
49 |
return "\n".join(text)
|
50 |
|
|
|
51 |
def extract_text_from_image(image_file):
|
52 |
+
reader = easyocr.Reader(["en"])
|
53 |
+
result = reader.readtext(image_file)
|
54 |
+
return " ".join([res[1] for res in result])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Function to answer questions based on document content
|
57 |
def answer_question_from_document(file, question):
|
|
|
81 |
def answer_question_from_image(image, question):
|
82 |
image_text = extract_text_from_image(image)
|
83 |
if not image_text:
|
84 |
+
return "No text detected in the image."
|
85 |
|
86 |
truncated_text = truncate_text(image_text)
|
87 |
input_text = f"Question: {question} Context: {truncated_text}"
|
|
|
110 |
|
111 |
@app.get("/")
|
112 |
def home():
|
113 |
+
return RedirectResponse(url="/")
|