Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,82 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from streamlit_option_menu import option_menu
|
| 4 |
-
from gemini_utility import (load_gemini_pro,
|
| 5 |
-
gemini_pro_vision_responce)
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
# To get the working directory
|
| 9 |
-
working_directory = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
-
|
| 11 |
-
# Setting the page config
|
| 12 |
-
st.set_page_config(
|
| 13 |
-
page_title="Gemini AI",
|
| 14 |
-
page_icon="🤖",
|
| 15 |
-
layout="centered",
|
| 16 |
-
initial_sidebar_state="expanded",
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
with st.sidebar:
|
| 20 |
-
selected = option_menu("Gemini AI",
|
| 21 |
-
["Chatbot",
|
| 22 |
-
"Image Captioning",
|
| 23 |
-
],
|
| 24 |
-
menu_icon="robot",
|
| 25 |
-
icons=['chat-dots-fill', 'image-fill'],
|
| 26 |
-
default_index=0)
|
| 27 |
-
|
| 28 |
-
def translate_role_to_streamlit(user_role):
|
| 29 |
-
if user_role == "model":
|
| 30 |
-
return "assistant"
|
| 31 |
-
else:
|
| 32 |
-
return user_role
|
| 33 |
-
|
| 34 |
-
if selected == "Chatbot":
|
| 35 |
-
model = load_gemini_pro()
|
| 36 |
-
|
| 37 |
-
# Initialize chat session in Streamlit if not present
|
| 38 |
-
if "chat_session" not in st.session_state:
|
| 39 |
-
st.session_state.chat_session = model.start_chat(history=[])
|
| 40 |
-
|
| 41 |
-
# Streamlit page title
|
| 42 |
-
st.title("Gemini Chatbot 🤖")
|
| 43 |
-
|
| 44 |
-
# Display the chatbot history
|
| 45 |
-
for message in st.session_state.chat_session.history:
|
| 46 |
-
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 47 |
-
st.markdown(message.parts[0].text)
|
| 48 |
-
|
| 49 |
-
# Input field for user's message
|
| 50 |
-
user_prompt = st.chat_input("Ask Gemini Pro...")
|
| 51 |
-
|
| 52 |
-
if user_prompt:
|
| 53 |
-
st.chat_message("user").markdown(user_prompt)
|
| 54 |
-
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
| 55 |
-
|
| 56 |
-
# Display the chatbot response
|
| 57 |
-
with st.chat_message("assistant"):
|
| 58 |
-
st.markdown(gemini_response.text)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if selected == "Image Captioning":
|
| 62 |
-
|
| 63 |
-
# Streamlit title
|
| 64 |
-
st.title("Image Caption Generation📸")
|
| 65 |
-
|
| 66 |
-
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 67 |
-
|
| 68 |
-
if upload_image and st.button("Generate"):
|
| 69 |
-
image = Image.open(upload_image)
|
| 70 |
-
|
| 71 |
-
col1, col2 = st.columns(2)
|
| 72 |
-
|
| 73 |
-
with col1:
|
| 74 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 75 |
-
|
| 76 |
-
default_prompt = "Write a caption for this image"
|
| 77 |
-
|
| 78 |
-
# Getting the response from gemini
|
| 79 |
-
caption = gemini_pro_vision_responce(default_prompt, image)
|
| 80 |
-
|
| 81 |
-
with col2:
|
| 82 |
-
st.info(caption)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from streamlit_option_menu import option_menu
|
| 4 |
+
from gemini_utility import (load_gemini_pro,
|
| 5 |
+
gemini_pro_vision_responce)
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# To get the working directory
|
| 9 |
+
working_directory = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
+
|
| 11 |
+
# Setting the page config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Gemini AI",
|
| 14 |
+
page_icon="🤖",
|
| 15 |
+
layout="centered",
|
| 16 |
+
initial_sidebar_state="expanded",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
with st.sidebar:
|
| 20 |
+
selected = option_menu("Gemini AI",
|
| 21 |
+
["Chatbot",
|
| 22 |
+
"Image Captioning",
|
| 23 |
+
],
|
| 24 |
+
menu_icon="robot",
|
| 25 |
+
icons=['chat-dots-fill', 'image-fill'],
|
| 26 |
+
default_index=0)
|
| 27 |
+
|
| 28 |
+
def translate_role_to_streamlit(user_role):
|
| 29 |
+
if user_role == "model":
|
| 30 |
+
return "assistant"
|
| 31 |
+
else:
|
| 32 |
+
return user_role
|
| 33 |
+
|
| 34 |
+
if selected == "Chatbot":
|
| 35 |
+
model = load_gemini_pro()
|
| 36 |
+
|
| 37 |
+
# Initialize chat session in Streamlit if not present
|
| 38 |
+
if "chat_session" not in st.session_state:
|
| 39 |
+
st.session_state.chat_session = model.start_chat(history=[])
|
| 40 |
+
|
| 41 |
+
# Streamlit page title
|
| 42 |
+
st.title("Gemini Chatbot 🤖")
|
| 43 |
+
|
| 44 |
+
# Display the chatbot history
|
| 45 |
+
for message in st.session_state.chat_session.history:
|
| 46 |
+
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 47 |
+
st.markdown(message.parts[0].text)
|
| 48 |
+
|
| 49 |
+
# Input field for user's message
|
| 50 |
+
user_prompt = st.chat_input("Ask Gemini Pro...")
|
| 51 |
+
|
| 52 |
+
if user_prompt:
|
| 53 |
+
st.chat_message("user").markdown(user_prompt)
|
| 54 |
+
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
| 55 |
+
|
| 56 |
+
# Display the chatbot response
|
| 57 |
+
with st.chat_message("assistant"):
|
| 58 |
+
st.markdown(gemini_response.text)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if selected == "Image Captioning":
|
| 62 |
+
|
| 63 |
+
# Streamlit title
|
| 64 |
+
st.title("Gemini Image Caption Generation📸")
|
| 65 |
+
|
| 66 |
+
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
| 67 |
+
|
| 68 |
+
if upload_image and st.button("Generate"):
|
| 69 |
+
image = Image.open(upload_image)
|
| 70 |
+
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
|
| 73 |
+
with col1:
|
| 74 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 75 |
+
|
| 76 |
+
default_prompt = "Write a caption for this image"
|
| 77 |
+
|
| 78 |
+
# Getting the response from gemini
|
| 79 |
+
caption = gemini_pro_vision_responce(default_prompt, image)
|
| 80 |
+
|
| 81 |
+
with col2:
|
| 82 |
+
st.info(caption)
|