Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
genai.configure(api_key='AIzaSyCeNgXfZx0kJ736XFVtxXxev_RdscB0i5s')
|
11 |
+
|
12 |
+
## Function to load OpenAI model and get respones
|
13 |
+
|
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 |
+
|
20 |
+
def input_image_setup(uploaded_file):
|
21 |
+
# Check if a file has been uploaded
|
22 |
+
if uploaded_file is not None:
|
23 |
+
# Read the file into bytes
|
24 |
+
bytes_data = uploaded_file.getvalue()
|
25 |
+
|
26 |
+
image_parts = [
|
27 |
+
{
|
28 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
29 |
+
"data": bytes_data
|
30 |
+
}
|
31 |
+
]
|
32 |
+
return image_parts
|
33 |
+
else:
|
34 |
+
raise FileNotFoundError("No file uploaded")
|
35 |
+
|
36 |
+
|
37 |
+
##initialize our streamlit app
|
38 |
+
|
39 |
+
st.set_page_config(page_title="Gemini Image Demo")
|
40 |
+
|
41 |
+
st.header("Generative AI : Invoice Reader")
|
42 |
+
input=st.text_input("Input Prompt: ",key="input")
|
43 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
44 |
+
image=""
|
45 |
+
if uploaded_file is not None:
|
46 |
+
image = Image.open(uploaded_file)
|
47 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
48 |
+
|
49 |
+
|
50 |
+
submit=st.button("Tell me about the image")
|
51 |
+
|
52 |
+
input_prompt = """
|
53 |
+
You are an expert in understanding business cards.
|
54 |
+
You will receive input images as business card & you will have to answer questions based on the input image.
|
55 |
+
You have to extract information from business card images and give correct tag to the output text
|
56 |
+
like person name, company name, occupation, address, phone/telephone number, email, website, etc.
|
57 |
+
"""
|
58 |
+
|
59 |
+
## If ask button is clicked
|
60 |
+
|
61 |
+
if submit:
|
62 |
+
image_data = input_image_setup(uploaded_file)
|
63 |
+
response=get_gemini_response(input_prompt,image_data,input)
|
64 |
+
st.subheader("The Response is")
|
65 |
+
st.write(response)
|