ombhojane commited on
Commit
5d45da0
·
verified ·
1 Parent(s): 46fbc6c

Update utils/gemini_utils.py

Browse files
Files changed (1) hide show
  1. utils/gemini_utils.py +15 -25
utils/gemini_utils.py CHANGED
@@ -1,5 +1,7 @@
1
  import google.generativeai as genai
2
  import streamlit as st
 
 
3
 
4
  # Set up the API key
5
  GOOGLE_API_KEY = st.secrets["GOOGLE_API_KEY"]
@@ -14,37 +16,25 @@ generation_config = {
14
  }
15
 
16
  model = genai.GenerativeModel(
17
- model_name="gemini-1.5-pro",
18
  generation_config=generation_config,
19
  )
20
 
21
- def upload_to_gemini(image_bytes, mime_type="image/png"):
22
- """Uploads the given image bytes to Gemini."""
23
- try:
24
- file = genai.types.GenerativeContentBlob(data=image_bytes)
25
- return file
26
- except Exception as e:
27
- st.error(f"Error uploading file: {e}")
28
- return None
29
-
30
  def get_meme_template_description(image_bytes):
31
  """Get a description of the meme template from the image bytes."""
32
  try:
33
- file = upload_to_gemini(image_bytes)
34
- if file:
35
- prompt = """
36
- You are an AI specialized in describing meme templates. Please provide a detailed description of the meme template in this image. Include:
37
- 1. The name of the meme template (if it's a well-known one)
38
- 2. A description of the visual layout (number of panels, arrangement)
39
- 3. Key visual elements in each panel
40
- 4. The typical use or purpose of this meme template
41
- 5. Where text is usually placed in this template
42
 
43
- Format your response as a concise paragraph.
44
- """
45
- response = model.generate_content([file, prompt])
46
- return response.text
47
- else:
48
- return "Failed to upload image."
49
  except Exception as e:
50
  return f"Error processing image: {e}"
 
1
  import google.generativeai as genai
2
  import streamlit as st
3
+ from PIL import Image
4
+ import io
5
 
6
  # Set up the API key
7
  GOOGLE_API_KEY = st.secrets["GOOGLE_API_KEY"]
 
16
  }
17
 
18
  model = genai.GenerativeModel(
19
+ model_name="gemini-pro-vision",
20
  generation_config=generation_config,
21
  )
22
 
 
 
 
 
 
 
 
 
 
23
  def get_meme_template_description(image_bytes):
24
  """Get a description of the meme template from the image bytes."""
25
  try:
26
+ image = Image.open(io.BytesIO(image_bytes))
27
+ prompt = """
28
+ You are an AI specialized in describing meme templates. Please provide a detailed description of the meme template in this image. Include:
29
+ 1. The name of the meme template (if it's a well-known one)
30
+ 2. A description of the visual layout (number of panels, arrangement)
31
+ 3. Key visual elements in each panel
32
+ 4. The typical use or purpose of this meme template
33
+ 5. Where text is usually placed in this template
 
34
 
35
+ Format your response as a concise paragraph.
36
+ """
37
+ response = model.generate_content([prompt, image])
38
+ return response.text
 
 
39
  except Exception as e:
40
  return f"Error processing image: {e}"