pratikshahp commited on
Commit
55726a8
·
verified ·
1 Parent(s): f37f3c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
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, uploaded_file, query):
15
  try:
16
- # Check if the image is provided
17
- if uploaded_file is None:
18
- return "Please upload an image."
19
-
20
- # Prepare the image as bytes
21
- image_parts = [{"mime_type": "image/png", "data": uploaded_file}]
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="binary")
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