Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,14 +12,12 @@ api_key = os.getenv("GOOGLE_API_KEY")
|
|
| 12 |
|
| 13 |
# If .env file is missing or API key is not found, ask the user to input it
|
| 14 |
if not api_key:
|
| 15 |
-
st.warning("π Google API key not found in `.env` file. Please enter your API key below
|
| 16 |
api_key = st.text_input("Enter your Google API Key:", type="password")
|
| 17 |
-
|
| 18 |
if not api_key:
|
| 19 |
st.error("π¨ API key is required to proceed.")
|
| 20 |
st.stop()
|
| 21 |
else:
|
| 22 |
-
# Save the API key to the environment for the session
|
| 23 |
os.environ["GOOGLE_API_KEY"] = api_key
|
| 24 |
|
| 25 |
# β
Configure Gemini Model
|
|
@@ -50,32 +48,68 @@ conversation = ConversationChain(
|
|
| 50 |
prompt=PromptTemplate(template=prompt_template, input_variables=["history", "input"])
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# β
Streamlit UI
|
| 54 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
st.title("π§ Conversational AI Data Science Tutor")
|
| 56 |
|
| 57 |
# Initialize session state for messages
|
| 58 |
if "messages" not in st.session_state:
|
| 59 |
st.session_state.messages = []
|
| 60 |
|
| 61 |
-
# Display
|
| 62 |
for message in st.session_state.messages:
|
| 63 |
with st.chat_message(message["role"]):
|
| 64 |
if message["role"] == "assistant":
|
| 65 |
-
|
|
|
|
| 66 |
else:
|
| 67 |
st.markdown(message["content"])
|
| 68 |
|
| 69 |
-
# User Input
|
| 70 |
-
user_input = st.chat_input("Ask
|
| 71 |
|
| 72 |
if user_input:
|
| 73 |
-
# Display
|
| 74 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 75 |
with st.chat_message("user"):
|
| 76 |
st.markdown(user_input)
|
| 77 |
|
| 78 |
-
# Generate
|
| 79 |
with st.spinner("Thinking..."):
|
| 80 |
try:
|
| 81 |
response = conversation.predict(input=user_input)
|
|
@@ -84,16 +118,17 @@ if user_input:
|
|
| 84 |
with st.expander("See error details"):
|
| 85 |
st.code(str(e))
|
| 86 |
|
| 87 |
-
# Display AI
|
| 88 |
with st.chat_message("assistant"):
|
| 89 |
st.markdown(f"```python\n{response}\n```")
|
| 90 |
|
| 91 |
-
#
|
| 92 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 93 |
|
| 94 |
# β
Reset Chat Button
|
| 95 |
if st.button("Reset Chat"):
|
| 96 |
-
if st.
|
| 97 |
-
st.
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
| 12 |
|
| 13 |
# If .env file is missing or API key is not found, ask the user to input it
|
| 14 |
if not api_key:
|
| 15 |
+
st.warning("π Google API key not found in `.env` file. Please enter your API key below:")
|
| 16 |
api_key = st.text_input("Enter your Google API Key:", type="password")
|
|
|
|
| 17 |
if not api_key:
|
| 18 |
st.error("π¨ API key is required to proceed.")
|
| 19 |
st.stop()
|
| 20 |
else:
|
|
|
|
| 21 |
os.environ["GOOGLE_API_KEY"] = api_key
|
| 22 |
|
| 23 |
# β
Configure Gemini Model
|
|
|
|
| 48 |
prompt=PromptTemplate(template=prompt_template, input_variables=["history", "input"])
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# β
Streamlit UI Config
|
| 52 |
+
st.set_page_config(
|
| 53 |
+
page_title="AI Data Science Tutor",
|
| 54 |
+
layout="wide",
|
| 55 |
+
initial_sidebar_state="collapsed"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# β
Custom CSS for Clean UI and Reduced Spacing
|
| 59 |
+
st.markdown(
|
| 60 |
+
"""
|
| 61 |
+
<style>
|
| 62 |
+
.stApp {
|
| 63 |
+
padding-top: 0.5rem;
|
| 64 |
+
padding-bottom: 1rem;
|
| 65 |
+
}
|
| 66 |
+
.stTitle {
|
| 67 |
+
margin-bottom: -0.5rem;
|
| 68 |
+
font-size: 2rem;
|
| 69 |
+
font-weight: 600;
|
| 70 |
+
color: #4CAF50;
|
| 71 |
+
}
|
| 72 |
+
.stChatInputContainer {
|
| 73 |
+
margin-top: -1.0rem;
|
| 74 |
+
}
|
| 75 |
+
.stMarkdown {
|
| 76 |
+
font-size: 1rem;
|
| 77 |
+
line-height: 1.6;
|
| 78 |
+
}
|
| 79 |
+
.css-1kyxreq {
|
| 80 |
+
display: none;
|
| 81 |
+
}
|
| 82 |
+
</style>
|
| 83 |
+
""",
|
| 84 |
+
unsafe_allow_html=True
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# β
Title
|
| 88 |
st.title("π§ Conversational AI Data Science Tutor")
|
| 89 |
|
| 90 |
# Initialize session state for messages
|
| 91 |
if "messages" not in st.session_state:
|
| 92 |
st.session_state.messages = []
|
| 93 |
|
| 94 |
+
# β
Display Previous Messages
|
| 95 |
for message in st.session_state.messages:
|
| 96 |
with st.chat_message(message["role"]):
|
| 97 |
if message["role"] == "assistant":
|
| 98 |
+
# Use Markdown for better readability and formatting
|
| 99 |
+
st.markdown(f"```python\n{message['content']}\n```")
|
| 100 |
else:
|
| 101 |
st.markdown(message["content"])
|
| 102 |
|
| 103 |
+
# β
User Input
|
| 104 |
+
user_input = st.chat_input("Ask a data science question (e.g., What is linear regression?)...")
|
| 105 |
|
| 106 |
if user_input:
|
| 107 |
+
# Display User Message
|
| 108 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 109 |
with st.chat_message("user"):
|
| 110 |
st.markdown(user_input)
|
| 111 |
|
| 112 |
+
# Generate AI Response
|
| 113 |
with st.spinner("Thinking..."):
|
| 114 |
try:
|
| 115 |
response = conversation.predict(input=user_input)
|
|
|
|
| 118 |
with st.expander("See error details"):
|
| 119 |
st.code(str(e))
|
| 120 |
|
| 121 |
+
# Display AI Response
|
| 122 |
with st.chat_message("assistant"):
|
| 123 |
st.markdown(f"```python\n{response}\n```")
|
| 124 |
|
| 125 |
+
# Save AI Message to History
|
| 126 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 127 |
|
| 128 |
# β
Reset Chat Button
|
| 129 |
if st.button("Reset Chat"):
|
| 130 |
+
if st.session_state.messages: # Only show confirmation if thereβs a conversation history
|
| 131 |
+
if st.confirm("Are you sure you want to reset the chat?"):
|
| 132 |
+
st.session_state.messages = []
|
| 133 |
+
memory.clear()
|
| 134 |
+
st.rerun()
|