-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (51 loc) · 2.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import streamlit as st
from genai import write_caption
def show_app():
"""
App Page
"""
# initialise the session state
st.session_state["input_image"] = None
st.session_state["state"] = (
"initial" # initial, image_selected, generate_caption, caption_generate_complete
)
st.session_state["generated_caption"] = None
with st.container():
st.title("📸 Insta Caption Generator")
st.header("1️⃣ Select an Image")
image_upload, image_camera = None, None
file_upload, camera_input = st.tabs(["File Upload", "Camera Input"])
# Tab - File Upload
with file_upload:
image_camera = st.file_uploader(
"Choose an image file",
type=["jpg", "jpeg", "png"],
accept_multiple_files=False,
)
# Tab - Camera Input
with camera_input:
image_upload = st.camera_input("Take a picture", label_visibility="hidden")
# if image is available via one of file upload or camera input, update the session state and display the image
if image_upload or image_camera:
st.session_state["state"] = "image_selected"
st.session_state["input_image"] = image_upload or image_camera
st.image(st.session_state["input_image"], use_column_width=True)
st.header("2️⃣ Generate Caption")
st.caption("Powered by Google Gemini ✨")
# if button clicked, set the state as 'generate_caption'
if st.button("✨ Generate Caption", use_container_width=True, type="primary", disabled=st.session_state["state"] != "image_selected"):
st.session_state["state"] = "generate_caption"
# if state is set as 'generate_caption', generate caption using long running process behind the spinner
if st.session_state["state"] == "generate_caption":
with st.spinner("Generating caption..."):
st.session_state["generated_caption"] = write_caption(
st.session_state["input_image"].getvalue(),
st.session_state["input_image"].type,
)
st.session_state["state"] = "caption_generate_complete"
st.divider()
# if completed, display the result along with flying balloons
if st.session_state["state"] == "caption_generate_complete":
st.caption("✨ Generated Caption")
st.markdown(st.session_state["generated_caption"])
st.balloons()