pc-17 commited on
Commit
4e97c67
1 Parent(s): 933a3ef

upload app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+
3
+ load_dotenv() # take environment variables from .env.
4
+
5
+ import streamlit as st
6
+ import os
7
+ from PIL import Image
8
+ import google.generativeai as genai
9
+
10
+ os.getenv("GOOGLE_API_KEY")
11
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
12
+
13
+ # Function to load OpenAI model and get response
14
+ def get_gemini_response(input, image, prompt):
15
+ model = genai.GenerativeModel('gemini-pro-vision')
16
+ response = model.generate_content([input, image[0], prompt])
17
+ return response.text
18
+
19
+ # Function to format numeric value as currency
20
+ def format_currency(value, currency_symbol='$'):
21
+ return f"{currency_symbol}{value:,.2f}"
22
+
23
+ # Converting image into bytes
24
+ def input_image_setup(uploaded_file):
25
+ # Check if a file has been uploaded
26
+ if uploaded_file is not None:
27
+ # Read the file into bytes
28
+ bytes_data = uploaded_file.getvalue()
29
+
30
+ image_parts = [
31
+ {
32
+ "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
33
+ "data": bytes_data
34
+ }
35
+ ]
36
+ return image_parts
37
+ else:
38
+ raise FileNotFoundError("No file uploaded")
39
+
40
+ # Initialize our streamlit app
41
+ st.set_page_config(page_title="Gemini Image Demo")
42
+ st.header("Gemini Application")
43
+
44
+ input_prompt = """
45
+ You are an expert in understanding invoices.
46
+ You will receive input images as invoices &
47
+ you will have to answer questions based on the input image
48
+ """
49
+
50
+ input_text = st.text_input("Input Prompt: ", key="input")
51
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
52
+ image = ""
53
+
54
+ if uploaded_file is not None:
55
+ image = Image.open(uploaded_file)
56
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
57
+
58
+ submit = st.button("Tell me about the image")
59
+
60
+ # If ask button is clicked
61
+ if submit:
62
+ try:
63
+ image_data = input_image_setup(uploaded_file)
64
+ response = get_gemini_response(input_prompt, image_data, input_text)
65
+
66
+ # Print the raw response
67
+ st.subheader("Raw Response:")
68
+ st.text(response)
69
+
70
+ # Check if the response can be converted to a numeric value
71
+ if response.replace('.', '', 1).isdigit():
72
+ numeric_response = float(response)
73
+ formatted_response = format_currency(numeric_response, currency_symbol='£')
74
+ st.subheader("The Response is")
75
+ st.write(formatted_response)
76
+ else:
77
+ st.warning("Warning: The response is not a numeric value. Displaying raw response.")
78
+
79
+ except Exception as e:
80
+ st.error(f"Error: {str(e)}")