cha0smagick commited on
Commit
dd6c9a9
1 Parent(s): 3a20421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -37
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
- from pathlib import Path
3
  from PIL import Image
4
- from libs.gemini_vision import GeminiVision
 
5
 
6
  # Initialize session state
7
  def init_session_state():
@@ -13,24 +13,20 @@ def init_session_state():
13
  st.session_state['top_k'] = 32
14
  if 'top_p' not in st.session_state:
15
  st.session_state['top_p'] = 1.0
16
- if 'gemini_vision' not in st.session_state:
17
- st.session_state['gemini_vision'] = None
18
-
19
- # Exception handling decorator
20
- def exception_handler(func):
21
- def wrapper(*args, **kwargs):
22
- try:
23
- return func(*args, **kwargs)
24
- except Exception as exception:
25
- st.session_state.logger.error(f"An error occurred in {func.__name__}: {exception}")
26
- st.error(f"An error occurred: {exception}")
27
- st.session_state.logger.error(traceback.format_exc())
28
- st.stop()
29
- return wrapper
30
 
31
  # Streamlit App
32
- def streamlit_app():
33
- st.title("Gemini Vision - Image Descriptions")
 
 
 
 
34
 
35
  # Display the Gemini Sidebar settings
36
  with st.sidebar.title("Gemini Settings"):
@@ -39,28 +35,51 @@ def streamlit_app():
39
  st.session_state.top_k = st.sidebar.number_input("Top K", value=32)
40
  st.session_state.top_p = st.sidebar.slider("Top P", 0.0, 1.0, 1.0)
41
 
42
- if st.button("Apply Settings"):
43
- st.session_state.gemini_vision = GeminiVision(
44
- st.session_state.api_key,
45
- st.session_state.temperature,
46
- st.session_state.top_p,
47
- st.session_state.top_k
48
- )
49
- st.success("Settings applied successfully!")
 
 
 
 
 
 
 
 
50
 
51
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
52
 
53
- if uploaded_file is not None:
54
  # Display the uploaded image
55
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- if st.session_state.gemini_vision is not None:
58
- # Generate description using Gemini Vision API
59
- image = Image.open(uploaded_file)
60
- description = st.session_state.gemini_vision.generate_content(contents=[image])
61
- st.success(f"Gemini Vision Description: {description.text}")
62
 
63
- # Main
64
  if __name__ == "__main__":
65
- init_session_state()
66
- streamlit_app()
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  from PIL import Image
3
+ from google.generativeai import GenerativeModel # Assuming the actual module from Gemini Vision API
4
+ from pathlib import Path
5
 
6
  # Initialize session state
7
  def init_session_state():
 
13
  st.session_state['top_k'] = 32
14
  if 'top_p' not in st.session_state:
15
  st.session_state['top_p'] = 1.0
16
+ if 'gemini_model' not in st.session_state:
17
+ st.session_state['gemini_model'] = None
18
+ if 'uploaded_image' not in st.session_state:
19
+ st.session_state['uploaded_image'] = None
20
+ if 'image_description' not in st.session_state:
21
+ st.session_state['image_description'] = ''
 
 
 
 
 
 
 
 
22
 
23
  # Streamlit App
24
+ def streamlit_app():
25
+ # Google Logo and Title
26
+ st.write('<div style="display: flex; flex-direction: row; align-items: center; justify-content: center;"><a style="margin-right: 10px;" href="https://www.google.com" target="_blank"><img src="https://img.icons8.com/color/32/000000/google-logo.png"/></a><h1 style="margin-left: 10px;">Google - Gemini Vision</h1></div>', unsafe_allow_html=True)
27
+
28
+ # Display support
29
+ display_support()
30
 
31
  # Display the Gemini Sidebar settings
32
  with st.sidebar.title("Gemini Settings"):
 
35
  st.session_state.top_k = st.sidebar.number_input("Top K", value=32)
36
  st.session_state.top_p = st.sidebar.slider("Top P", 0.0, 1.0, 1.0)
37
 
38
+ if (st.session_state.api_key is not None and st.session_state.api_key != '') \
39
+ and (st.session_state.temperature is not None and st.session_state.temperature != '') \
40
+ and (st.session_state.top_k is not None and st.session_state.top_k != '') \
41
+ and (st.session_state.top_p is not None and st.session_state.top_p != ''):
42
+ st.toast("Settings updated successfully!", icon="👍")
43
+ else:
44
+ st.toast("Please enter all the settings.\nAPI Key is required", icon="❌")
45
+ raise ValueError("Please enter all values the settings.\nAPI Key is required")
46
+
47
+ # Initialize services once
48
+ if st.session_state.gemini_model is None:
49
+ st.session_state.gemini_model = GenerativeModel(model_name="gemini-pro-vision",
50
+ api_key=st.session_state['api_key'],
51
+ temperature=st.session_state['temperature'],
52
+ top_p=st.session_state['top_p'],
53
+ top_k=st.session_state['top_k'])
54
 
55
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
56
 
57
+ if uploaded_image:
58
  # Display the uploaded image
59
+ st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
60
+
61
+ # Store the uploaded image in session state
62
+ st.session_state['uploaded_image'] = uploaded_image
63
+
64
+ if st.button("Generate Image Description"):
65
+ try:
66
+ # Process the uploaded image using Gemini Vision API
67
+ image_description = st.session_state.gemini_model.generate_content(contents=[uploaded_image])
68
+ st.session_state['image_description'] = image_description.text
69
+ st.success(f"Image description generated: {image_description.text}")
70
+ except Exception as e:
71
+ st.error(f"An error occurred: {e}")
72
 
73
+ # Display the generated image description
74
+ if st.session_state['image_description']:
75
+ st.code(f"Image Description: {st.session_state['image_description']}", language="plaintext")
 
 
76
 
 
77
  if __name__ == "__main__":
78
+ try:
79
+ init_session_state()
80
+ streamlit_app()
81
+ except Exception as exception:
82
+ import traceback
83
+ st.session_state.logger.error(f"An error occurred: {exception}")
84
+ st.session_state.logger.error(traceback.format_exc())
85
+ st.error(f"An error occurred: {exception}")