msaid1976 commited on
Commit
12629c1
·
verified ·
1 Parent(s): 75ef796

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import pathlib
4
+ import textwrap
5
+ from PIL import Image
6
+
7
+
8
+ import google.generativeai as genai
9
+
10
+
11
+ os.getenv("GOOGLE_API_KEY")
12
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
13
+
14
+ # function to load gemini Pro model
15
+
16
+
17
+ def get_gemini_response(input, image, prompt):
18
+ model = genai.GenerativeModel('gemini-pro-vision')
19
+ response = model.generate_content([input, image[0], prompt])
20
+ return response.text
21
+
22
+
23
+ def input_image_details(uploaded_file):
24
+ if uploaded_file is not None:
25
+ # Read the file into bytes
26
+ bytes_data = uploaded_file.getvalue()
27
+
28
+ image_parts = [
29
+ {
30
+ # Get the mime type of the up]
31
+ "mime_type": uploaded_file.type,
32
+ "data": bytes_data
33
+ }
34
+ ]
35
+
36
+ return image_parts
37
+ else:
38
+ raise FileNotFoundError("No file uploaded")
39
+
40
+
41
+ # Initialize streamlit app
42
+ st.set_page_config(page_title="Data Extractor")
43
+
44
+ st.header("Data Extractor")
45
+ input = st.text_input("Input Prompt: ", key="input")
46
+
47
+ uploaded_file = st.file_uploader(
48
+ "Choose an image of ... ", type=["jpg", "jpeg", "png"])
49
+ image = ""
50
+
51
+ if uploaded_file is not None:
52
+ image = Image.open(uploaded_file)
53
+ st.image(image, caption="Uploaded image.", use_column_width=True)
54
+
55
+
56
+ submit = st.button("Submit")
57
+
58
+ input_prompt = """
59
+ You are an expert in understanding financial reports. We will upload a a image as financial statement
60
+ and you will have to answer any questions based on the uploaded invoice image
61
+ """
62
+
63
+ # If submit button clicked
64
+ if submit:
65
+ image_data = input_image_details(uploaded_file)
66
+ reponse = get_gemini_response(
67
+ input_prompt, image_data, input)
68
+ st.subheader("The response is")
69
+ st.write(reponse)