Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,69 @@
|
|
1 |
-
import os
|
2 |
-
import streamlit as st
|
3 |
-
import google.generativeai as gen_ai
|
4 |
-
import pyttsx3
|
5 |
-
import threading
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
|
8 |
-
# Load environment variables
|
9 |
-
load_dotenv()
|
10 |
-
|
11 |
-
# Configure Streamlit page settings
|
12 |
-
st.set_page_config(
|
13 |
-
page_title="Gemini-Pro ChatBot",
|
14 |
-
page_icon="π€", # Favicon emoji
|
15 |
-
layout="centered", # Page layout option
|
16 |
-
)
|
17 |
-
|
18 |
-
# Retrieve Google API Key
|
19 |
-
Google_API_Key = os.getenv("Google_API_Key")
|
20 |
-
|
21 |
-
# Set up Google Gemini-Pro AI Model
|
22 |
-
gen_ai.configure(api_key=Google_API_Key)
|
23 |
-
model = gen_ai.GenerativeModel('gemini-
|
24 |
-
|
25 |
-
# Function to translate roles between Gemini-Pro and Streamlit terminology
|
26 |
-
def translate_role_for_streamlit(user_role):
|
27 |
-
return "assistant" if user_role == "model" else user_role
|
28 |
-
|
29 |
-
# Function to handle text-to-speech (TTS) in a separate thread
|
30 |
-
def speak_text(text):
|
31 |
-
engine = pyttsx3.init()
|
32 |
-
engine.say(text)
|
33 |
-
engine.runAndWait()
|
34 |
-
|
35 |
-
# Initialize chat session in Streamlit if not already present
|
36 |
-
if "chat_session" not in st.session_state:
|
37 |
-
st.session_state.chat_session = model.start_chat(history=[])
|
38 |
-
|
39 |
-
# Display chatbot title and description
|
40 |
-
st.markdown("<h1 style='text-align: center; color: #4A90E2;'>π€ Gemini-Pro ChatBot</h1>", unsafe_allow_html=True)
|
41 |
-
st.markdown("<p style='text-align: center; font-size: 16px;'>Ask me anything! I'm powered by Gemini-Pro AI.</p>", unsafe_allow_html=True)
|
42 |
-
|
43 |
-
# Display chat history
|
44 |
-
for message in st.session_state.chat_session.history:
|
45 |
-
with st.chat_message(translate_role_for_streamlit(message.role)):
|
46 |
-
st.markdown(message.parts[0].text)
|
47 |
-
|
48 |
-
# User input field
|
49 |
-
user_prompt = st.chat_input("Ask Gemini Pro...")
|
50 |
-
|
51 |
-
# If user enters a prompt
|
52 |
-
if user_prompt:
|
53 |
-
# Display user's message
|
54 |
-
st.chat_message("user").markdown(user_prompt)
|
55 |
-
|
56 |
-
# Show a loading indicator while waiting for a response
|
57 |
-
with st.spinner("Thinking..."):
|
58 |
-
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
59 |
-
|
60 |
-
# Display Gemini-Pro's response
|
61 |
-
with st.chat_message("assistant"):
|
62 |
-
st.markdown(gemini_response.text)
|
63 |
-
|
64 |
-
# Run text-to-speech in the background
|
65 |
-
threading.Thread(target=speak_text, args=(gemini_response.text,), daemon=True).start()
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import google.generativeai as gen_ai
|
4 |
+
import pyttsx3
|
5 |
+
import threading
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Configure Streamlit page settings
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Gemini-Pro ChatBot",
|
14 |
+
page_icon="π€", # Favicon emoji
|
15 |
+
layout="centered", # Page layout option
|
16 |
+
)
|
17 |
+
|
18 |
+
# Retrieve Google API Key
|
19 |
+
Google_API_Key = os.getenv("Google_API_Key")
|
20 |
+
|
21 |
+
# Set up Google Gemini-Pro AI Model
|
22 |
+
gen_ai.configure(api_key=Google_API_Key)
|
23 |
+
model = gen_ai.GenerativeModel('gemini-2.0-flash')
|
24 |
+
|
25 |
+
# Function to translate roles between Gemini-Pro and Streamlit terminology
|
26 |
+
def translate_role_for_streamlit(user_role):
|
27 |
+
return "assistant" if user_role == "model" else user_role
|
28 |
+
|
29 |
+
# Function to handle text-to-speech (TTS) in a separate thread
|
30 |
+
def speak_text(text):
|
31 |
+
engine = pyttsx3.init()
|
32 |
+
engine.say(text)
|
33 |
+
engine.runAndWait()
|
34 |
+
|
35 |
+
# Initialize chat session in Streamlit if not already present
|
36 |
+
if "chat_session" not in st.session_state:
|
37 |
+
st.session_state.chat_session = model.start_chat(history=[])
|
38 |
+
|
39 |
+
# Display chatbot title and description
|
40 |
+
st.markdown("<h1 style='text-align: center; color: #4A90E2;'>π€ Gemini-Pro ChatBot</h1>", unsafe_allow_html=True)
|
41 |
+
st.markdown("<p style='text-align: center; font-size: 16px;'>Ask me anything! I'm powered by Gemini-Pro AI.</p>", unsafe_allow_html=True)
|
42 |
+
|
43 |
+
# Display chat history
|
44 |
+
for message in st.session_state.chat_session.history:
|
45 |
+
with st.chat_message(translate_role_for_streamlit(message.role)):
|
46 |
+
st.markdown(message.parts[0].text)
|
47 |
+
|
48 |
+
# User input field
|
49 |
+
user_prompt = st.chat_input("Ask Gemini Pro...")
|
50 |
+
|
51 |
+
# If user enters a prompt
|
52 |
+
if user_prompt:
|
53 |
+
# Display user's message
|
54 |
+
st.chat_message("user").markdown(user_prompt)
|
55 |
+
|
56 |
+
# Show a loading indicator while waiting for a response
|
57 |
+
with st.spinner("Thinking..."):
|
58 |
+
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
59 |
+
|
60 |
+
# Display Gemini-Pro's response
|
61 |
+
with st.chat_message("assistant"):
|
62 |
+
st.markdown(gemini_response.text)
|
63 |
+
|
64 |
+
# Run text-to-speech in the background
|
65 |
+
threading.Thread(target=speak_text, args=(gemini_response.text,), daemon=True).start()
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|