Expected str, bytes or os.PathLike object, not UploadedFile for PDF file

I am trying to extract text for a PDF by converting into image where my PDF is the input file.
In streamlit when I am trying to import the PDF and call the function

    uploaded_file = st.file_uploader('Import PDF from local', type='pdf')
    if uploaded_file is not None:
        text = pic_to_text(uploaded_file)

I am getting an error expected str, bytes or os.PathLike object, not UploadedFile
I am not wanting to extract the pages or text from PDF but directly pass PDF as an input to my function.

Hey @Saksham,

First, welcome to the Streamlit Community!!! :partying_face: :partying_face: :tada: :tada: :balloon: :star2:

Can you post a link to your code? Also, you’re passing your uploaded file into a function that I cannot see, I imagine that somewhere in that function your passing the uploaded file directly into something that was expected “str”, “bytes” or a path.

Check out this discussion where someone has a similar issue (docs typo already fixed):

Happy Streamlit-ing!

1 Like

Hi @Marisa_Smith ,

Thank you so much for your response. Please find the code below where I am trying to pass the pdf directly.

def pic_to_text(infile):
infile = read_pdf(infile) # Returns memory view
os.environ[“GOOGLE_APPLICATION_CREDENTIALS”] = “servicekey.json”

"""Detects text in an image file

infile: path to image file

String of text detected in image

# Instantiates a client
client = vision.ImageAnnotatorClient()

# Opens the input image file
content = infile.tobytes()

image = vision.Image(content=content)

# For dense text, use document_text_detection
# For less dense text, use text_detection
response = client.text_detection(image=image, image_context={"language_hints": ["en"]})
text = response.text_annotations[0].description
# print("Detected text: {}".format(text))
return text

def translate_text(text, source_language_code, target_language_code):

“”"Translates text to a given language using a glossary

text: String of text to translate
source_language_code: language of input text
target_language_code: language of output text
project_id: GCP project id
glossary_name: name you gave your project's glossary
    resource when you created it

String of translated text

# Instantiates a client
client = translate.TranslationServiceClient()

# Designates the data center location that you want to use
location = "us-central1"
project_id = "testprojectincloud"

parent = f"projects/{project_id}/locations/{location}"

result = client.translate_text(request={"parent": parent,
                                        "contents": [text],
                                        "mime_type": "text/plain",  # mime types: text/plain, text/html
                                        "source_language_code": source_language_code,
                                        "target_language_code": target_language_code

# Extract translated text from API response
return result.translations

In the code above I am passing the pdf file,converting the pdf to image and getting the memory value, converting it into bytes and passing it to google cloud api to extract the text from image.