Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16951

Trouble trying to convert and shows problem Streamlit throwing error list index out of range

$
0
0

This is what am facing:Trouble with converting result to subscriptable? Error converting PDF to images: 'Result' object is not subscriptable I also tried different libraries like PDF2IMG and it was getting stuck on processingWhat do you suggest best edit for this I am trying to convert pdf to image then process it and get query answers type chatting based

 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])~~~~~~~~~~ 
 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)

Viewing all articles
Browse latest Browse all 16951

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>