Yadvendra commited on
Commit
482f183
·
verified ·
1 Parent(s): fefb928

Upload 4 files

Browse files
Files changed (4) hide show
  1. config.json +1 -0
  2. gemini_utility.py +27 -0
  3. main.py +82 -0
  4. requirements.txt +4 -0
config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"GOOGLE_API_KEY" : "AIzaSyDwRCnePROIZ83NM3c1iTMGNDBlNPSz_kU"}
gemini_utility.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import google.generativeai as genai
4
+ from PIL import Image
5
+
6
+ #to get the working directory
7
+ working_directory = os.path.dirname(os.path.abspath(__file__))
8
+ config_file_path = f"{working_directory}/config.json"
9
+ config_data = json.load(open(config_file_path))
10
+
11
+
12
+ #loading the API key
13
+ GOOGLE_API_KEY = config_data["GOOGLE_API_KEY"]
14
+ genai.configure(api_key= GOOGLE_API_KEY)
15
+
16
+ #function to load gemini pro model
17
+ def load_gemini_pro():
18
+ gemini_pro_model = genai.GenerativeModel("gemini-1.5-flash")
19
+ return gemini_pro_model
20
+
21
+ # Function to load image vision model
22
+ def gemini_pro_vision_responce(prompt, image):
23
+ gemini_pro_vision_model = genai.GenerativeModel("gemini-1.5-pro")
24
+ responce = gemini_pro_vision_model.generate_content([prompt, image])
25
+ result = responce.text
26
+ return result
27
+
main.py ADDED
@@ -0,0 +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)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ google-generativeai
2
+ pillow
3
+ streamlit
4
+ streamlit-option-menu