-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (99 loc) · 3.69 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# import os
#
# import streamlit as st
# from dotenv import load_dotenv
# import google.generativeai as gen_ai
#
#
# # Load environment variables
# load_dotenv()
#
# # Configure Streamlit page settings
# st.set_page_config(
# page_title="Chat with Gemini-Pro!",
# page_icon=":brain:", # Favicon emoji
# layout="centered", # Page layout option
# )
#
# GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
#
# # Set up Google Gemini-Pro AI model
# gen_ai.configure(api_key=GOOGLE_API_KEY)
# model = gen_ai.GenerativeModel('gemini-pro')
#
#
# # Function to translate roles between Gemini-Pro and Streamlit terminology
# def translate_role_for_streamlit(user_role):
# if user_role == "model":
# return "assistant"
# else:
# return user_role
#
#
# # Initialize chat session in Streamlit if not already present
# if "chat_session" not in st.session_state:
# st.session_state.chat_session = model.start_chat(history=[])
#
#
# # Display the chatbot's title on the page
# st.title("🤖 Gemini Pro - ChatBot")
#
# # Display the chat history
# for message in st.session_state.chat_session.history:
# with st.chat_message(translate_role_for_streamlit(message.role)):
# st.markdown(message.parts[0].text)
#
# # Input field for user's message
# user_prompt = st.chat_input("Ask Gemini-Pro...")
# if user_prompt:
# # Add user's message to chat and display it
# st.chat_message("user").markdown(user_prompt)
#
# # Send user's message to Gemini-Pro and get the response
# gemini_response = st.session_state.chat_session.send_message(user_prompt)
#
# # Display Gemini-Pro's response
# with st.chat_message("assistant"):
# st.markdown(gemini_response.text)
import os
import streamlit as st
import google.generativeai as gen_ai
# Configure Streamlit page settings
st.set_page_config(
page_title="Chat with Gemini-Pro!",
page_icon=":brain:", # Favicon emoji
layout="centered", # Page layout option
)
# Input field for Google API key
api_key_input = st.text_input("Enter your Google API Key:")
# Only configure the API and initialize the chat session if the API key is provided
if api_key_input:
# Save the API key in the session state
st.session_state.GOOGLE_API_KEY = api_key_input
# Configure the Google Gemini-Pro AI model
gen_ai.configure(api_key=st.session_state.GOOGLE_API_KEY)
model = gen_ai.GenerativeModel('gemini-pro')
# Initialize the chat session if not already present
if "chat_session" not in st.session_state:
st.session_state.chat_session = model.start_chat(history=[])
# Function to translate roles between Gemini-Pro and Streamlit terminology
def translate_role_for_streamlit(user_role):
return "assistant" if user_role == "model" else user_role
# Display the chatbot's title on the page
st.title("🤖 Gemini Pro - ChatBot")
# Display the chat history
for message in st.session_state.chat_session.history:
with st.chat_message(translate_role_for_streamlit(message.role)):
st.markdown(message.parts[0].text)
# Input field for user's message
user_prompt = st.chat_input("Ask Gemini-Pro...")
if user_prompt:
# Add user's message to chat and display it
st.chat_message("user").markdown(user_prompt)
# Send user's message to Gemini-Pro and get the response
gemini_response = st.session_state.chat_session.send_message(user_prompt)
# Display Gemini-Pro's response
with st.chat_message("assistant"):
st.markdown(gemini_response.text)
else:
st.warning("Please enter your Google API Key to start the chat.")