Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
load_dotenv() # Load environment variables from .env file
|
9 |
+
|
10 |
+
api_key = os.getenv("gemini_api")
|
11 |
+
|
12 |
+
|
13 |
+
#api_key = st.secrets["gemini_api"] # Ensure this is set in your Streamlit secrets
|
14 |
+
genai.configure(api_key=api_key)
|
15 |
+
# Initialize the Generative Model
|
16 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
|
17 |
+
|
18 |
+
# Function to get the response from the model
|
19 |
+
def get_response(prompt):
|
20 |
+
context_before = "Give me a short summary of the book"
|
21 |
+
context_after = """You should consider reading it if: (3 to 4 reasons/person interests),
|
22 |
+
you should skip it if: (3 to 4 reasons/person interests)"""
|
23 |
+
prompt = f"{context_before} {prompt}. {context_after}"
|
24 |
+
response = model.generate_content([prompt])
|
25 |
+
return response.text
|
26 |
+
|
27 |
+
# Streamlit App
|
28 |
+
st.set_page_config(page_title="Book Summary Chatbot", page_icon="π", layout="wide")
|
29 |
+
|
30 |
+
# Custom CSS for chat layout
|
31 |
+
st.markdown("""
|
32 |
+
<style>
|
33 |
+
.chat-container {
|
34 |
+
background-color: #F9F9F9;
|
35 |
+
padding: 20px;
|
36 |
+
border-radius: 10px;
|
37 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
38 |
+
margin: 20px auto;
|
39 |
+
max-width: 700px;
|
40 |
+
}
|
41 |
+
.chat-message {
|
42 |
+
padding: 10px;
|
43 |
+
margin: 5px 0;
|
44 |
+
border-radius: 5px;
|
45 |
+
}
|
46 |
+
.user-message {
|
47 |
+
background-color: #DCF8C6;
|
48 |
+
text-align: right;
|
49 |
+
}
|
50 |
+
.assistant-message {
|
51 |
+
background-color: #E4E6EB;
|
52 |
+
text-align: left;
|
53 |
+
}
|
54 |
+
.centered-header {
|
55 |
+
text-align: center;
|
56 |
+
color: #4A90A2;
|
57 |
+
margin-bottom: 20px;
|
58 |
+
}
|
59 |
+
</style>
|
60 |
+
""", unsafe_allow_html=True)
|
61 |
+
|
62 |
+
# Page Header
|
63 |
+
st.markdown("<h1 class='centered-header'>Book Summary Chatbot</h1>", unsafe_allow_html=True)
|
64 |
+
st.write("Note: This chatbot will take the name of a book and provide a summary along with advice on whether you should read it or not.")
|
65 |
+
|
66 |
+
# Initialize session state for messages
|
67 |
+
if "messages" not in st.session_state:
|
68 |
+
st.session_state.messages = []
|
69 |
+
|
70 |
+
# Chat Container
|
71 |
+
with st.container():
|
72 |
+
chat_container = st.container()
|
73 |
+
|
74 |
+
# Display existing chat messages in a styled box
|
75 |
+
with chat_container:
|
76 |
+
for message in st.session_state.messages:
|
77 |
+
if message["role"] == "user":
|
78 |
+
st.markdown(f"<div class='chat-message user-message'>{message['content']}</div>", unsafe_allow_html=True)
|
79 |
+
else:
|
80 |
+
st.markdown(f"<div class='chat-message assistant-message'>{message['content']}</div>", unsafe_allow_html=True)
|
81 |
+
|
82 |
+
# Handle new input - always at the bottom
|
83 |
+
prompt = st.chat_input("Please enter the name of a book")
|
84 |
+
if prompt:
|
85 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
86 |
+
|
87 |
+
with st.chat_message("user"):
|
88 |
+
st.markdown(f"<div class='chat-message user-message'>{prompt}</div>", unsafe_allow_html=True)
|
89 |
+
|
90 |
+
response = get_response(prompt)
|
91 |
+
#st.write(list(response))
|
92 |
+
if response.startswith("##"):
|
93 |
+
response = response.replace("##", "", 2)
|
94 |
+
for i in range(1):
|
95 |
+
response = response.replace(" ", "", 1)
|
96 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
97 |
+
|
98 |
+
with st.chat_message("assistant"):
|
99 |
+
|
100 |
+
st.markdown(f"<div class='chat-message assistant-message'>{response}</div>", unsafe_allow_html=True)
|