Update app.py
Browse files
app.py
CHANGED
|
@@ -11,15 +11,20 @@ load_dotenv()
|
|
| 11 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 12 |
|
| 13 |
# Function to process the image and get response from Gemini model
|
| 14 |
-
def get_gemini_response(input_prompt,
|
| 15 |
try:
|
| 16 |
-
#
|
| 17 |
-
if
|
| 18 |
-
return "Please upload
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Load the Gemini model and get the response
|
| 24 |
model = genai.GenerativeModel("gemini-pro-vision")
|
| 25 |
response = model.generate_content([input_prompt, image_parts[0], query])
|
|
@@ -42,9 +47,9 @@ with gr.Blocks() as invoice_extractor:
|
|
| 42 |
The system uses Google's Gemini model to extract and interpret the invoice details.
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
-
|
| 46 |
input_prompt = gr.Textbox(label="Input Prompt", value=default_prompt, lines=3)
|
| 47 |
-
image_input = gr.Image(label="Upload Invoice Image", type="
|
| 48 |
query_input = gr.Textbox(label="Enter your query about the invoice", placeholder="e.g., What is the total amount?")
|
| 49 |
output_response = gr.Textbox(label="Response", lines=5)
|
| 50 |
|
|
|
|
| 11 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 12 |
|
| 13 |
# Function to process the image and get response from Gemini model
|
| 14 |
+
def get_gemini_response(input_prompt, uploaded_file_path, query):
|
| 15 |
try:
|
| 16 |
+
# Validate the image file path
|
| 17 |
+
if not uploaded_file_path or not os.path.exists(uploaded_file_path):
|
| 18 |
+
return "Please upload a valid image."
|
| 19 |
+
|
| 20 |
+
# Read the image file as binary data
|
| 21 |
+
with open(uploaded_file_path, "rb") as f:
|
| 22 |
+
image_data = f.read()
|
| 23 |
+
|
| 24 |
+
# Prepare the image parts for the model
|
| 25 |
+
mime_type = f"image/{uploaded_file_path.split('.')[-1]}" # Dynamically detect mime type
|
| 26 |
+
image_parts = [{"mime_type": mime_type, "data": image_data}]
|
| 27 |
+
|
| 28 |
# Load the Gemini model and get the response
|
| 29 |
model = genai.GenerativeModel("gemini-pro-vision")
|
| 30 |
response = model.generate_content([input_prompt, image_parts[0], query])
|
|
|
|
| 47 |
The system uses Google's Gemini model to extract and interpret the invoice details.
|
| 48 |
"""
|
| 49 |
)
|
| 50 |
+
|
| 51 |
input_prompt = gr.Textbox(label="Input Prompt", value=default_prompt, lines=3)
|
| 52 |
+
image_input = gr.Image(label="Upload Invoice Image", type="filepath") # Use type="filepath"
|
| 53 |
query_input = gr.Textbox(label="Enter your query about the invoice", placeholder="e.g., What is the total amount?")
|
| 54 |
output_response = gr.Textbox(label="Response", lines=5)
|
| 55 |
|