Update app.py
Browse files
app.py
CHANGED
@@ -8,13 +8,11 @@ import google.generativeai as genai
|
|
8 |
|
9 |
genai.configure(api_key='AIzaSyCeNgXfZx0kJ736XFVtxXxev_RdscB0i5s')
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
def get_gemini_response(input,image,prompt):
|
14 |
model = genai.GenerativeModel('gemini-pro-vision')
|
15 |
-
response = model.generate_content([input,image[0],prompt])
|
16 |
return response.text
|
17 |
-
|
18 |
|
19 |
def input_image_setup(uploaded_file):
|
20 |
# Check if a file has been uploaded
|
@@ -32,43 +30,37 @@ def input_image_setup(uploaded_file):
|
|
32 |
else:
|
33 |
raise FileNotFoundError("No file uploaded")
|
34 |
|
35 |
-
|
36 |
-
##initialize our streamlit app
|
37 |
-
|
38 |
st.set_page_config(page_title="Gemini Image Demo")
|
39 |
|
40 |
st.header("Generative AI : Business card Reader")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Display sample input images
|
42 |
sample_images_folder = pathlib.Path(__file__).parent / "examples"
|
43 |
-
sample_images = [sample_image for sample_image in sample_images_folder.glob("*.jpg") if sample_image.is_file()]
|
|
|
|
|
44 |
|
45 |
-
if
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
input=st.text_input("Input Prompt: ",key="input")
|
52 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
53 |
-
image=""
|
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 |
-
|
59 |
-
submit=st.button("Submit")
|
60 |
-
|
61 |
-
input_prompt = """
|
62 |
-
You are an expert in understanding business cards.
|
63 |
-
You will receive input images of business card & you will have to answer questions based on the input image.
|
64 |
-
You have to extract information from business card images and give correct tag to the output text
|
65 |
-
like person name, company name, occupation, address, telephone number, mobile number, email, website, etc. Give output in json format.
|
66 |
-
"""
|
67 |
-
|
68 |
-
## If ask button is clicked
|
69 |
|
70 |
if submit:
|
71 |
image_data = input_image_setup(uploaded_file)
|
72 |
-
response=get_gemini_response(input_prompt,image_data,input)
|
73 |
st.subheader("Output :")
|
74 |
st.write(response)
|
|
|
8 |
|
9 |
genai.configure(api_key='AIzaSyCeNgXfZx0kJ736XFVtxXxev_RdscB0i5s')
|
10 |
|
11 |
+
# Function to load OpenAI model and get responses
|
12 |
+
def get_gemini_response(input, image, prompt):
|
|
|
13 |
model = genai.GenerativeModel('gemini-pro-vision')
|
14 |
+
response = model.generate_content([input, image[0], prompt])
|
15 |
return response.text
|
|
|
16 |
|
17 |
def input_image_setup(uploaded_file):
|
18 |
# Check if a file has been uploaded
|
|
|
30 |
else:
|
31 |
raise FileNotFoundError("No file uploaded")
|
32 |
|
33 |
+
# Initialize our Streamlit app
|
|
|
|
|
34 |
st.set_page_config(page_title="Gemini Image Demo")
|
35 |
|
36 |
st.header("Generative AI : Business card Reader")
|
37 |
+
input_prompt = textwrap.dedent("""
|
38 |
+
You are an expert in understanding business cards.
|
39 |
+
You will receive input images of business card & you will have to answer questions based on the input image.
|
40 |
+
You have to extract information from business card images and give correct tag to the output text
|
41 |
+
like person name, company name, occupation, address, telephone number, mobile number, email, website, etc. Give output in json format.
|
42 |
+
""")
|
43 |
+
|
44 |
# Display sample input images
|
45 |
sample_images_folder = pathlib.Path(__file__).parent / "examples"
|
46 |
+
sample_images = [sample_image.name for sample_image in sample_images_folder.glob("*.jpg") if sample_image.is_file()]
|
47 |
+
|
48 |
+
sample_image_uploaded = st.sidebar.file_uploader("Choose a sample image", type=["jpg", "jpeg"], options=sample_images)
|
49 |
|
50 |
+
if sample_image_uploaded:
|
51 |
+
uploaded_file = sample_image_uploaded
|
52 |
+
else:
|
53 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
54 |
+
|
55 |
+
image = ""
|
|
|
|
|
|
|
56 |
if uploaded_file is not None:
|
57 |
image = Image.open(uploaded_file)
|
58 |
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
59 |
|
60 |
+
submit = st.button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
if submit:
|
63 |
image_data = input_image_setup(uploaded_file)
|
64 |
+
response = get_gemini_response(input_prompt, image_data, input)
|
65 |
st.subheader("Output :")
|
66 |
st.write(response)
|