1Prarthana commited on
Commit
a8bbadc
·
verified ·
1 Parent(s): 666ed00

Upload 4 files

Browse files
Files changed (4) hide show
  1. Config.json +1 -0
  2. app.py +118 -0
  3. gemini_utility.py +67 -0
  4. requirements.txt +1 -0
Config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ { "GOOGLE_API_KEY": "AIzaSyCkC1v75TynkOx6V4hk903jhDHtLz4V2BQ" }
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from PIL import Image
4
+ import streamlit as st
5
+ from streamlit_option_menu import option_menu
6
+
7
+ from gemini_utility import (load_gemini_pro_model,
8
+ gemini_pro_response,
9
+ gemini_pro_vision_response,
10
+ embeddings_model_response)
11
+
12
+
13
+ working_dir = os.path.dirname(os.path.abspath(__file__))
14
+
15
+ st.set_page_config(
16
+ page_title="Gemini AI",
17
+ page_icon="🧠",
18
+ layout="centered",
19
+ )
20
+
21
+ with st.sidebar:
22
+ selected = option_menu('Gemini AI',
23
+ ['ChatBot',
24
+ 'Image Captioning',
25
+ 'Embed text',
26
+ 'Ask me anything'],
27
+ menu_icon='robot', icons=['chat-dots-fill', 'image-fill', 'textarea-t', 'patch-question-fill'],
28
+ default_index=0
29
+ )
30
+
31
+
32
+ # Function to translate roles between Gemini-Pro and Streamlit terminology
33
+ def translate_role_for_streamlit(user_role):
34
+ if user_role == "model":
35
+ return "assistant"
36
+ else:
37
+ return user_role
38
+
39
+
40
+ # chatbot page
41
+ if selected == 'ChatBot':
42
+ model = load_gemini_pro_model()
43
+
44
+ # Initialize chat session in Streamlit if not already present
45
+ if "chat_session" not in st.session_state: # Renamed for clarity
46
+ st.session_state.chat_session = model.start_chat(history=[])
47
+
48
+ # Display the chatbot's title on the page
49
+ st.title("🤖 ChatBot")
50
+
51
+ # Display the chat history
52
+ for message in st.session_state.chat_session.history:
53
+ with st.chat_message(translate_role_for_streamlit(message.role)):
54
+ st.markdown(message.parts[0].text)
55
+
56
+ # Input field for user's message
57
+ user_prompt = st.chat_input("Ask Gemini-Pro...") # Renamed for clarity
58
+ if user_prompt:
59
+ # Add user's message to chat and display it
60
+ st.chat_message("user").markdown(user_prompt)
61
+
62
+ # Send user's message to Gemini-Pro and get the response
63
+ gemini_response = st.session_state.chat_session.send_message(user_prompt) # Renamed for clarity
64
+
65
+ # Display Gemini-Pro's response
66
+ with st.chat_message("assistant"):
67
+ st.markdown(gemini_response.text)
68
+
69
+
70
+ # Image captioning page
71
+ if selected == "Image Captioning":
72
+
73
+ st.title("📷 Snap Narrate")
74
+
75
+ uploaded_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
76
+
77
+ if st.button("Generate Caption"):
78
+ image = Image.open(uploaded_image)
79
+
80
+ col1, col2 = st.columns(2)
81
+
82
+ with col1:
83
+ resized_img = image.resize((800, 500))
84
+ st.image(resized_img)
85
+
86
+ default_prompt = "write a short caption for this image" # change this prompt as per your requirement
87
+
88
+ # get the caption of the image from the gemini-pro-vision LLM
89
+ caption = gemini_pro_vision_response(default_prompt, image)
90
+
91
+ with col2:
92
+ st.info(caption)
93
+
94
+
95
+ # text embedding model
96
+ if selected == "Embed text":
97
+
98
+ st.title("🔡 Embed Text")
99
+
100
+ # text box to enter prompt
101
+ user_prompt = st.text_area(label='', placeholder="Enter the text to get embeddings")
102
+
103
+ if st.button("Get Response"):
104
+ response = embeddings_model_response(user_prompt)
105
+ st.markdown(response)
106
+
107
+
108
+ # text embedding model
109
+ if selected == "Ask me anything":
110
+
111
+ st.title("❓ Ask me a question")
112
+
113
+ # text box to enter prompt
114
+ user_prompt = st.text_area(label='', placeholder="Ask me anything...")
115
+
116
+ if st.button("Get Response"):
117
+ response = gemini_pro_response(user_prompt)
118
+ st.markdown(response)
gemini_utility.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from PIL import Image
4
+
5
+ import google.generativeai as genai
6
+
7
+ # working directory path
8
+ working_dir = os.path.dirname(os.path.abspath(__file__))
9
+
10
+ # path of config_data file
11
+ config_file_path = f"{working_dir}/Config.json"
12
+ with open(config_file_path, "r") as f:
13
+ config_data = json.load(f)
14
+
15
+ # config_data = json.load(open("Config.json"))
16
+
17
+ # loading the GOOGLE_API_KEY
18
+ GOOGLE_API_KEY = config_data["GOOGLE_API_KEY"]
19
+
20
+ # configuring google.generativeai with API key
21
+ genai.configure(api_key=GOOGLE_API_KEY)
22
+
23
+
24
+ def load_gemini_pro_model():
25
+ gemini_pro_model = genai.GenerativeModel("gemini-2.0-flash")
26
+ return gemini_pro_model
27
+
28
+
29
+ # get response from Gemini-Pro-Vision model - image/text to text
30
+ def gemini_pro_vision_response(prompt, image):
31
+ gemini_pro_vision_model = genai.GenerativeModel("gemini-2.0-flash")
32
+ response = gemini_pro_vision_model.generate_content([prompt, image])
33
+ result = response.text
34
+ return result
35
+
36
+
37
+ # get response from embeddings model - text to embeddings
38
+ def embeddings_model_response(input_text):
39
+ embedding_model = "models/embedding-001"
40
+ embedding = genai.embed_content(model=embedding_model,
41
+ content=input_text,
42
+ task_type="retrieval_document")
43
+ embedding_list = embedding["embedding"]
44
+ return embedding_list
45
+
46
+
47
+ # get response from Gemini-Pro model - text to text
48
+ def gemini_pro_response(user_prompt):
49
+ gemini_pro_model = genai.GenerativeModel("gemini-2.0-flash")
50
+ response = gemini_pro_model.generate_content(user_prompt)
51
+ result = response.text
52
+ return result
53
+
54
+
55
+ # result = gemini_pro_response("What is Machine Learning")
56
+ # print(result)
57
+ # print("-"*50)
58
+ #
59
+ #
60
+ # image = Image.open("test_image.png")
61
+ # result = gemini_pro_vision_response("Write a short caption for this image", image)
62
+ # print(result)
63
+ # print("-"*50)
64
+ #
65
+ #
66
+ # result = embeddings_model_response("Machine Learning is a subset of Artificial Intelligence")
67
+ # print(result)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ streamlit~=1.31.1