ikraamkb commited on
Commit
dbe3ba4
·
verified ·
1 Parent(s): 59b31a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -1,10 +1,12 @@
1
-
2
  from fastapi import FastAPI, File, UploadFile
3
  import pdfplumber
4
  import docx
5
  import openpyxl
6
  from pptx import Presentation
7
- import easyocr
 
 
 
8
  from transformers import pipeline
9
  import gradio as gr
10
  from fastapi.responses import RedirectResponse
@@ -15,6 +17,15 @@ app = FastAPI()
15
  # Load AI Model for Question Answering
16
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-large", tokenizer="google/flan-t5-large", use_fast=True)
17
 
 
 
 
 
 
 
 
 
 
18
  # Function to truncate text to 450 tokens
19
  def truncate_text(text, max_tokens=450):
20
  words = text.split()
@@ -49,10 +60,20 @@ def extract_text_from_excel(excel_file):
49
  text.append(" ".join(map(str, row)))
50
  return "\n".join(text)
51
 
 
52
  def extract_text_from_image(image_file):
53
- reader = easyocr.Reader(["en"])
54
- result = reader.readtext(image_file)
55
- return " ".join([res[1] for res in result])
 
 
 
 
 
 
 
 
 
56
 
57
  # Function to answer questions based on document content
58
  def answer_question_from_document(file, question):
@@ -82,7 +103,7 @@ def answer_question_from_document(file, question):
82
  def answer_question_from_image(image, question):
83
  image_text = extract_text_from_image(image)
84
  if not image_text:
85
- return "No text detected in the image."
86
 
87
  truncated_text = truncate_text(image_text)
88
  input_text = f"Question: {question} Context: {truncated_text}"
 
 
1
  from fastapi import FastAPI, File, UploadFile
2
  import pdfplumber
3
  import docx
4
  import openpyxl
5
  from pptx import Presentation
6
+ import torch
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
  # 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
  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
+ image = Image.open(image_file).convert("RGB")
66
+ image_tensor = transform(image).unsqueeze(0)
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
  def answer_question_from_image(image, question):
104
  image_text = extract_text_from_image(image)
105
  if not image_text:
106
+ return "No meaningful content detected in the image."
107
 
108
  truncated_text = truncate_text(image_text)
109
  input_text = f"Question: {question} Context: {truncated_text}"