File size: 3,048 Bytes
cf28f91
 
 
 
 
 
 
 
437210c
cf28f91
b32bf68
cf28f91
b32bf68
cf28f91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb5b998
332ced6
b32bf68
 
cf28f91
332ced6
fe4eda0
 
 
 
 
 
9e58f33
 
cf28f91
 
 
 
eb5b998
cf28f91
eb5b998
cf28f91
 
 
 
 
 
 
 
 
 
05f8939
cf28f91
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import os
import google.generativeai as genai
from PIL import Image
import pdfplumber
import io
import streamlit_pills as sp

# Configure API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Load gemini model
model = genai.GenerativeModel("gemini-1.5-flash")

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

def input_image_setup(uploaded_img):
    if uploaded_img is not None:
        bytes_data = uploaded_img.getvalue()
        image_parts = [
            {
                "mime_type": uploaded_img.type,
                "data": bytes_data
            }
        ]
        return image_parts
    else:
        raise FileNotFoundError("Image not found")

def extract_text_from_pdf(pdf_file):
    text = ""
    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            text += page.extract_text()
    return text

def input_file_setup(uploaded_file):
    if uploaded_file is not None:
        if uploaded_file.type.startswith('image'):
            bytes_data = uploaded_file.getvalue()
            image_parts = [
                {
                    "mime_type": uploaded_file.type,
                    "data": bytes_data
                }
            ]
            return image_parts
        elif uploaded_file.type.startswith('application/pdf'):
            text = extract_text_from_pdf(uploaded_file)
            return [{"text": text}]
        else:
            raise ValueError("Unsupported file type")
    else:
        raise FileNotFoundError("File not found")

st.set_page_config(page_title="Invoice Information Extractor", page_icon="🔮")
st.title("Invoice Information Extractor 📃")

# Display sample prompts

uploaded_file = st.file_uploader("Upload an Invoice Image 🖼️ or PDF 📄", type=["jpg", "jpeg", "png", "pdf"])

selected = sp.pills(" Ask a question related to the invoice. Here are some example prompts:", 
                        ["What is the total amount?",
                         'What are the Customter details?' , 
                         'What is the information about the Products?'],
                        clearable=True,index=None)

input_query = selected if selected else st.text_input("Ask a question about the invoice", key="input")

if uploaded_file is not None:
    if uploaded_file.type.startswith('image'):
        image = Image.open(uploaded_file)
        st.image(image, caption="Image uploaded.", use_column_width=True)
    elif uploaded_file.type.startswith('application/pdf'):
        st.write("PDF uploaded.")

submit = st.button("Submit")

input_prompt = """
You are an expert in understanding invoices. We will show you an invoice and you have to answer the following questions based on the invoice:
"""

if submit:
    file_data = input_file_setup(uploaded_file)
    response = get_gemini_response(input_prompt, file_data, input_query)
    st.subheader("Response🤖:")
    st.write(response)