from dotenv import load_dotenv
load_dotenv()                                             # Load the all envirement variable from .env

import streamlit as st
import os
from PIL import Image


import google.generativeai as genai
genai.configure(api_key=os.getenv("google_api_key"))
model=genai.GenerativeModel('gemini-1.5-flash')           # Load gemini pero version Model

def get_gemini_response(input,image,user_prompt):
    response=model.generate_content([input,image[0],user_prompt])
    return response.text

def input_image_details(uploaded_file):
    if uploaded_file is not None:
        bytes_data=uploaded_file.getvalue()               # Read the files into bytes
        image_parts=[
            {
                "mime_type":uploaded_file.type,           # get th mime type of the uploaded file
                "data":bytes_data
            }
        ]
        return image_parts
    else:
        raise FileNotFoundError("No file uploaded")
    

# Initialize our streamlit app

st.set_page_config(page_title='Multilanguage Invoice Extractor')

st.header('Multilanguage Invoice Extractor')
input=st.text_input("input  prompt:",key="input")

uploaded_file=st.file_uploader("Chose an image of the Invoice....",type=["jpg","jpeg","png"])

if uploaded_file is not None:
    image=Image.open(uploaded_file)
    st.image(image,caption="uploaded image.",use_column_width=True)


input_prompt="""
You are an expert in understanding invoices. We upload a image as invoice
and you will have to answer any quetions based on the uploaded invoice image 
"""    

submit=st.button('Tell me about the invoice')

if submit:
    image_data=input_image_details(uploaded_file)
    response=get_gemini_response(input_prompt,image_data,input)
    st.subheader("The Response is")
    st.write(response)