Error is converting result to subscriptable?
Error converting PDF to images: 'Result' object is not subscriptable
Traceback:File "C:\Users\arbaz\Documents\MultiLangModel\venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 534, in _run_script exec(code, module.__dict__)File "C:\Users\arbaz\Documents\MultiLangModel\app2.py", line 54, in <module> response2 = get_gemini_response_from_pdf(input_prompt2, pdf_images, input2) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\arbaz\Documents\MultiLangModel\app2.py", line 14, in get_gemini_response_from_pdf response2 = model2.generate_content([input_prompt, pdf_images[0], input2]) ~~~~~~~~~~
This is the code:
from dotenv import load_dotenvimport streamlit as stfrom PIL import Imageimport google.generativeai as genaiimport convertapiimport osimport ioimport tempfileconvertapi.api_secret = 'Secret Key'genai.configure(api_key='Secret Key')def get_gemini_response_from_pdf(input_prompt, pdf_images, input2): model2 = genai.GenerativeModel('gemini-pro-version') response2 = model2.generate_content([input_prompt, pdf_images[0], input2]) return response2.textdef convert_pdf_to_images(uploaded_file): try: temp_file = f"tmp{uploaded_file.name}" with open(temp_file, 'wb') as file: file.write(uploaded_file.read()) result = convertapi.convert('jpg', {'File': temp_file}, from_format='pdf') if result['response']['status'] == 'Ok': return result['files'] else: st.write(f"Conversion failed: {result['response']['message']}") return [] except Exception as e: st.write(f"Error converting PDF to images: {e}") return []st.set_page_config(page_title="Document Reader using AI")st.header("This is my document reader using AI")input_prompt2 = """You are an expert in understanding documents such as passport, bank statements, and financial investments into assets.You will receive input pdf of passport, bank statements, invoices, and financial investments into assets &you will have to answer questions based on the input image"""input2 = st.text_input("Input Prompt for PDF Document: ", key="input2")uploaded_file2 = st.file_uploader("Upload a PDF file", type="pdf")pdf_images = []if uploaded_file2 is not None: pdf_images = convert_pdf_to_images(uploaded_file2) for img_data in pdf_images: img_bytes = img_data['data'] image = Image.open(io.BytesIO(img_bytes)) st.image(image, caption="Converted Image", use_column_width=True) submit_pdf = st.button("Generate Pdf Response") if submit_pdf: response2 = get_gemini_response_from_pdf(input_prompt2, pdf_images, input2) st.subheader("The Response is") st.write(response2)
Why this error?
How do I turn this logic with convert API and streamlit?
Code works when I upload image but when I do with PDF it either breaks or it gets into infinite loop of processing.