Spaces:
Sleeping
Sleeping
Save changes
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
import base64
|
3 |
import io
|
4 |
from io import BytesIO
|
|
|
|
|
5 |
|
6 |
import streamlit as st
|
7 |
from PIL import Image
|
@@ -14,20 +16,18 @@ api_key = os.getenv("OPENAI_API_KEY")
|
|
14 |
|
15 |
client = OpenAI(api_key=api_key)
|
16 |
|
17 |
-
def extract_text_and_images_from_pdf(
|
18 |
try:
|
19 |
text_content = ""
|
20 |
image_urls = []
|
21 |
|
22 |
-
pdf_stream = BytesIO(pdf_file.read())
|
23 |
-
|
24 |
# Extract text using PdfReader
|
25 |
-
pdf_reader = PdfReader(
|
26 |
for page in pdf_reader.pages:
|
27 |
text_content += page.extract_text()
|
28 |
|
29 |
# Extract images using PyMuPDF
|
30 |
-
doc = fitz.open(
|
31 |
for page_index in range(len(doc)):
|
32 |
page = doc.load_page(page_index)
|
33 |
image_list = page.get_images()
|
@@ -53,16 +53,29 @@ def extract_text_and_images_from_pdf(pdf_file):
|
|
53 |
|
54 |
def generate_ai_response(text_content, image_urls, text_prompt):
|
55 |
try:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
response = client.chat.completions.create(
|
68 |
model="gpt-4o-mini",
|
@@ -92,7 +105,13 @@ def main():
|
|
92 |
|
93 |
uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
|
94 |
if uploaded_pdf is not None:
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
st.subheader("Extracted Text")
|
98 |
st.text(text_content)
|
@@ -110,5 +129,8 @@ def main():
|
|
110 |
st.success("Response generated!")
|
111 |
st.markdown(f"AI Response: {ai_response}")
|
112 |
|
|
|
|
|
|
|
113 |
if __name__ == "__main__":
|
114 |
main()
|
|
|
2 |
import base64
|
3 |
import io
|
4 |
from io import BytesIO
|
5 |
+
import tempfile
|
6 |
+
import shutil
|
7 |
|
8 |
import streamlit as st
|
9 |
from PIL import Image
|
|
|
16 |
|
17 |
client = OpenAI(api_key=api_key)
|
18 |
|
19 |
+
def extract_text_and_images_from_pdf(pdf_file_path):
|
20 |
try:
|
21 |
text_content = ""
|
22 |
image_urls = []
|
23 |
|
|
|
|
|
24 |
# Extract text using PdfReader
|
25 |
+
pdf_reader = PdfReader(pdf_file_path)
|
26 |
for page in pdf_reader.pages:
|
27 |
text_content += page.extract_text()
|
28 |
|
29 |
# Extract images using PyMuPDF
|
30 |
+
doc = fitz.open(pdf_file_path)
|
31 |
for page_index in range(len(doc)):
|
32 |
page = doc.load_page(page_index)
|
33 |
image_list = page.get_images()
|
|
|
53 |
|
54 |
def generate_ai_response(text_content, image_urls, text_prompt):
|
55 |
try:
|
56 |
+
|
57 |
+
if len(image_urls) > 0:
|
58 |
+
# Construct the messages list with the prompt and base64-encoded image URLs
|
59 |
+
messages = [
|
60 |
+
{
|
61 |
+
"role": "user",
|
62 |
+
"content": [
|
63 |
+
{"type": "text", "text": f"Perform this task {text_prompt} on the images:"},
|
64 |
+
*[{"type": "image_url", "image_url": {"url": url}} for url in image_urls]
|
65 |
+
]
|
66 |
+
}
|
67 |
+
]
|
68 |
+
|
69 |
+
else:
|
70 |
+
# Construct the prompt on the extracted text only
|
71 |
+
messages = [
|
72 |
+
{
|
73 |
+
"role": "user",
|
74 |
+
"content": [
|
75 |
+
{"type": "text", "text": f"Perform this task {text_prompt} on this text {text_content}"}
|
76 |
+
]
|
77 |
+
}
|
78 |
+
]
|
79 |
|
80 |
response = client.chat.completions.create(
|
81 |
model="gpt-4o-mini",
|
|
|
105 |
|
106 |
uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
|
107 |
if uploaded_pdf is not None:
|
108 |
+
# Save the uploaded PDF to a temporary directory
|
109 |
+
temp_dir = tempfile.mkdtemp()
|
110 |
+
pdf_file_path = os.path.join(temp_dir, uploaded_pdf.name)
|
111 |
+
with open(pdf_file_path, "wb") as f:
|
112 |
+
f.write(uploaded_pdf.getvalue())
|
113 |
+
|
114 |
+
text_content, image_urls = extract_text_and_images_from_pdf(pdf_file_path)
|
115 |
|
116 |
st.subheader("Extracted Text")
|
117 |
st.text(text_content)
|
|
|
129 |
st.success("Response generated!")
|
130 |
st.markdown(f"AI Response: {ai_response}")
|
131 |
|
132 |
+
# Clean up the temporary directory
|
133 |
+
shutil.rmtree(temp_dir)
|
134 |
+
|
135 |
if __name__ == "__main__":
|
136 |
main()
|