@@ -1,7 +1,7 @@
from .functions_utils import requires
from .functions_utils import requires_python_packages, requires_secret
@requires("pdfminer.six", "requests")
@requires_python_packages ("pdfminer.six", "requests")
def read_text_from_pdf(file_path: str) -> str:
def read_text_from_pdf(file_path: str) -> str:
"""
"""
Reads text from a PDF file and returns it as a string.
Reads text from a PDF file and returns it as a string.
@@ -39,7 +39,7 @@ def read_text_from_pdf(file_path: str) -> str:
return text
return text
@requires("python-docx")
@requires_python_packages ("python-docx")
def read_text_from_docx(file_path: str) -> str:
def read_text_from_docx(file_path: str) -> str:
"""
"""
Reads text from a DOCX file and returns it as a string.
Reads text from a DOCX file and returns it as a string.
@@ -59,7 +59,7 @@ def read_text_from_docx(file_path: str) -> str:
return text
return text
@requires("pillow", "requests", "easyocr")
@requires_python_packages ("pillow", "requests", "easyocr")
def read_text_from_image(file_path: str) -> str:
def read_text_from_image(file_path: str) -> str:
"""
"""
Reads text from an image file or URL and returns it as a string.
Reads text from an image file or URL and returns it as a string.
@@ -92,7 +92,7 @@ def read_text_from_image(file_path: str) -> str:
return text
return text
@requires("python-pptx")
@requires_python_packages ("python-pptx")
def read_text_from_pptx(file_path: str) -> str:
def read_text_from_pptx(file_path: str) -> str:
"""
"""
Reads text from a PowerPoint file and returns it as a string.
Reads text from a PowerPoint file and returns it as a string.
@@ -116,7 +116,7 @@ def read_text_from_pptx(file_path: str) -> str:
return text
return text
@requires("pandas")
@requires_python_packages ("pandas")
def read_text_from_xlsx(file_path: str) -> str:
def read_text_from_xlsx(file_path: str) -> str:
"""
"""
Reads text from an Excel file and returns it as a string.
Reads text from an Excel file and returns it as a string.
@@ -135,7 +135,7 @@ def read_text_from_xlsx(file_path: str) -> str:
return text
return text
@requires("SpeechRecognition", "requests")
@requires_python_packages ("SpeechRecognition", "requests")
def read_text_from_audio(file_path: str) -> str:
def read_text_from_audio(file_path: str) -> str:
"""
"""
Reads text from an audio file or a URL and returns it as a string.
Reads text from an audio file or a URL and returns it as a string.
@@ -166,3 +166,52 @@ def read_text_from_audio(file_path: str) -> str:
text = recognizer.recognize_google(audio)
text = recognizer.recognize_google(audio)
return text
return text
@requires_secret("OPENAI_API_KEY")
@requires_python_packages("openai")
def caption_image_using_gpt4v(file_path_or_url: str) -> str:
"""
Generates a caption for an image using the GPT-4 Vision model from OpenAI.
Args:
file_path_or_url (str): The path to the image file or the URL.
Returns:
str: The caption generated for the image.
"""
import os
import openai
from openai import OpenAI
caption = ""
openai.api_key = os.environ["OPENAI_API_KEY"]
client = OpenAI()
# check if the file_path_or_url is a URL
if file_path_or_url.startswith("http://") or file_path_or_url.startswith("https://"):
image_url = file_path_or_url
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
],
}
],
max_tokens=300,
)
caption = response.choices[0]
else:
caption = "Please provide a valid image URL"
return caption