-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
63 lines (52 loc) · 2.09 KB
/
app2.py
File metadata and controls
63 lines (52 loc) · 2.09 KB
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
from dotenv import load_dotenv
import os
import streamlit as st
from PIL import Image
import google.generativeai as genai
# Load environment variables from .env file
load_dotenv()
# Configure the Gemini API with the key from the environment
# Set your Gemini API key here or via .env
GEMINI_API_KEY = ""
genai.configure(api_key=GEMINI_API_KEY)
# Load Gemini 1.5 Flash Vision model
model = genai.GenerativeModel("gemini-1.5-flash")
# Function to generate response from Gemini
def get_gemini_response(user_input, image, prompt):
response = model.generate_content([user_input, image[0], prompt])
return response.text # Return the actual text content
# Function to convert uploaded image to byte format required by Gemini API
def input_image_details(uploaded_file):
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
image_parts = [
{
"mime_type": uploaded_file.type,
"data": bytes_data
}
]
return image_parts
# Streamlit app setup
st.set_page_config(page_title="Multi-Language Invoice Extractor")
st.header("Multi-Language Invoice Extractor")
# File uploader for invoice image
uploaded_file = st.file_uploader("Upload an invoice image", type=["jpg", "jpeg", "png"])
#User input
user_input = st.text_input("Enter your question or prompt:", key="input")
# Display uploaded image
if uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Invoice", use_container_width=True)
# Default input prompt for the model
input_prompt = """You are an expert in understanding invoices.
We will upload an image of an invoice, and you will answer any questions
based on the uploaded invoice image."""
# Submit button
if st.button("Get response from invoice"):
if uploaded_file and user_input.strip():
image_data = input_image_details(uploaded_file)
response = get_gemini_response(user_input, image_data, input_prompt)
st.subheader("Extracted Information:")
st.write(response)
else:
st.warning("Please upload an invoice and enter a question.")