pdf reader added
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from io import BytesIO
|
|
10 |
import re
|
11 |
from pathlib import Path
|
12 |
import openai
|
|
|
13 |
|
14 |
## utilty functions
|
15 |
def is_image_extension(filename: str) -> bool:
|
@@ -21,25 +22,28 @@ def load_file(path: str) -> list | dict:
|
|
21 |
"""Based on the file extension, load the file into a suitable object."""
|
22 |
|
23 |
image = None
|
24 |
-
excel = None
|
25 |
-
csv = None
|
26 |
text = None
|
27 |
ext = Path(path).suffix.lower() # same as os.path.splitext(filename)[1].lower()
|
28 |
|
29 |
if ext.endswith(".png") or ext.endswith(".jpg") or ext.endswith(".jpeg"):
|
30 |
image = Image.open(path).convert("RGB") # pillow object
|
31 |
elif ext.endswith(".xlsx") or ext.endswith(".xls"):
|
32 |
-
|
33 |
elif ext.endswith(".csv"):
|
34 |
-
|
|
|
|
|
|
|
35 |
elif ext.endswith(".py") or ext.endswith(".txt"):
|
36 |
with open(path, 'r') as f:
|
37 |
text = f.read() # plain text str
|
38 |
|
39 |
if image is not None:
|
40 |
return [image]
|
|
|
|
|
41 |
else:
|
42 |
-
return {"
|
43 |
|
44 |
|
45 |
## tools definition
|
@@ -68,8 +72,8 @@ def download_images(image_urls: str) -> list:
|
|
68 |
print(f"Failed to download from {url}: {e}")
|
69 |
return images
|
70 |
|
71 |
-
@tool
|
72 |
-
def transcribe_audio(audio_path: str) -> str:
|
73 |
"""
|
74 |
Transcribe audio file using OpenAI Whisper API.
|
75 |
Args:
|
|
|
10 |
import re
|
11 |
from pathlib import Path
|
12 |
import openai
|
13 |
+
import pdfplumber
|
14 |
|
15 |
## utilty functions
|
16 |
def is_image_extension(filename: str) -> bool:
|
|
|
22 |
"""Based on the file extension, load the file into a suitable object."""
|
23 |
|
24 |
image = None
|
|
|
|
|
25 |
text = None
|
26 |
ext = Path(path).suffix.lower() # same as os.path.splitext(filename)[1].lower()
|
27 |
|
28 |
if ext.endswith(".png") or ext.endswith(".jpg") or ext.endswith(".jpeg"):
|
29 |
image = Image.open(path).convert("RGB") # pillow object
|
30 |
elif ext.endswith(".xlsx") or ext.endswith(".xls"):
|
31 |
+
text = pd.read_excel(path) # DataFrame
|
32 |
elif ext.endswith(".csv"):
|
33 |
+
text = pd.read_csv(path) # DataFrame
|
34 |
+
elif ext.endswith(".pdf"):
|
35 |
+
with pdfplumber.open(path) as pdf:
|
36 |
+
text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
|
37 |
elif ext.endswith(".py") or ext.endswith(".txt"):
|
38 |
with open(path, 'r') as f:
|
39 |
text = f.read() # plain text str
|
40 |
|
41 |
if image is not None:
|
42 |
return [image]
|
43 |
+
elif ext.endswith(".mp3") or ext.endswith(".wav"):
|
44 |
+
return {"raw document text": text, "audio path": path}
|
45 |
else:
|
46 |
+
return {"raw document text": text, "file path": path}
|
47 |
|
48 |
|
49 |
## tools definition
|
|
|
72 |
print(f"Failed to download from {url}: {e}")
|
73 |
return images
|
74 |
|
75 |
+
@tool # since they gave us OpenAI API credits, we can keep using it
|
76 |
+
def transcribe_audio(audio_path: str) -> str:
|
77 |
"""
|
78 |
Transcribe audio file using OpenAI Whisper API.
|
79 |
Args:
|