Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from PIL import Image
|
|
5 |
from transformers import AutoTokenizer, AutoModel
|
6 |
import torch
|
7 |
from pdf2image import convert_from_path
|
|
|
8 |
|
9 |
# CSS styles
|
10 |
css = """
|
@@ -21,13 +22,11 @@ css = """
|
|
21 |
|
22 |
# Define layout with custom styles
|
23 |
layout = [
|
24 |
-
gr.Row([gr.File(label="Upload PDF", type="
|
25 |
-
gr.Row([gr.Button("Generate Insights")]),
|
26 |
gr.Row([gr.Textbox("Placeholder for PDF insights", label="Insights", type="text")])
|
27 |
]
|
28 |
|
29 |
-
|
30 |
-
|
31 |
# Function to get image embeddings using ViT
|
32 |
def get_image_embeddings(image_path, model_name='google/vit-base-patch16-224'):
|
33 |
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
@@ -64,35 +63,43 @@ def get_text_embeddings(text, model_name='bert-base-uncased'):
|
|
64 |
|
65 |
# Function to process PDF and generate a response
|
66 |
def process_pdf_and_generate_response(pdf_file):
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
return response
|
86 |
|
87 |
iface = gr.Interface(
|
88 |
fn=process_pdf_and_generate_response,
|
89 |
-
inputs=gr.File(label="Upload PDF", type="
|
90 |
outputs=gr.Textbox("Placeholder for PDF insights", label="Insights", type="text"),
|
91 |
title="pdf-chatbot",
|
92 |
description="Upload a PDF and receive insights based on its content.",
|
93 |
-
css=css
|
94 |
)
|
95 |
|
96 |
-
|
97 |
if __name__ == "__main__":
|
98 |
iface.launch()
|
|
|
|
5 |
from transformers import AutoTokenizer, AutoModel
|
6 |
import torch
|
7 |
from pdf2image import convert_from_path
|
8 |
+
import io
|
9 |
|
10 |
# CSS styles
|
11 |
css = """
|
|
|
22 |
|
23 |
# Define layout with custom styles
|
24 |
layout = [
|
25 |
+
gr.Row([gr.File(label="Upload PDF", type="file")]),
|
26 |
+
gr.Row([gr.Button("Generate Insights")]),
|
27 |
gr.Row([gr.Textbox("Placeholder for PDF insights", label="Insights", type="text")])
|
28 |
]
|
29 |
|
|
|
|
|
30 |
# Function to get image embeddings using ViT
|
31 |
def get_image_embeddings(image_path, model_name='google/vit-base-patch16-224'):
|
32 |
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
|
|
63 |
|
64 |
# Function to process PDF and generate a response
|
65 |
def process_pdf_and_generate_response(pdf_file):
|
66 |
+
try:
|
67 |
+
# Save the uploaded PDF to a temporary file
|
68 |
+
tmp_pdf_path = "/tmp/uploaded_file.pdf"
|
69 |
+
with open(tmp_pdf_path, 'wb') as tmp_pdf:
|
70 |
+
tmp_pdf.write(pdf_file.read())
|
71 |
+
|
72 |
+
# Convert PDF to images
|
73 |
+
img_dir = "pdf_images"
|
74 |
+
pdf_to_images(tmp_pdf_path, img_dir)
|
75 |
+
|
76 |
+
# Generate embeddings for each image
|
77 |
+
image_embeddings = []
|
78 |
+
for filename in os.listdir(img_dir):
|
79 |
+
if filename.endswith(".png"):
|
80 |
+
image_path = os.path.join(img_dir, filename)
|
81 |
+
image_embeddings.append(get_image_embeddings(image_path))
|
82 |
+
|
83 |
+
# Perform some text analysis on the PDF content (replace with your logic)
|
84 |
+
pdf_text = "PDF content analysis placeholder"
|
85 |
+
text_embeddings = get_text_embeddings(pdf_text)
|
86 |
+
|
87 |
+
# Combine image and text embeddings and generate a response (replace with your logic)
|
88 |
+
combined_embeddings = torch.cat([*image_embeddings, text_embeddings], dim=0)
|
89 |
+
response = "Response based on the processed PDF"
|
90 |
+
except Exception as e:
|
91 |
+
response = f"An error occurred: {str(e)}"
|
92 |
return response
|
93 |
|
94 |
iface = gr.Interface(
|
95 |
fn=process_pdf_and_generate_response,
|
96 |
+
inputs=gr.File(label="Upload PDF", type="file"),
|
97 |
outputs=gr.Textbox("Placeholder for PDF insights", label="Insights", type="text"),
|
98 |
title="pdf-chatbot",
|
99 |
description="Upload a PDF and receive insights based on its content.",
|
100 |
+
css=css
|
101 |
)
|
102 |
|
|
|
103 |
if __name__ == "__main__":
|
104 |
iface.launch()
|
105 |
+
|