jayparmar0109 commited on
Commit
3ee0221
·
verified ·
1 Parent(s): be6a8b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -49
app.py CHANGED
@@ -1,71 +1,48 @@
1
- import streamlit as st
2
- import os
3
- import pathlib
4
- import textwrap
5
- from PIL import Image
6
 
7
  import google.generativeai as genai
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
19
- if uploaded_file is not None:
20
- # Read the file into bytes
21
  bytes_data = uploaded_file.getvalue()
22
-
23
  image_parts = [
24
  {
25
- "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
26
- "data": bytes_data
27
  }
28
  ]
29
  return image_parts
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 / "sample_images"
46
- sample_images = [sample_image.name for sample_image in sample_images_folder.glob("*.jpg") if sample_image.is_file()]
47
 
48
- sample_image_selected = st.sidebar.selectbox("Choose a sample image", sample_images)
49
-
50
- uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
51
-
52
- if uploaded_file is not None:
53
- if sample_image_selected:
54
- sample_image_path = sample_images_folder / sample_image_selected
55
- uploaded_file.name = sample_image_selected
56
- uploaded_file.seek(0)
57
- with open(sample_image_path, "wb") as f:
58
- f.write(uploaded_file.read())
59
- image = Image.open(sample_image_path)
60
- st.image(image, caption="Uploaded Image.", use_column_width=True)
61
- else:
62
- image = Image.open(uploaded_file)
63
- st.image(image, caption="Uploaded Image.", use_column_width=True)
64
 
65
- submit = st.button("Submit")
 
 
 
 
 
 
66
 
67
  if submit:
68
  image_data = input_image_setup(uploaded_file)
69
- response = get_gemini_response(input_prompt, image_data, input)
70
- st.subheader("Output :")
71
- st.write(response)
 
 
1
+ import streamlit as st
2
+ import os import pathlib
3
+ import textwrap from PIL
4
+ import Image
 
5
 
6
  import google.generativeai as genai
7
 
8
  genai.configure(api_key='AIzaSyCeNgXfZx0kJ736XFVtxXxev_RdscB0i5s')
9
 
10
+ Function to load OpenAI model and get respones
11
+ def get_gemini_response(input,image,prompt):
12
  model = genai.GenerativeModel('gemini-pro-vision')
13
+ response = model.generate_content([input,image[0],prompt])
14
  return response.text
15
 
16
+ def input_image_setup(uploaded_file): # Check if a file has been uploaded
17
+ if uploaded_file is not None: # Read the file into bytes
 
 
18
  bytes_data = uploaded_file.getvalue()
 
19
  image_parts = [
20
  {
21
+ "mime_type": uploaded_file.type, # Get the mime type of the uploaded file
22
+ "data": bytes_data
23
  }
24
  ]
25
  return image_parts
26
  else:
27
  raise FileNotFoundError("No file uploaded")
28
 
29
+ if uploaded_file is not None:
30
+ image = Image.open(uploaded_file)
31
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ submit=st.button("Submit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ input_prompt ="""
36
+ You are an expert in understanding business cards.
37
+ Input: Image of a business card.
38
+ Task: Extract and label the following information in JSON format:
39
+ Lagels : person_name, company_name, occupation, contact_number, email addresse, website, address, other_details (services, features, etc.)
40
+ Constraints: Do not include missing information.
41
+ """
42
 
43
  if submit:
44
  image_data = input_image_setup(uploaded_file)
45
+ if image_data is not None:
46
+ response = get_gemini_response(input_prompt, image_data, input_text)
47
+ st.subheader("Output :")
48
+ st.write(response)