File size: 1,738 Bytes
b72363d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from PIL import Image
import google.generativeai as genai # type: ignore
import streamlit as st
from dotenv import load_dotenv
import os
load_dotenv()

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

# load gemini pro vision
model = genai.GenerativeModel("gemini-1.5-flash")

def input_image_details(upload_file):
    if upload_file is not None:
        # read the file into bytes
        bytes_data= upload_file.getvalue()
        
        image_parts = [
            {
                "mime_type":upload_file.type,
                "data":bytes_data
            }
            
        ]
        return image_parts
    else:
        raise FileNotFoundError("No file uploaded")
    

def get_gemni_response(prompt, image,user_input):
    response = model.generate_content([prompt, image[0], user_input])
    return response.text


#initilize our streamlite app
st.set_page_config(page_title="Multilanguage Invoice Extractor ")
st.header("MultiLanguage Invoice Extractor")
user_input = st.text_input("Input Prompt", key="input")
upload_file = st.file_uploader("choose an image of the invoice..", type=["jpg","jpeg","png"])

# to show the uploaded image
if upload_file is not None:
    image = Image.open(upload_file)
    st.image(image, caption="upload Image.", use_column_width=True)
    
    
submit = st.button("Tell me about the Invice")

prompt = """
Your are an expert in understanding invoice. We will upload a image as invoice 
and you will have to answer any questions based on the upload invoice image
"""


# if submit buttion is clicked
if submit:
    image_data = input_image_details(upload_file)
    response = get_gemni_response(prompt, image_data, user_input)
    st.subheader("The response is")
    st.write(response)